A place to cache linked articles (think custom and personal wayback machine)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 4 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. title: Speeding Things Up With Service Worker, Resource Hints, and More
  2. url: https://www.aaron-gustafson.com/notebook/speeding-things-up-with-service-worker-resource-hints-and-more/
  3. hash_url: 3d972c32b16f08bca1aec9f23c059ed0
  4. <p>User experience encompasses more than just the interface. Download speed, render performance, and the cost of accessing a site are often overlooked areas when it comes to the practice of UX, but they all affect how users experience what we build on the Web.</p>
  5. <p>I’m always looking for ways to improve these aspects of my own site. And, since it’s my own personal playground, I often use it as a test-bed for new technologies, ideas, and techniques. My latest adventure was inspired by a bunch of <a href="https://www.aaron-gustafson.com/notebook/links/">articles and posts I’ve linked to recently</a>, especially</p>
  6. <ul>
  7. <li><a href="https://adactio.com/journal/9775">Jeremy Keith’s “My First Service Worker”</a>,</li>
  8. <li><a href="https://css-tricks.com/serviceworker-for-offline/">Nicolas Bevacqua’s “Making a Simple Site Work Offline with ServiceWorker”</a>,</li>
  9. <li><a href="http://deanhume.com/Home/BlogPost/service-workers--dynamic-responsive-images-using-webp-images/10132/">Dean Hume’s “Service Workers: Dynamic Responsive Images Using Webp Images”</a>, and</li>
  10. <li><a href="https://medium.com/@cramforce/not-so-micro-optimizations-f867c47b832d#.satdv0fap">Malte Ubl’s “Not so micro optimizations”</a></li>
  11. </ul>
  12. <p>After reading these pieces, I decided to see how much I could do to improve the performance of this site, especially on posts with a lot of images and embedded code samples, like <a href="https://www.aaron-gustafson.com/notebook/labeled-with-love/">my recent post on form labels</a>.</p>
  13. <h2 id="using-resource-hints">Using Resource Hints</h2>
  14. <p>To kick things off, I followed Malte’s advice and used <a href="https://w3c.github.io/resource-hints/">Resource Hints</a> to <em>prime the pump</em> for any third-party servers hosting assets I use frequently (e.g. Disqus, Twitter, etc.). I used the code Malte references in <a href="https://github.com/ampproject/amphtml">the AMP Project</a> as my starting point and <a href="https://github.com/aarongustafson/aarongustafson.github.io/blob/source/source/_javascript/main/resource-hints.js">added two new methods (<code>preconnect()</code> and <code>prefetch()</code>) to my global <code>AG</code> object</a>. With that library code in place, I can call those methods as necessary from within my other JavaScript files. Here’s a simplified extract from <a href="https://github.com/aarongustafson/aarongustafson.github.io/blob/source/source/_javascript/post/disqus.js">my Disqus integration script</a>:</p>
  15. <div><link rel="stylesheet" href="https://assets-cdn.github.com/assets/gist/embed-e75a8866f2a8f51e8bbbf8ca746676f1e7cacb08f4b1d9b52ea62524b333cc6d.css" />
  16. <div id="gist28197221" class="gist">
  17. <div class="gist-file">
  18. <div class="gist-data">
  19. <div class="js-gist-file-update-container js-task-list-container file-box">
  20. <div id="file-resource-hints-sample-js" class="file">
  21. <div class="blob-wrapper data type-javascript">
  22. <table class="highlight tab-size js-file-line-container" data-tab-size="8">
  23. <tr>
  24. <td id="file-resource-hints-sample-js-L1" class="blob-num js-line-number" data-line-number="1"></td>
  25. <td id="file-resource-hints-sample-js-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-k">if</span> ( <span class="pl-s"><span class="pl-pds">&#39;</span>AG<span class="pl-pds">&#39;</span></span> <span class="pl-k">in</span> <span class="pl-c1">window</span> <span class="pl-k">&amp;&amp;</span> <span class="pl-s"><span class="pl-pds">&#39;</span>preconnect<span class="pl-pds">&#39;</span></span> <span class="pl-k">in</span> <span class="pl-c1">window</span>.AG ) {</td>
  26. </tr>
  27. <tr>
  28. <td id="file-resource-hints-sample-js-L2" class="blob-num js-line-number" data-line-number="2"></td>
  29. <td id="file-resource-hints-sample-js-LC2" class="blob-code blob-code-inner js-file-line"> <span class="pl-c1">window</span>.AG.preconnect( <span class="pl-s"><span class="pl-pds">&#39;</span>//disqus.com/<span class="pl-pds">&#39;</span></span> );</td>
  30. </tr>
  31. <tr>
  32. <td id="file-resource-hints-sample-js-L3" class="blob-num js-line-number" data-line-number="3"></td>
  33. <td id="file-resource-hints-sample-js-LC3" class="blob-code blob-code-inner js-file-line"> <span class="pl-c1">window</span>.AG.prefetch( <span class="pl-s"><span class="pl-pds">&#39;</span>//<span class="pl-pds">&#39;</span></span> <span class="pl-k">+</span> disqus_shortname <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#39;</span>.disqus.com/count.js<span class="pl-pds">&#39;</span></span> );</td>
  34. </tr>
  35. <tr>
  36. <td id="file-resource-hints-sample-js-L4" class="blob-num js-line-number" data-line-number="4"></td>
  37. <td id="file-resource-hints-sample-js-LC4" class="blob-code blob-code-inner js-file-line">}</td>
  38. </tr>
  39. </table>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="gist-meta">
  45. <a href="https://gist.github.com/aarongustafson/7f05709cca9293e4efea/raw/b6ca32d30643137603def7b4805d7bdcbfe63137/resource-hints-sample.js" style="float:right">view raw</a>
  46. <a href="https://gist.github.com/aarongustafson/7f05709cca9293e4efea#file-resource-hints-sample-js">resource-hints-sample.js</a>
  47. hosted with &#10084; by <a href="https://github.com">GitHub</a>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <p>While a minor addition, the speed improvement in <a href="http://caniuse.com/#search=resource%20hints">supporting browsers</a> was noticeable.<sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup></p>
  53. <h2 id="integrating-service-worker">Integrating Service Worker</h2>
  54. <p>With that in the bag, I set about making my first <a href="http://www.w3.org/TR/service-workers/">Service Worker</a>. I started off gently, using Dean’s piece as a guide. I added a WebP conversion bit to <a href="https://github.com/aarongustafson/aarongustafson.github.io/blob/source/tasks/gulp/images.js">my image processing Gulp task</a> to get the files in place and then I created the Service Worker. By default, <a href="https://gist.github.com/deanhume/c04478df744ce833925c#file-client-hints-service-worker-js">Dean’s code</a> converts <em>all</em> JPG and PNG requests to WebP responses, so I set it up to limit the requests to only those files being requested directly from my server. I have no way of knowing if WebP equivalents of every JPG and PNG exist on the open web (probably not), but I know they exist on my server. Here’s the updated code:</p>
  55. <div><link rel="stylesheet" href="https://assets-cdn.github.com/assets/gist/embed-e75a8866f2a8f51e8bbbf8ca746676f1e7cacb08f4b1d9b52ea62524b333cc6d.css" />
  56. <div id="gist28206589" class="gist">
  57. <div class="gist-file">
  58. <div class="gist-data">
  59. <div class="js-gist-file-update-container js-task-list-container file-box">
  60. <div id="file-webp-service-worker-js" class="file">
  61. <div class="blob-wrapper data type-javascript">
  62. <table class="highlight tab-size js-file-line-container" data-tab-size="8">
  63. <tr>
  64. <td id="file-webp-service-worker-js-L1" class="blob-num js-line-number" data-line-number="1"></td>
  65. <td id="file-webp-service-worker-js-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-s"><span class="pl-pds">&quot;</span>use strict<span class="pl-pds">&quot;</span></span>;</td>
  66. </tr>
  67. <tr>
  68. <td id="file-webp-service-worker-js-L2" class="blob-num js-line-number" data-line-number="2"></td>
  69. <td id="file-webp-service-worker-js-LC2" class="blob-code blob-code-inner js-file-line">
  70. </td>
  71. </tr>
  72. <tr>
  73. <td id="file-webp-service-worker-js-L3" class="blob-num js-line-number" data-line-number="3"></td>
  74. <td id="file-webp-service-worker-js-LC3" class="blob-code blob-code-inner js-file-line">self.addEventListener(<span class="pl-s"><span class="pl-pds">&#39;</span>fetch<span class="pl-pds">&#39;</span></span>, <span class="pl-k">function</span>(<span class="pl-smi">event</span>) {</td>
  75. </tr>
  76. <tr>
  77. <td id="file-webp-service-worker-js-L4" class="blob-num js-line-number" data-line-number="4"></td>
  78. <td id="file-webp-service-worker-js-LC4" class="blob-code blob-code-inner js-file-line">
  79. </td>
  80. </tr>
  81. <tr>
  82. <td id="file-webp-service-worker-js-L5" class="blob-num js-line-number" data-line-number="5"></td>
  83. <td id="file-webp-service-worker-js-LC5" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">var</span> request <span class="pl-k">=</span> <span class="pl-c1">event</span>.request,</td>
  84. </tr>
  85. <tr>
  86. <td id="file-webp-service-worker-js-L6" class="blob-num js-line-number" data-line-number="6"></td>
  87. <td id="file-webp-service-worker-js-LC6" class="blob-code blob-code-inner js-file-line"> url <span class="pl-k">=</span> request.url,</td>
  88. </tr>
  89. <tr>
  90. <td id="file-webp-service-worker-js-L7" class="blob-num js-line-number" data-line-number="7"></td>
  91. <td id="file-webp-service-worker-js-LC7" class="blob-code blob-code-inner js-file-line"> url_object <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">URL</span>( url ),</td>
  92. </tr>
  93. <tr>
  94. <td id="file-webp-service-worker-js-L8" class="blob-num js-line-number" data-line-number="8"></td>
  95. <td id="file-webp-service-worker-js-LC8" class="blob-code blob-code-inner js-file-line"> re_jpg_or_png <span class="pl-k">=</span><span class="pl-sr"> <span class="pl-pds">/</span><span class="pl-cce">\\.</span>(?:jpg<span class="pl-k">|</span>png)<span class="pl-k">$</span><span class="pl-pds">/</span></span>,</td>
  96. </tr>
  97. <tr>
  98. <td id="file-webp-service-worker-js-L9" class="blob-num js-line-number" data-line-number="9"></td>
  99. <td id="file-webp-service-worker-js-LC9" class="blob-code blob-code-inner js-file-line"> supports_webp <span class="pl-k">=</span> <span class="pl-c1">false</span>, <span class="pl-c">// pessimism</span></td>
  100. </tr>
  101. <tr>
  102. <td id="file-webp-service-worker-js-L10" class="blob-num js-line-number" data-line-number="10"></td>
  103. <td id="file-webp-service-worker-js-LC10" class="blob-code blob-code-inner js-file-line"> webp_url;</td>
  104. </tr>
  105. <tr>
  106. <td id="file-webp-service-worker-js-L11" class="blob-num js-line-number" data-line-number="11"></td>
  107. <td id="file-webp-service-worker-js-LC11" class="blob-code blob-code-inner js-file-line">
  108. </td>
  109. </tr>
  110. <tr>
  111. <td id="file-webp-service-worker-js-L12" class="blob-num js-line-number" data-line-number="12"></td>
  112. <td id="file-webp-service-worker-js-LC12" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">// Check if the image is a local jpg or png</span></td>
  113. </tr>
  114. <tr>
  115. <td id="file-webp-service-worker-js-L13" class="blob-num js-line-number" data-line-number="13"></td>
  116. <td id="file-webp-service-worker-js-LC13" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">if</span> ( re_jpg_or_png.<span class="pl-c1">test</span>( request.url ) <span class="pl-k">&amp;&amp;</span></td>
  117. </tr>
  118. <tr>
  119. <td id="file-webp-service-worker-js-L14" class="blob-num js-line-number" data-line-number="14"></td>
  120. <td id="file-webp-service-worker-js-LC14" class="blob-code blob-code-inner js-file-line"> url_object.origin <span class="pl-k">==</span> location.origin ) {</td>
  121. </tr>
  122. <tr>
  123. <td id="file-webp-service-worker-js-L15" class="blob-num js-line-number" data-line-number="15"></td>
  124. <td id="file-webp-service-worker-js-LC15" class="blob-code blob-code-inner js-file-line">
  125. </td>
  126. </tr>
  127. <tr>
  128. <td id="file-webp-service-worker-js-L16" class="blob-num js-line-number" data-line-number="16"></td>
  129. <td id="file-webp-service-worker-js-LC16" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">// console.log(&#39;WORKER: caught a request for a local PNG or JPG&#39;);</span></td>
  130. </tr>
  131. <tr>
  132. <td id="file-webp-service-worker-js-L17" class="blob-num js-line-number" data-line-number="17"></td>
  133. <td id="file-webp-service-worker-js-LC17" class="blob-code blob-code-inner js-file-line">
  134. </td>
  135. </tr>
  136. <tr>
  137. <td id="file-webp-service-worker-js-L18" class="blob-num js-line-number" data-line-number="18"></td>
  138. <td id="file-webp-service-worker-js-LC18" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">// Inspect the accept header for WebP support</span></td>
  139. </tr>
  140. <tr>
  141. <td id="file-webp-service-worker-js-L19" class="blob-num js-line-number" data-line-number="19"></td>
  142. <td id="file-webp-service-worker-js-LC19" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">if</span> ( request.<span class="pl-c1">headers</span>.has(<span class="pl-s"><span class="pl-pds">&#39;</span>accept<span class="pl-pds">&#39;</span></span>) )</td>
  143. </tr>
  144. <tr>
  145. <td id="file-webp-service-worker-js-L20" class="blob-num js-line-number" data-line-number="20"></td>
  146. <td id="file-webp-service-worker-js-LC20" class="blob-code blob-code-inner js-file-line"> {</td>
  147. </tr>
  148. <tr>
  149. <td id="file-webp-service-worker-js-L21" class="blob-num js-line-number" data-line-number="21"></td>
  150. <td id="file-webp-service-worker-js-LC21" class="blob-code blob-code-inner js-file-line"> supports_webp <span class="pl-k">=</span> request.<span class="pl-c1">headers</span>.get(<span class="pl-s"><span class="pl-pds">&#39;</span>accept<span class="pl-pds">&#39;</span></span>).includes(<span class="pl-s"><span class="pl-pds">&#39;</span>webp<span class="pl-pds">&#39;</span></span>);</td>
  151. </tr>
  152. <tr>
  153. <td id="file-webp-service-worker-js-L22" class="blob-num js-line-number" data-line-number="22"></td>
  154. <td id="file-webp-service-worker-js-LC22" class="blob-code blob-code-inner js-file-line"> }</td>
  155. </tr>
  156. <tr>
  157. <td id="file-webp-service-worker-js-L23" class="blob-num js-line-number" data-line-number="23"></td>
  158. <td id="file-webp-service-worker-js-LC23" class="blob-code blob-code-inner js-file-line">
  159. </td>
  160. </tr>
  161. <tr>
  162. <td id="file-webp-service-worker-js-L24" class="blob-num js-line-number" data-line-number="24"></td>
  163. <td id="file-webp-service-worker-js-LC24" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">// Browser supports WebP</span></td>
  164. </tr>
  165. <tr>
  166. <td id="file-webp-service-worker-js-L25" class="blob-num js-line-number" data-line-number="25"></td>
  167. <td id="file-webp-service-worker-js-LC25" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">if</span> ( supports_webp )</td>
  168. </tr>
  169. <tr>
  170. <td id="file-webp-service-worker-js-L26" class="blob-num js-line-number" data-line-number="26"></td>
  171. <td id="file-webp-service-worker-js-LC26" class="blob-code blob-code-inner js-file-line"> {</td>
  172. </tr>
  173. <tr>
  174. <td id="file-webp-service-worker-js-L27" class="blob-num js-line-number" data-line-number="27"></td>
  175. <td id="file-webp-service-worker-js-LC27" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">// Make the new URL</span></td>
  176. </tr>
  177. <tr>
  178. <td id="file-webp-service-worker-js-L28" class="blob-num js-line-number" data-line-number="28"></td>
  179. <td id="file-webp-service-worker-js-LC28" class="blob-code blob-code-inner js-file-line"> webp_url <span class="pl-k">=</span> url.<span class="pl-c1">substr</span>(<span class="pl-c1">0</span>, url.<span class="pl-c1">lastIndexOf</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>.<span class="pl-pds">&#39;</span></span>)) <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#39;</span>.webp<span class="pl-pds">&#39;</span></span>;</td>
  180. </tr>
  181. <tr>
  182. <td id="file-webp-service-worker-js-L29" class="blob-num js-line-number" data-line-number="29"></td>
  183. <td id="file-webp-service-worker-js-LC29" class="blob-code blob-code-inner js-file-line">
  184. </td>
  185. </tr>
  186. <tr>
  187. <td id="file-webp-service-worker-js-L30" class="blob-num js-line-number" data-line-number="30"></td>
  188. <td id="file-webp-service-worker-js-LC30" class="blob-code blob-code-inner js-file-line"> <span class="pl-c1">event</span>.respondWith(</td>
  189. </tr>
  190. <tr>
  191. <td id="file-webp-service-worker-js-L31" class="blob-num js-line-number" data-line-number="31"></td>
  192. <td id="file-webp-service-worker-js-LC31" class="blob-code blob-code-inner js-file-line"> fetch(</td>
  193. </tr>
  194. <tr>
  195. <td id="file-webp-service-worker-js-L32" class="blob-num js-line-number" data-line-number="32"></td>
  196. <td id="file-webp-service-worker-js-LC32" class="blob-code blob-code-inner js-file-line"> webp_url,</td>
  197. </tr>
  198. <tr>
  199. <td id="file-webp-service-worker-js-L33" class="blob-num js-line-number" data-line-number="33"></td>
  200. <td id="file-webp-service-worker-js-LC33" class="blob-code blob-code-inner js-file-line"> { mode<span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">&#39;</span>no-cors<span class="pl-pds">&#39;</span></span> }</td>
  201. </tr>
  202. <tr>
  203. <td id="file-webp-service-worker-js-L34" class="blob-num js-line-number" data-line-number="34"></td>
  204. <td id="file-webp-service-worker-js-LC34" class="blob-code blob-code-inner js-file-line"> )</td>
  205. </tr>
  206. <tr>
  207. <td id="file-webp-service-worker-js-L35" class="blob-num js-line-number" data-line-number="35"></td>
  208. <td id="file-webp-service-worker-js-LC35" class="blob-code blob-code-inner js-file-line"> );</td>
  209. </tr>
  210. <tr>
  211. <td id="file-webp-service-worker-js-L36" class="blob-num js-line-number" data-line-number="36"></td>
  212. <td id="file-webp-service-worker-js-LC36" class="blob-code blob-code-inner js-file-line"> }</td>
  213. </tr>
  214. <tr>
  215. <td id="file-webp-service-worker-js-L37" class="blob-num js-line-number" data-line-number="37"></td>
  216. <td id="file-webp-service-worker-js-LC37" class="blob-code blob-code-inner js-file-line"> }</td>
  217. </tr>
  218. <tr>
  219. <td id="file-webp-service-worker-js-L38" class="blob-num js-line-number" data-line-number="38"></td>
  220. <td id="file-webp-service-worker-js-LC38" class="blob-code blob-code-inner js-file-line">});</td>
  221. </tr>
  222. </table>
  223. </div>
  224. </div>
  225. </div>
  226. </div>
  227. <div class="gist-meta">
  228. <a href="https://gist.github.com/aarongustafson/ff6aef09a10038e1728a/raw/078af4350bca2900c2d4dc10b3827948bc401c9b/webp-service-worker.js" style="float:right">view raw</a>
  229. <a href="https://gist.github.com/aarongustafson/ff6aef09a10038e1728a#file-webp-service-worker-js">webp-service-worker.js</a>
  230. hosted with &#10084; by <a href="https://github.com">GitHub</a>
  231. </div>
  232. </div>
  233. </div>
  234. </div>
  235. <p>When I began tucking to the caching possibilities of Service Workers, following Nicolas’ and Jeremy’s posts, I <a href="https://github.com/aarongustafson/aarongustafson.github.io/blob/source/source/_javascript/serviceworker/fetch-cached.js">opted to tweak Nicholas’ caching setup a bit</a>. I’m still not completely thrilled with it, but it’s a work in progress. I’m sure I will tweak as I get more familiar with the technology.</p>
  236. <p>To keep my Service Worker code modularized (like my other JavaScript code), I opted to <a href="https://github.com/aarongustafson/aarongustafson.github.io/tree/source/source/_javascript/serviceworker">break it up into separate files</a> and am using Gulp to merge them all together and move the combined file into the root of the site. If you’d like to follow a similar path, feel free to adapt this Gulp task (which builds all of my JavaScript):</p>
  237. <div><link rel="stylesheet" href="https://assets-cdn.github.com/assets/gist/embed-e75a8866f2a8f51e8bbbf8ca746676f1e7cacb08f4b1d9b52ea62524b333cc6d.css" />
  238. <div id="gist28197221" class="gist">
  239. <div class="gist-file">
  240. <div class="gist-data">
  241. <div class="js-gist-file-update-container js-task-list-container file-box">
  242. <div id="file-gulp-scripts-js" class="file">
  243. <div class="blob-wrapper data type-javascript">
  244. <table class="highlight tab-size js-file-line-container" data-tab-size="8">
  245. <tr>
  246. <td id="file-gulp-scripts-js-L1" class="blob-num js-line-number" data-line-number="1"></td>
  247. <td id="file-gulp-scripts-js-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-k">var</span> gulp <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp<span class="pl-pds">&#39;</span></span>),</td>
  248. </tr>
  249. <tr>
  250. <td id="file-gulp-scripts-js-L2" class="blob-num js-line-number" data-line-number="2"></td>
  251. <td id="file-gulp-scripts-js-LC2" class="blob-code blob-code-inner js-file-line"> path <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>path<span class="pl-pds">&#39;</span></span>),</td>
  252. </tr>
  253. <tr>
  254. <td id="file-gulp-scripts-js-L3" class="blob-num js-line-number" data-line-number="3"></td>
  255. <td id="file-gulp-scripts-js-LC3" class="blob-code blob-code-inner js-file-line"> folder <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-folders<span class="pl-pds">&#39;</span></span>),</td>
  256. </tr>
  257. <tr>
  258. <td id="file-gulp-scripts-js-L4" class="blob-num js-line-number" data-line-number="4"></td>
  259. <td id="file-gulp-scripts-js-LC4" class="blob-code blob-code-inner js-file-line"> gulpIf <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-if<span class="pl-pds">&#39;</span></span>),</td>
  260. </tr>
  261. <tr>
  262. <td id="file-gulp-scripts-js-L5" class="blob-num js-line-number" data-line-number="5"></td>
  263. <td id="file-gulp-scripts-js-LC5" class="blob-code blob-code-inner js-file-line"> insert <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-insert<span class="pl-pds">&#39;</span></span>),</td>
  264. </tr>
  265. <tr>
  266. <td id="file-gulp-scripts-js-L6" class="blob-num js-line-number" data-line-number="6"></td>
  267. <td id="file-gulp-scripts-js-LC6" class="blob-code blob-code-inner js-file-line"> concat <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-concat<span class="pl-pds">&#39;</span></span>),</td>
  268. </tr>
  269. <tr>
  270. <td id="file-gulp-scripts-js-L7" class="blob-num js-line-number" data-line-number="7"></td>
  271. <td id="file-gulp-scripts-js-LC7" class="blob-code blob-code-inner js-file-line"> uglify <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-uglify<span class="pl-pds">&#39;</span></span>),</td>
  272. </tr>
  273. <tr>
  274. <td id="file-gulp-scripts-js-L8" class="blob-num js-line-number" data-line-number="8"></td>
  275. <td id="file-gulp-scripts-js-LC8" class="blob-code blob-code-inner js-file-line"> notify <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-notify<span class="pl-pds">&#39;</span></span>),</td>
  276. </tr>
  277. <tr>
  278. <td id="file-gulp-scripts-js-L9" class="blob-num js-line-number" data-line-number="9"></td>
  279. <td id="file-gulp-scripts-js-LC9" class="blob-code blob-code-inner js-file-line"> rename <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>gulp-rename<span class="pl-pds">&#39;</span></span>),</td>
  280. </tr>
  281. <tr>
  282. <td id="file-gulp-scripts-js-L10" class="blob-num js-line-number" data-line-number="10"></td>
  283. <td id="file-gulp-scripts-js-LC10" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">//handleErrors = require(&#39;handleErrors&#39;),</span></td>
  284. </tr>
  285. <tr>
  286. <td id="file-gulp-scripts-js-L11" class="blob-num js-line-number" data-line-number="11"></td>
  287. <td id="file-gulp-scripts-js-LC11" class="blob-code blob-code-inner js-file-line"> source_folder <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#39;</span>source/_javascript<span class="pl-pds">&#39;</span></span>,</td>
  288. </tr>
  289. <tr>
  290. <td id="file-gulp-scripts-js-L12" class="blob-num js-line-number" data-line-number="12"></td>
  291. <td id="file-gulp-scripts-js-LC12" class="blob-code blob-code-inner js-file-line"> destination_root <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#39;</span>source<span class="pl-pds">&#39;</span></span>,</td>
  292. </tr>
  293. <tr>
  294. <td id="file-gulp-scripts-js-L13" class="blob-num js-line-number" data-line-number="13"></td>
  295. <td id="file-gulp-scripts-js-LC13" class="blob-code blob-code-inner js-file-line"> destination_folder <span class="pl-k">=</span> destination_root <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#39;</span>/j<span class="pl-pds">&#39;</span></span>,</td>
  296. </tr>
  297. <tr>
  298. <td id="file-gulp-scripts-js-L14" class="blob-num js-line-number" data-line-number="14"></td>
  299. <td id="file-gulp-scripts-js-LC14" class="blob-code blob-code-inner js-file-line"> public_root <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#39;</span>public<span class="pl-pds">&#39;</span></span></td>
  300. </tr>
  301. <tr>
  302. <td id="file-gulp-scripts-js-L15" class="blob-num js-line-number" data-line-number="15"></td>
  303. <td id="file-gulp-scripts-js-LC15" class="blob-code blob-code-inner js-file-line"> public_folder <span class="pl-k">=</span> public_root <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#39;</span>/j<span class="pl-pds">&#39;</span></span>,</td>
  304. </tr>
  305. <tr>
  306. <td id="file-gulp-scripts-js-L16" class="blob-num js-line-number" data-line-number="16"></td>
  307. <td id="file-gulp-scripts-js-LC16" class="blob-code blob-code-inner js-file-line"> rename_serviceworker <span class="pl-k">=</span> rename({</td>
  308. </tr>
  309. <tr>
  310. <td id="file-gulp-scripts-js-L17" class="blob-num js-line-number" data-line-number="17"></td>
  311. <td id="file-gulp-scripts-js-LC17" class="blob-code blob-code-inner js-file-line"> dirname<span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">&quot;</span>../<span class="pl-pds">&quot;</span></span></td>
  312. </tr>
  313. <tr>
  314. <td id="file-gulp-scripts-js-L18" class="blob-num js-line-number" data-line-number="18"></td>
  315. <td id="file-gulp-scripts-js-LC18" class="blob-code blob-code-inner js-file-line"> });</td>
  316. </tr>
  317. <tr>
  318. <td id="file-gulp-scripts-js-L19" class="blob-num js-line-number" data-line-number="19"></td>
  319. <td id="file-gulp-scripts-js-LC19" class="blob-code blob-code-inner js-file-line">
  320. </td>
  321. </tr>
  322. <tr>
  323. <td id="file-gulp-scripts-js-L20" class="blob-num js-line-number" data-line-number="20"></td>
  324. <td id="file-gulp-scripts-js-LC20" class="blob-code blob-code-inner js-file-line">gulp.task(<span class="pl-s"><span class="pl-pds">&#39;</span>scripts<span class="pl-pds">&#39;</span></span>, folder(source_folder, <span class="pl-k">function</span>(<span class="pl-smi">folder</span>){</td>
  325. </tr>
  326. <tr>
  327. <td id="file-gulp-scripts-js-L21" class="blob-num js-line-number" data-line-number="21"></td>
  328. <td id="file-gulp-scripts-js-LC21" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">return</span> gulp.src(path.<span class="pl-c1">join</span>(source_folder, folder, <span class="pl-s"><span class="pl-pds">&#39;</span>*.js<span class="pl-pds">&#39;</span></span>))</td>
  329. </tr>
  330. <tr>
  331. <td id="file-gulp-scripts-js-L22" class="blob-num js-line-number" data-line-number="22"></td>
  332. <td id="file-gulp-scripts-js-LC22" class="blob-code blob-code-inner js-file-line"> .pipe(<span class="pl-c1">concat</span>(folder <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#39;</span>.js<span class="pl-pds">&#39;</span></span>))</td>
  333. </tr>
  334. <tr>
  335. <td id="file-gulp-scripts-js-L23" class="blob-num js-line-number" data-line-number="23"></td>
  336. <td id="file-gulp-scripts-js-LC23" class="blob-code blob-code-inner js-file-line"> .pipe(insert.transform(<span class="pl-k">function</span>(<span class="pl-smi">contents</span>, <span class="pl-smi">file</span>){</td>
  337. </tr>
  338. <tr>
  339. <td id="file-gulp-scripts-js-L24" class="blob-num js-line-number" data-line-number="24"></td>
  340. <td id="file-gulp-scripts-js-LC24" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">// insert a build time variable</span></td>
  341. </tr>
  342. <tr>
  343. <td id="file-gulp-scripts-js-L25" class="blob-num js-line-number" data-line-number="25"></td>
  344. <td id="file-gulp-scripts-js-LC25" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">var</span> build_time <span class="pl-k">=</span> (<span class="pl-k">new</span> <span class="pl-en">Date</span>()).<span class="pl-c1">getTime</span>() <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#39;</span><span class="pl-pds">&#39;</span></span>;</td>
  345. </tr>
  346. <tr>
  347. <td id="file-gulp-scripts-js-L26" class="blob-num js-line-number" data-line-number="26"></td>
  348. <td id="file-gulp-scripts-js-LC26" class="blob-code blob-code-inner js-file-line"> <span class="pl-k">return</span> contents.<span class="pl-c1">replace</span>( <span class="pl-s"><span class="pl-pds">&#39;</span>{{BUILD_TIME}}<span class="pl-pds">&#39;</span></span>, build_time );</td>
  349. </tr>
  350. <tr>
  351. <td id="file-gulp-scripts-js-L27" class="blob-num js-line-number" data-line-number="27"></td>
  352. <td id="file-gulp-scripts-js-LC27" class="blob-code blob-code-inner js-file-line"> }))</td>
  353. </tr>
  354. <tr>
  355. <td id="file-gulp-scripts-js-L28" class="blob-num js-line-number" data-line-number="28"></td>
  356. <td id="file-gulp-scripts-js-LC28" class="blob-code blob-code-inner js-file-line"> .pipe(gulp.dest(destination_folder))</td>
  357. </tr>
  358. <tr>
  359. <td id="file-gulp-scripts-js-L29" class="blob-num js-line-number" data-line-number="29"></td>
  360. <td id="file-gulp-scripts-js-LC29" class="blob-code blob-code-inner js-file-line"> .pipe(gulp.dest(public_folder))</td>
  361. </tr>
  362. <tr>
  363. <td id="file-gulp-scripts-js-L30" class="blob-num js-line-number" data-line-number="30"></td>
  364. <td id="file-gulp-scripts-js-LC30" class="blob-code blob-code-inner js-file-line"> .pipe(rename({suffix<span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">&#39;</span>.min<span class="pl-pds">&#39;</span></span>}))</td>
  365. </tr>
  366. <tr>
  367. <td id="file-gulp-scripts-js-L31" class="blob-num js-line-number" data-line-number="31"></td>
  368. <td id="file-gulp-scripts-js-LC31" class="blob-code blob-code-inner js-file-line"> .pipe(uglify())</td>
  369. </tr>
  370. <tr>
  371. <td id="file-gulp-scripts-js-L32" class="blob-num js-line-number" data-line-number="32"></td>
  372. <td id="file-gulp-scripts-js-LC32" class="blob-code blob-code-inner js-file-line"> .pipe(gulpIf(folder<span class="pl-k">==</span><span class="pl-s"><span class="pl-pds">&#39;</span>serviceworker<span class="pl-pds">&#39;</span></span>,rename_serviceworker))</td>
  373. </tr>
  374. <tr>
  375. <td id="file-gulp-scripts-js-L33" class="blob-num js-line-number" data-line-number="33"></td>
  376. <td id="file-gulp-scripts-js-LC33" class="blob-code blob-code-inner js-file-line"> .pipe(gulp.dest(destination_folder))</td>
  377. </tr>
  378. <tr>
  379. <td id="file-gulp-scripts-js-L34" class="blob-num js-line-number" data-line-number="34"></td>
  380. <td id="file-gulp-scripts-js-LC34" class="blob-code blob-code-inner js-file-line"> .pipe(gulp.dest(public_folder))</td>
  381. </tr>
  382. <tr>
  383. <td id="file-gulp-scripts-js-L35" class="blob-num js-line-number" data-line-number="35"></td>
  384. <td id="file-gulp-scripts-js-LC35" class="blob-code blob-code-inner js-file-line"> .pipe(notify({ message<span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">&#39;</span>Scripts task complete<span class="pl-pds">&#39;</span></span> }));</td>
  385. </tr>
  386. <tr>
  387. <td id="file-gulp-scripts-js-L36" class="blob-num js-line-number" data-line-number="36"></td>
  388. <td id="file-gulp-scripts-js-LC36" class="blob-code blob-code-inner js-file-line"> <span class="pl-c">//.on(&#39;error&#39;, handleErrors);</span></td>
  389. </tr>
  390. <tr>
  391. <td id="file-gulp-scripts-js-L37" class="blob-num js-line-number" data-line-number="37"></td>
  392. <td id="file-gulp-scripts-js-LC37" class="blob-code blob-code-inner js-file-line">}));</td>
  393. </tr>
  394. </table>
  395. </div>
  396. </div>
  397. </div>
  398. </div>
  399. <div class="gist-meta">
  400. <a href="https://gist.github.com/aarongustafson/7f05709cca9293e4efea/raw/b6ca32d30643137603def7b4805d7bdcbfe63137/gulp-scripts.js" style="float:right">view raw</a>
  401. <a href="https://gist.github.com/aarongustafson/7f05709cca9293e4efea#file-gulp-scripts-js">gulp-scripts.js</a>
  402. hosted with &#10084; by <a href="https://github.com">GitHub</a>
  403. </div>
  404. </div>
  405. </div>
  406. </div>
  407. <p>As most of the walkthroughs recommended that you version your Service Worker if you’re doing any caching, I set mine up to be auto-versioned by inserting a timestamp (lines 23-27, above) into my Service Worker header file (line 3, below):</p>
  408. <div><link rel="stylesheet" href="https://assets-cdn.github.com/assets/gist/embed-e75a8866f2a8f51e8bbbf8ca746676f1e7cacb08f4b1d9b52ea62524b333cc6d.css" />
  409. <div id="gist28197221" class="gist">
  410. <div class="gist-file">
  411. <div class="gist-data">
  412. <div class="js-gist-file-update-container js-task-list-container file-box">
  413. <div id="file-_header-js" class="file">
  414. <div class="blob-wrapper data type-javascript">
  415. <table class="highlight tab-size js-file-line-container" data-tab-size="8">
  416. <tr>
  417. <td id="file-_header-js-L1" class="blob-num js-line-number" data-line-number="1"></td>
  418. <td id="file-_header-js-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-s"><span class="pl-pds">&#39;</span>use strict<span class="pl-pds">&#39;</span></span>;</td>
  419. </tr>
  420. <tr>
  421. <td id="file-_header-js-L2" class="blob-num js-line-number" data-line-number="2"></td>
  422. <td id="file-_header-js-LC2" class="blob-code blob-code-inner js-file-line">
  423. </td>
  424. </tr>
  425. <tr>
  426. <td id="file-_header-js-L3" class="blob-num js-line-number" data-line-number="3"></td>
  427. <td id="file-_header-js-LC3" class="blob-code blob-code-inner js-file-line"><span class="pl-k">var</span> version <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#39;</span>v{{BUILD_TIME}}:<span class="pl-pds">&#39;</span></span>,</td>
  428. </tr>
  429. <tr>
  430. <td id="file-_header-js-L4" class="blob-num js-line-number" data-line-number="4"></td>
  431. <td id="file-_header-js-LC4" class="blob-code blob-code-inner js-file-line"> default_avatar <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#39;</span>https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&amp;f=y<span class="pl-pds">&#39;</span></span>,</td>
  432. </tr>
  433. <tr>
  434. <td id="file-_header-js-L5" class="blob-num js-line-number" data-line-number="5"></td>
  435. <td id="file-_header-js-LC5" class="blob-code blob-code-inner js-file-line"> missing_image <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#39;</span>https://i.imgur.com/oWLuFAa.gif<span class="pl-pds">&#39;</span></span>;</td>
  436. </tr>
  437. </table>
  438. </div>
  439. </div>
  440. </div>
  441. </div>
  442. <div class="gist-meta">
  443. <a href="https://gist.github.com/aarongustafson/7f05709cca9293e4efea/raw/b6ca32d30643137603def7b4805d7bdcbfe63137/_header.js" style="float:right">view raw</a>
  444. <a href="https://gist.github.com/aarongustafson/7f05709cca9293e4efea#file-_header-js">_header.js</a>
  445. hosted with &#10084; by <a href="https://github.com">GitHub</a>
  446. </div>
  447. </div>
  448. </div>
  449. </div>
  450. <p>Service Workers are still pretty new (and <a href="http://caniuse.com/#feat=serviceworkers">modestly supported</a>), but it’s definitely interesting to see what’s possible using them. <a href="https://adactio.com/journal/9844">Like Jeremy</a>, I want to do a bit more exploration into caching and how it may actually <em>increase</em> the monetary cost of accessing a website if not used properly. Like any powerful tool, we need to wield it wisely.</p>
  451. <figure><img src="https://media.giphy.com/media/dlmcYrvalMmAw/giphy.gif" alt="Animated GIF of a guy accidentally launching a board into his helper while power sanding." /></figure>
  452. <h2 id="making-gists-static">Making Gists Static</h2>
  453. <p>On particularly code-heavy posts (yes, like this one), I make liberal use of Gists. They’re quite useful, but <a href="https://gist.github.com/BinaryMuse/803483">the Gist plugin for Jekyll</a>, while good, still requests a script from Github in order to load the pretty printed version of the Gist. On some posts, that can mean 5 or more additional network requests, not to mention execution time for the JavaScript. It’s yet another dependency that could prohibit you from quickly getting to the content you’re looking for. Additionally, <a href="https://gds.blog.gov.uk/2013/10/21/how-many-people-are-missing-out-on-javascript-enhancement/">if JavaScript should be available, but isn’t</a>, you get nothing (since the <code>noscript</code> content is only evaluated if JavaScript support isn’t available or if a user turns it off).</p>
  454. <p>With all of this in mind, I decided to revise the plugin and make it capable of downloading the JavaScript code directly. It then extracts the HTML markup that the JavaScript would be writing into the page and just embeds it directly. It also caches the result, which is handy for speeding up the build process.</p>
  455. <p>You can grab <a href="https://gist.github.com/aarongustafson/b98add8f3580f6707cf5">my fork of the Gist Jekyll Plugin as, well, a Gist</a>. It’s also <a href="https://github.com/aarongustafson/aarongustafson.github.io/blob/source/plugins/gist_tag.rb">in the source of this site on Github</a>.</p>
  456. <h2 id="hopefully-a-little-faster">(Hopefully) A Little Faster</h2>
  457. <p>All told, these changes have gotten the render time of this site down significantly across the board.<sup id="fnref:2"><a href="#fn:2" class="footnote">2</a></sup> Even more so on browsers that support Service Workers and Resource Hints. I’ll likely continue tweaking as I go, but I wanted to share my process, code, and thoughts in case any of it might be useful to you in your own work. In the end, it’s all about creating better experiences for our users. How our sites perform is a big part of that.</p>
  458. <div class="footnotes">
  459. <ol>
  460. <li id="fn:1">
  461. <p>Sadly I forgot to run some speed tests prior to rolling out this change and I didn’t feel like rolling back the site, so I don’t have solid numbers for you. That said, it seemed to shave nearly 2 seconds off of the load time on heavy pages like the post I mentioned. <a href="#fnref:1" class="reversefootnote">&#8617;</a></p>
  462. </li>
  463. <li id="fn:2">
  464. <p>Again, I don’t have the numbers, but I am routinely seeing <code>DOMContentLoaded</code> reached between 400-600ms with Service Worker caching in play. <a href="#fnref:2" class="reversefootnote">&#8617;</a></p>
  465. </li>
  466. </ol>
  467. </div>