A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 17KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. title: An Offline Experience with Service Workers
  2. url: https://brandonrozek.com/2015/11/service-workers/
  3. hash_url: 8bd7ed4d765d27a4455c9979db78f203
  4. <p>I’m excited to say that I’ve written my first service worker for brandonrozek.com. What is a service worker? A service worker provides an extra layer between the client and the server. The exciting part about this is that you can use service workers to deliver an offline experience. (Cached versions of your site, offline pages, etc.)</p>
  5. <p>Service workers are currently supported in Chrome, Opera, and Firefox nightly. You don’t have to worry too much about browser support because the Service Worker spec was written in a <a href="http://alistapart.com/article/understandingprogressiveenhancement">progressively enchanced</a> way meaning it won’t break your existing site :)</p>
  6. <h2 id="caveats"><a href="#caveats" name="caveats"></a>Caveats</h2>
  7. <p>You need HTTPS to be able to use service workers on your site. This is mainly for security reasons. Imagine if a third party can control all of the networking requests on your site?</p>
  8. <p>If you don’t want to go out and buy a SSL Certificate, there are a couple free ways to go about this.<br/>
  9. 1) <a href="https://www.cloudflare.com/">Cloudflare</a><br/>
  10. 2) <a href="https://letsencrypt.org/">Let’s Encrypt</a></p>
  11. <p>Service workers are promise heavy. Promises contain a then clause which runs code asynchronously. If you’re not accustomed to this idea please check out this <a href="https://ponyfoo.com/articles/es6-promises-in-depth">post</a> by Nicolas Bevacqua.</p>
  12. <p>Now onto making the service worker! If you want to skip to the final code scroll down to the bottom. Unless you don’t like my syntax highlighting, then you can check out this <a href="https://gist.github.com/brandonrozek/0cf038df40a913fda655">gist</a>.</p>
  13. <h2 id="register-the-service-worker"><a href="#register-the-service-worker" name="register-the-service-worker"></a>Register the service worker</h2>
  14. <p>Place <code>service-worker.js</code> on the root of your site. This is so the service worker can access all the files in the site.</p>
  15. <p>Then in your main javascript file, register the service worker.</p>
  16. <pre><code class='language-javascript' rel='Script.js'>
  17. if (navigator.serviceWorker) {
  18. navigator.serviceWorker.register('/serviceworker.js', {
  19. scope: '/'
  20. });
  21. }
  22. </code></pre>
  23. <h2 id="install-the-service-worker"><a href="#install-the-service-worker" name="install-the-service-worker"></a>Install the service worker</h2>
  24. <p>The first time the service worker runs, it emits the <code>install</code> event. At this time, we can load the visitor’s cache with some resources for when they’re offline.</p>
  25. <p>Every so often, I like to change up the theme of the site. So I have version numbers attached to my files. I would also like to invalidate my cache with this version number. So at the top of the file I added</p>
  26. <pre><code rel='service-worker.js' class='language-javascript'>
  27. var version = 'v2.0.24:';
  28. </code></pre>
  29. <p>Now, to specify which files I want the service worker to cache for offline use. I thought my home page and my offline page would be good enough.</p>
  30. <pre><code rel='service-worker.js' class='language-javascript'>
  31. var offlineFundamentals = [
  32. '/',
  33. '/offline/'
  34. ];
  35. </code></pre>
  36. <p>Since <code class='language-javascript'>cache.addAll()</code> hasn’t been implemented yet in any of the browsers, and the polyfill implementation didn’t work for my needs. I pieced together my own.</p>
  37. <pre><code rel='service-worker.js' class='language-javascript'>
  38. var updateStaticCache = function() {
  39. return caches.open(version + 'fundamentals').then(function(cache) {
  40. return Promise.all(offlineFundamentals.map(function(value) {
  41. var request = new Request(value);
  42. var url = new URL(request.url);
  43. if (url.origin != location.origin) {
  44. request = new Request(value, {mode: 'no-cors'});
  45. }
  46. return fetch(request).then(function(response) {
  47. var cachedCopy = response.clone();
  48. return cache.put(request, cachedCopy);
  49. });
  50. }))
  51. })
  52. };
  53. </code></pre>
  54. <p>Let’s go through this chunk of code.</p>
  55. <ol>
  56. <li>Open the cache called <code class='language-javascript'>'v2.0.24:fundamentals'</code></li>
  57. <li>Go through all of the <code class='language-javascript'>offlineFundamental</code>‘s URLs</li>
  58. <ul>
  59. <li>Does the file I ask for come from the same domain as my site?
  60. <li>No. Then, make the request ‘no-cors’ (I had difficulty getting my asset files in cors mode. If the cors headers are included in the response, then you can take out this line)</li>
  61. <li>Fetch the file from the network and then cache it.</li>
  62. </ul>
  63. </ol>
  64. <p>Now we call it when the <code>install</code> event is fired.</p>
  65. <pre><code rel='service-worker.js' class='language-javascript'>
  66. self.addEventListener("install", function(event) {
  67. event.waitUntil(updateStaticCache())
  68. })
  69. </code></pre>
  70. <p>With this we now cached all the files in the offlineFundamentals array during the install step.</p>
  71. <h2 id="clear-out-the-old-cache"><a href="#clear-out-the-old-cache" name="clear-out-the-old-cache"></a>Clear out the old cache</h2>
  72. <p>Since we’re caching everything. If you change one of the files, your visitor wouldn’t get the changed file. Wouldn’t it be nice to remove old files from the visitor’s cache?</p>
  73. <p>Every time the service worker finishes the install step, it releases an <code>activate</code> event. We can use this to look and see if there are any old cache containers on the visitor’s computer.</p>
  74. <p>From <a href="https://ponyfoo.com/articles/serviceworker-revolution">Nicolas’ code</a>. Thanks for sharing :)</p>
  75. <pre><code rel='service-worker.js' class='language-javascript'>
  76. var clearOldCaches = function() {
  77. return caches.keys().then(function(keys) {
  78. return Promise.all(
  79. keys
  80. .filter(function (key) {
  81. return key.indexOf(version) != 0;
  82. })
  83. .map(function (key) {
  84. return caches.delete(key);
  85. })
  86. );
  87. })
  88. }
  89. </code></pre>
  90. <ol>
  91. <li>Check the names of each of the cache containers</li>
  92. <li>If they don’t start with the correct version number
  93. <ul>
  94. <li>Delete that cache container</li>
  95. </ul>
  96. </ol>
  97. <p>Call the function when the <code>activate</code> event fires.</p>
  98. <pre><code rel='service-worker.js' class='language-javascript'>
  99. self.addEventListener("activate", function(event) {
  100. event.waitUntil(clearOldCaches())
  101. });
  102. </code></pre>
  103. <h2 id="intercepting-fetch-requests"><a href="#intercepting-fetch-requests" name="intercepting-fetch-requests"></a>Intercepting fetch requests</h2>
  104. <p>The cool thing about service worker’s is that it can handle file requests. We could cache all files requested for offline use, and if a fetch for a resource failed, then the service worker can look for it in the cache or provide an alternative.</p>
  105. <p>This is a large section, so I’m going to attempt to break it down as much as I can.</p>
  106. <h3 id="limit-the-cache"><a href="#limit-the-cache" name="limit-the-cache"></a>Limit the cache</h3>
  107. <p>If the visitor started browsing all of the pages on my site, his or her cache would start to get bloated with files. To not burden my visitors, I decided to only keep the latest 25 pages and latest 10 images in the cache.</p>
  108. <pre><code rel='service-worker.js' class='language-javascript'>
  109. var limitCache = function(cache, maxItems) {
  110. cache.keys().then(function(items) {
  111. if (items.length &gt; maxItems) {
  112. cache.delete(items[0]);
  113. }
  114. })
  115. }
  116. </code></pre>
  117. <p>We’ll call it later in the code.</p>
  118. <h3 id="fetch-from-network-and-cache"><a href="#fetch-from-network-and-cache" name="fetch-from-network-and-cache"></a>Fetch from network and cache</h3>
  119. <p>Every time I fetch a file from the network I throw it into a specific cache container. <code class='language-javascript'>'pages'</code> for HTML files, <code class='language-javascript'>'images'</code> for CSS files, and <code class='language-javascript'>'assets'</code> for any other file. This is so I can handle the cache limiting above easier.</p>
  120. <p>Defined within the <code>fetch</code> event.</p>
  121. <pre><code rel='service-worker.js' class='language-javascript'>
  122. var fetchFromNetwork = function(response) {
  123. var cacheCopy = response.clone();
  124. if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
  125. caches.open(version + 'pages').then(function(cache) {
  126. cache.put(event.request, cacheCopy).then(function() {
  127. limitCache(cache, 25);
  128. })
  129. });
  130. } else if (event.request.headers.get('Accept').indexOf('image') != -1) {
  131. caches.open(version + 'images').then(function(cache) {
  132. cache.put(event.request, cacheCopy).then(function() {
  133. limitCache(cache, 10);
  134. });
  135. });
  136. } else {
  137. caches.open(version + 'assets').then(function add(cache) {
  138. cache.put(event.request, cacheCopy);
  139. });
  140. }
  141. return response;
  142. }
  143. </code></pre>
  144. <h3 id="when-the-network-fails"><a href="#when-the-network-fails" name="when-the-network-fails"></a>When the network fails</h3>
  145. <p>There are going to be times where the visitor cannot access the website. Maybe they went in a tunnel while they were riding a train? Or maybe your site went down.</p>
  146. <p>I thought it would be nice for my reader’s to be able to look over my blog posts again regardless of an internet connection. So I provide a fall-back.</p>
  147. <p>Defined within the <code>fetch</code> event.</p>
  148. <pre><code rel='service-worker.js' class='language-javascript'>
  149. var fallback = function() {
  150. if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
  151. return caches.match(event.request).then(function (response) {
  152. return response || caches.match('/offline/');
  153. })
  154. } else if (event.request.headers.get('Accept').indexOf('image') != -1) {
  155. return new Response('&lt;svg width="400" height="300" role="img" aria-labelledby="offline-title" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"&gt;&lt;title id="offline-title"&gt;Offline&lt;/title&gt;&lt;g fill="none" fill-rule="evenodd"&gt;&lt;path fill="#D8D8D8" d="M0 0h400v300H0z"/&gt;&lt;text fill="#9B9B9B" font-family="Helvetica Neue,Arial,Helvetica,sans-serif" font-size="72" font-weight="bold"&gt;&lt;tspan x="93" y="172"&gt;offline&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/svg&gt;', { headers: { 'Content-Type': 'image/svg+xml' }});
  156. }
  157. }
  158. </code></pre>
  159. <ol>
  160. <li>Is the request for a HTML file?
  161. <ul>
  162. <li>Show the <a href="https://brandonrozek.com/offline/">offline</a> page.</li>
  163. </ul>
  164. <li>Is the request for an image?
  165. <ul>
  166. <li>Show a place-holder image (Courtesy of <a href="https://adactio.com/journal/9775">Jeremy Keith</a>)</li>
  167. </ul>
  168. </ol>
  169. <h3 id="handle-the-request"><a href="#handle-the-request" name="handle-the-request"></a>Handle the request</h3>
  170. <p>First off, I’m only handling GET requests.</p>
  171. <pre><code rel='service-worker.js' class='language-javascript'>
  172. if (event.request.method != 'GET') {
  173. return;
  174. }
  175. </code></pre>
  176. <p>For HTML files, grab the file from the network. If that fails, then look for it in the cache.<br/>
  177. <em>Network then cache strategy<br/>
  178. </em></p>
  179. <pre><code rel='service-worker.js' class='language-javascript'>
  180. if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
  181. event.respondWith(fetch(event.request).then(fetchFromNetwork, fallback));
  182. return;
  183. }
  184. </code></pre>
  185. <p>For non-HTML files, follow this series of steps</p>
  186. <ol>
  187. <li>Check the cache</li>
  188. <li>Does a cache exist for this file?</li>
  189. <ul>
  190. <li>Yes. Then show it</li>
  191. <li>No. Then grab it from the network and cache it.</li>
  192. </ul>
  193. </ol>
  194. <p><em>Cache then network strategy<br/>
  195. </em></p>
  196. <pre><code rel='service-worker.js' class='language-javascript'>
  197. event.respondWith(
  198. caches.match(event.request).then(function(cached) {
  199. return cached || fetch(event.request).then(fetchFromNetwork, fallback);
  200. })
  201. )
  202. </code></pre>
  203. <p>For different stategy’s, take a look at Jake Archibald’s <a href="https://jakearchibald.com/2014/offline-cookbook/">offline cookbook</a>.</p>
  204. <h2 id="conclusion"><a href="#conclusion" name="conclusion"></a>Conclusion</h2>
  205. <p>With all of that, we now have a fully functioning offline-capable website! I wouldn’t be able to implement this myself if it wasn’t for some of the awesome people I mentioned earlier sharing their experience. So share, share, share!</p>
  206. <p>With that sentiment, I’ll now share the full code for <code>service-worker.js</code></p>
  207. <pre><code rel='service-worker.js' class='language-javascript'>
  208. var version = 'v2.0.24:';
  209. var offlineFundamentals = [
  210. '/',
  211. '/offline/'
  212. ];
  213. //Add core website files to cache during serviceworker installation
  214. var updateStaticCache = function() {
  215. return caches.open(version + 'fundamentals').then(function(cache) {
  216. return Promise.all(offlineFundamentals.map(function(value) {
  217. var request = new Request(value);
  218. var url = new URL(request.url);
  219. if (url.origin != location.origin) {
  220. request = new Request(value, {mode: 'no-cors'});
  221. }
  222. return fetch(request).then(function(response) {
  223. var cachedCopy = response.clone();
  224. return cache.put(request, cachedCopy);
  225. });
  226. }))
  227. })
  228. };
  229. //Clear caches with a different version number
  230. var clearOldCaches = function() {
  231. return caches.keys().then(function(keys) {
  232. return Promise.all(
  233. keys
  234. .filter(function (key) {
  235. return key.indexOf(version) != 0;
  236. })
  237. .map(function (key) {
  238. return caches.delete(key);
  239. })
  240. );
  241. })
  242. }
  243. /*
  244. limits the cache
  245. If cache has more than maxItems then it removes the first item in the cache
  246. */
  247. var limitCache = function(cache, maxItems) {
  248. cache.keys().then(function(items) {
  249. if (items.length &gt; maxItems) {
  250. cache.delete(items[0]);
  251. }
  252. })
  253. }
  254. //When the service worker is first added to a computer
  255. self.addEventListener("install", function(event) {
  256. event.waitUntil(updateStaticCache())
  257. })
  258. //Service worker handles networking
  259. self.addEventListener("fetch", function(event) {
  260. //Fetch from network and cache
  261. var fetchFromNetwork = function(response) {
  262. var cacheCopy = response.clone();
  263. if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
  264. caches.open(version + 'pages').then(function(cache) {
  265. cache.put(event.request, cacheCopy).then(function() {
  266. limitCache(cache, 25);
  267. })
  268. });
  269. } else if (event.request.headers.get('Accept').indexOf('image') != -1) {
  270. caches.open(version + 'images').then(function(cache) {
  271. cache.put(event.request, cacheCopy).then(function() {
  272. limitCache(cache, 10);
  273. });
  274. });
  275. } else {
  276. caches.open(version + 'assets').then(function add(cache) {
  277. cache.put(event.request, cacheCopy);
  278. });
  279. }
  280. return response;
  281. }
  282. //Fetch from network failed
  283. var fallback = function() {
  284. if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
  285. return caches.match(event.request).then(function (response) {
  286. return response || caches.match('/offline/');
  287. })
  288. } else if (event.request.headers.get('Accept').indexOf('image') != -1) {
  289. return new Response('&lt;svg width="400" height="300" role="img" aria-labelledby="offline-title" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"&gt;&lt;title id="offline-title"&gt;Offline&lt;/title&gt;&lt;g fill="none" fill-rule="evenodd"&gt;&lt;path fill="#D8D8D8" d="M0 0h400v300H0z"/&gt;&lt;text fill="#9B9B9B" font-family="Helvetica Neue,Arial,Helvetica,sans-serif" font-size="72" font-weight="bold"&gt;&lt;tspan x="93" y="172"&gt;offline&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/svg&gt;', { headers: { 'Content-Type': 'image/svg+xml' }});
  290. }
  291. }
  292. //This service worker won't touch non-get requests
  293. if (event.request.method != 'GET') {
  294. return;
  295. }
  296. //For HTML requests, look for file in network, then cache if network fails.
  297. if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
  298. event.respondWith(fetch(event.request).then(fetchFromNetwork, fallback));
  299. return;
  300. }
  301. //For non-HTML requests, look for file in cache, then network if no cache exists.
  302. event.respondWith(
  303. caches.match(event.request).then(function(cached) {
  304. return cached || fetch(event.request).then(fetchFromNetwork, fallback);
  305. })
  306. )
  307. });
  308. //After the install event
  309. self.addEventListener("activate", function(event) {
  310. event.waitUntil(clearOldCaches())
  311. });
  312. </code></pre>