title: Taking the web offline with service workers
url: https://mobiforge.com/design-development/taking-web-offline-service-workers
hash_url: 37423996e74c25551e0f500cdabb85a7

<p>You’re probably already familiar with the idea of offline web apps, web apps that can continue to work in the face of intermittent network connectivity. This concept has been around for a while, and various technologies have been developed along the way to achieve offline web apps, (Google) Gears, and Appcache for example, but none of these addressed the offline challenge quite as well as service workers.  </p>
<h2><a name="user-content-what-is-a-service-worker" href="#what-is-a-service-worker" aria-hidden="true"></a>What is a service worker?</h2>

<p>Service workers provide a way for webpages to run scripts in the background <em>even when the page is not open</em>. A service worker can act as a proxy to a web page, intercepting requests and controlling responses, and this makes them well suited to dealing with many exciting web features, such as offline capabilities, background syncing, and <a href="https://mobiforge.com/design-development/web-push-notifications">push notifications</a>. Features like these have traditionally given native apps an edge over web apps, but with service workers, all kinds of previously impossible things are now possible on the web. This means service workers are a very big deal!</p>


<h2><a name="user-content-what-are-offline-web-apps" href="#what-are-offline-web-apps" aria-hidden="true"></a>What are offline web apps?</h2>
<p>The term <em>offline web app</em> can mean different things to different people. A good description of what is meant by an <em>offline</em> web app is given in this article: <a href="http://www.html5rocks.com/en/tutorials/offline/whats-offline/">What’s offline and why should I care?</a> Sometimes called <em>online-offline</em>, what we’re really talking about in this article is a class of web app that can function, at least to some useful degree, with an intermittent network connection.</p>

<h2><a name="user-content-why-are-offline-web-apps-desirable" href="#why-are-offline-web-apps-desirable" aria-hidden="true"></a>Why are offline web apps desirable?</h2>
<p>Two main reason are availability and performance:</p>
<ul>
<li><strong>Availability</strong> A web app can still work without a network connection, thus making it available</li>
<li><strong>Performance</strong> Because a web app makes content offline, by caching requests and responses as we’ll see later, this means that it can be much quicker to retrieve and present content to the user, often without having to make a network request</li>
</ul>
<h2><a name="user-content-offline-web-app-use-cases" href="#offline-web-app-use-cases" aria-hidden="true"></a>Offline web app use cases</h2>
<p>Now with some idea of what an offline webapp is, we can list a few use cases:</p>
<ul>
<li>Simple case: caching to improve performance and availability</li>
<li>Collaborative document writing</li>
<li>Communcation: for example composing email offline, which sends when online again</li>
</ul>
<p>A list of interesting offline projects and use cases is given on the <a href="https://github.com/w3c-webmob/ServiceWorkersDemos">W3C Web and Mobile (WebMob) Interest Group’s GitHub page</a>.</p>
<p>However, there is no doubt that to endow a web app with offline capabilities is more complex than to build a web app without. There are cache policies to consider, issues around stale content, when to update, and how to notify users that there is newer content, all of which must be considered so that the user experience is not accidentally adversely affected. For example, updating a piece of content <em>while</em> a user is reading it might be jarring if the user was not expecting it. As <a href="http://www.html5rocks.com/en/tutorials/offline/whats-offline/">pointed out by Michael Mahemoff</a>, you should be able to justify the complexity of making your web app available offline. </p>
<p>In this article we’ll show how to add basic offline capabilities to a webapp, which sort of corresponds with the offline magazine app suggestion on the WebMob Service Workers Demo page given above, although what we develop here will be more of a proof of concept that a full blown offline solution.</p>

<h2>Why not just use the browser cache?</h2>
<p>Sure, browsers cache stuff all the time. The advantages here are in persistence and control. Browser caches are easily overwritten while an application cache is more persistent. Badly configured servers often force the client to refetch things unecessarily, for example hosting providers you don't have control over. Service workers, with a full caching API, allow the client full control, and can make smarter decisions about what and when to cache. Many of us have experienced the case where we get on a plane with a page loaded in our browser, only to find that either a browser setting or cache header forces a reload attempt when you then go to read the page, a reload that has no hope of succeeding, and your page is gone.</p>
<h2><a name="user-content-getting-started-with-service-workers" href="#getting-started-with-service-workers" aria-hidden="true"></a>Getting started with service workers</h2>
<p>Let’s get the service worker basics out of the way. </p>
<h3><a name="user-content-https" href="#https" aria-hidden="true"></a>HTTPS</h3>
<p>Firstly, HTTPS is required for service workers. Since service workers control webpages and how they respond to a client, HTTPS is needed to needed to prevent hijacking.</p>

<h3><a name="user-content-registering-a-service-worker" href="#registering-a-service-worker" aria-hidden="true"></a>Registering a service worker</h3>
<p>To register a service worker, you need to include a reference to the service worker JavaScript file in your web page. You should check for service worker support first:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>if</span> <span>(</span><span>'serviceWorker'</span> <span>in</span> navigator<span>)</span> <span>{</span>
  navigator.<span>serviceWorker</span>.<span>register</span><span>(</span><span>'/sw.js'</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
    <span>// Success</span>
  <span>}</span><span>)</span>.<span>catch</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
    <span>// Fail :(</span>
  <span>}</span><span>)</span>;
<span>}</span></pre></div>

<p>Scope refers to the pages the service worker can control. In the above, the service worker can control <em>any</em> under the root and its subdirectories.</p>
<h3><a name="user-content-lifecycle" href="#lifecycle" aria-hidden="true"></a>Lifecycle</h3>
<p>The lifecyle of a service worker is a little bit more complex than this, but the two main events we are interested in are </p>

<ol class="numbered">
<li>Installing - perform some tasks at install time to prepare your service worker</li>
<li>Activation - perform some tasks at activation time</li>
</ol>

<p>Implicit in the above is that a service worker can be installed but not active. On first page-load the service worker will be installed, but it won’t be until the next page request that it actually takes control of the page. This default behaviour can be overridden, and a service worker can take control of a page immediately by making calls to the <span class="geshifilter"><code class="geshifilter-text">skipWaiting()</code></span> and <span class="geshifilter"><code class="geshifilter-text">clients.claim()</code></span> methods.</p>
<p>Service workers can be stopped and restarted as they are needed. This means that global variables don’t exist across restarts. If you need to preserve state then you should use IndexedDB, which is available in service workers.</p>
<h3><a name="user-content-fetching-and-caching" href="#fetching-and-caching" aria-hidden="true"></a>Fetching and caching</h3>
<p>Two key components of getting service workers to build offline apps are the <em>cache</em>, and <em>fetch</em> APIs. The cache allows us to store requests and responses, while fetch allows us the intercept requests, and serve up cached responses. Using cache and fetch together enables us to concoct all kinds of interesting offline scenarios. See <a href="https://jakearchibald.com/2014/offline-cookbook/">Jake Archibald’s Offline Cookbook</a> for an excellent hands-on review of various caching policies you might use.</p>


<h3><a name="user-content-service-worker-cache-api" href="#service-worker-cache-api" aria-hidden="true"></a>Service Worker Cache API</h3>
<p>Service workers bring a new cache API to the table. Let’s have a quick look at this. With the <em>CacheStorage</em> object we can <span class="geshifilter"><code class="geshifilter-text">open</code></span> (and name), and <span class="geshifilter"><code class="geshifilter-text">delete</code></span>  individual caches. With individual caches, typical operations are to <span class="geshifilter"><code class="geshifilter-text">add</code></span> and <span class="geshifilter"><code class="geshifilter-text">remove</code></span> items. So, let’s open a cache and and store some pages.</p>

<div class="geshifilter"><pre class="geshifilter-javascript">  <span>// Add the addAll method</span>
  importScripts<span>(</span><span>'sw-cache-addall.js'</span><span>)</span>;
 
  <span>// A list of paths to cache</span>
  <span>var</span> paths = <span>[</span>
    <span>'/'</span>,
    <span>'/css/main.css'</span>,
    <span>'/js/main.js'</span>,
    <span>'/about.htm'</span>
  <span>]</span>;
 
  <span>// Open the cache (and name it)</span>
  caches.<span>open</span><span>(</span><span>'offline-v1'</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>cache<span>)</span> <span>{</span>
    <span>return</span> cache.<span>addAll</span><span>(</span>paths<span>)</span>;
  <span>}</span><span>)</span></pre></div>

<h3><a name="user-content-fetch-requests-with-service-workers" href="#fetch-requests-with-service-workers" aria-hidden="true"></a>Fetch requests with service workers</h3>
<p>By listening for <span class="geshifilter"><code class="geshifilter-text">fetch</code></span> events from web pages under its control, a service worker can intercept, manipulate and respond to requests for those pages. For instance, the service worker can try to pull content from its cache, and if that fails then go to the network for the original request:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">self.<span>addEventListener</span><span>(</span><span>'fetch'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
  event.<span>respondWith</span><span>(</span>
    caches.<span>match</span><span>(</span>event.<span>request</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>response<span>)</span> <span>{</span>
      <span>return</span> response <span>||</span> fetch<span>(</span>event.<span>request</span><span>)</span>;
    <span>}</span><span>)</span>
  <span>)</span>;
<span>}</span><span>)</span>;</pre></div>

<h3><a name="user-content-communicating-with-service-workers-postmessage" href="#communicating-with-service-workers-postmessage" aria-hidden="true"></a>Communicating with Service Workers: postMessage</h3>
<p>Service workers control pages, but they don’t have access to the DOM. However, service workers and web pages can communicate by sending messages to each other via the <span class="geshifilter"><code class="geshifilter-text">postMessage()</code></span> method. To send a message from webpage to service worker you could use:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">  navigator.<span>serviceWorker</span>.<span>controller</span>.<span>postMessage</span><span>(</span><span>{</span><span>'command'</span>: <span>'say-hello!'</span><span>}</span><span>)</span></pre></div>

<p>Note that the structure and content of the message is entirely up to you. </p>
<p>And to send a message from service worker to a page that it controls, you could use:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">  client.<span>postMessage</span><span>(</span><span>{</span><span>'message'</span>:<span>'hello!'</span><span>}</span><span>)</span>;</pre></div>

<p>You’ll need to get a reference to the client page, as a service worker might control multiple pages.</p>
<p>To receive a message in either page or service worker, we need to set up a listener for the message event. In a webpage we set it up as:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">navigator.<span>serviceWorker</span>.<span>addEventListener</span><span>(</span><span>'message'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
  <span>//do something with the message event.data</span>
<span>}</span><span>)</span></pre></div>


<p>In the service worker we set it up with:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">self.<span>addEventListener</span><span>(</span><span>'message'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
  <span>//do something with the message event.data</span>
<span>}</span><span>)</span></pre></div>

<p>And for completeness, to send a message to a service worker from a page, <em>and</em> to receive a response from the service worker, then you must also provide a <em>message channel</em> in the original message that the service worker can use to reply via. You can see an example of this in this <a href="https://googlechrome.github.io/samples/service-worker/post-message/">Service Worker two way postMessage example</a> article.</p>
<p>Additionally, since a service worker intercepts requests, it’s also possible to communicate with the service worker via HTTP request, with the service worker responding perhaps with a JSON response.</p>

<h2><a name="user-content-building-offline-web-apps-with-server-workers" href="#building-offline-web-apps-with-server-workers" aria-hidden="true"></a>Building offline web apps with server workers</h2>
<p>After that quick introduction to service workers, you should now have a rough idea what they are, and how to use them, and you’re hopefully convinced of the benefits of offline-capable web apps; now it's time to build an offline web app with service workers.</p>
<p>As a working demo, we’ll add some offline capabilities to mobiForge so that it will have <em>opt-in offline-reading</em> capabilities. It will work as follows:</p>
<ol class="numbered">
<li>User opts-in by via checkbox</li>
<li>Web page sends message to service worker</li>
<li>Service worker fetches recent content and caches it</li>
<li>For each subsequent request, service worker checks cache first, and falls back to network if content is not found. If network is unavailable and page is not available offline, a new ‘not available offline’ page is displayed.</li>
</ol>

<h2>Service worker offline reading demo</h2>
<p>The impatient reader can click through to the demo now; after opting in, kill the network on your device, and follow the link to the offline content.</p>
<p><a href="/page/service-workers-offline-opt-demo" target="_blank">Launch offline reading demo!</a></p>

<h2>Implementing offline reading with service workers</h2>
<p>First we register the service worker on our web page:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>if</span> <span>(</span><span>'serviceWorker'</span> <span>in</span> navigator<span>)</span> <span>{</span>
  navigator.<span>serviceWorker</span>.<span>register</span><span>(</span><span>'/sw.js'</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
    console.<span>log</span><span>(</span><span>'Service worker registered'</span><span>)</span>;
  <span>}</span><span>)</span>.<span>catch</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
    console.<span>log</span><span>(</span><span>'Service worker registration failed'</span><span>)</span>;
  <span>}</span><span>)</span>;
<span>}</span>
<span>else</span> <span>{</span>
  console.<span>log</span><span>(</span><span>'Service worker not supported'</span><span>)</span>;
<span>}</span></pre></div>

<p>We’ll want to cache some static resources when the service worker is installed, but we’ll come to that later; let’s stick with the web page code for now.</p>
<p>Next, we’ll build the user interface. It’s just a simple checkbox for an all-or-nothing opt-in. A better interface might allow the user to specify exactly what content he or she wanted to make available offline, by topic or category, or by author, for example.</p>

<p>Because a service worker can be terminated and restarted as needed, you can’t rely on it to store state across restarts. If you need persistent data, then you can use IndexedDB. Since we only want to store a simple boolean in this example <span class="geshifilter"><code class="geshifilter-text">opt-in</code></span> to keep things simple we’ll just use <span class="geshifilter"><code class="geshifilter-text">localStorage</code></span> to store this state, but in general if you have state information to store it would probably be better to use IndexedDB.</p>

<div class="geshifilter"><pre class="geshifilter-xml">  <span><span>&lt;div</span> <span>id</span>=<span>"go-offline"</span><span>&gt;</span></span><span><span>&lt;/div<span>&gt;</span></span></span></pre></div>

<p>And the JavaScript:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>var</span> optedIn = <span>false</span>;
window.<span>onload</span> = <span>function</span><span>(</span><span>)</span> <span>{</span>
 
<span>if</span><span>(</span><span>'serviceWorker'</span> <span>in</span> navigator<span>)</span> <span>{</span>
  document.<span>getElementById</span><span>(</span><span>'go-offline'</span><span>)</span>.<span>innerHTML</span> = <span>'&lt;h4&gt;mobiForge offline reading&lt;/h4&gt;&lt;input type="checkbox" name="go-offline" id="go-offline-status" value="false" /&gt;&lt;label for="go-offline-status"&gt;Make recent articles available for offline reading&lt;/label&gt;&lt;div id="off-line-msg"&gt;&lt;/div&gt;'</span>;
 
  checkOptedIn<span>(</span><span>)</span>;
 
  document.<span>getElementById</span><span>(</span><span>"go-offline-status"</span><span>)</span>.<span>addEventListener</span><span>(</span><span>'click'</span>, <span>function</span><span>(</span><span>)</span><span>{</span>
      console.<span>log</span><span>(</span><span>'start/stop fetching'</span><span>)</span>;
      optInOut<span>(</span><span>)</span>;
    <span>}</span><span>)</span>;
 
  <span>}</span>
  <span>else</span> document.<span>getElementById</span><span>(</span><span>'go-offline'</span><span>)</span>.<span>innerHTML</span> = <span>'ServiceWorker not supported :-('</span>;
 
<span>}</span>;</pre></div>

<p>Note that we need to check the opt-in status with <span class="geshifilter"><code class="geshifilter-text">checkOptedIn</code></span>. This just checks the value of the localStorage item with the following code, and sets the checkbox appropriately:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>function</span> checkOptedIn<span>(</span><span>)</span> <span>{</span>
  console.<span>log</span><span>(</span><span>'checking opted in...'</span><span>)</span>
  <span>if</span><span>(</span><span>!!</span>localStorage.<span>getItem</span><span>(</span><span>'offlineoptin'</span><span>)</span><span>)</span> <span>{</span>
    optedIn = <span>true</span>;
    document.<span>getElementById</span><span>(</span><span>"go-offline-status"</span><span>)</span>.<span>checked</span> = <span>true</span>;
    console.<span>log</span><span>(</span><span>'opted in'</span><span>)</span>;
    <span>return</span>;
  <span>}</span>
  <span>else</span> console.<span>log</span><span>(</span><span>'not opted in'</span><span>)</span>;
<span>}</span></pre></div>

<p>Note also that we added a click handler for the check box, which call <span class="geshifilter"><code class="geshifilter-text">optInOut</code></span>. Recall from earlier that pages and service workers can communicate by posting messages to each other. To allow a user to opt-in to offline, we’ll provide a checkbox which, when checked, will post a message to the service worker to make the most recent content available offline. We could also have a <em>Make this article available offline</em> associated with each article, in addition to, or instead of our <em>Make all recent content available offline</em> checkbox. This is left as an exercise for the reader.</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>function</span> optInOut<span>(</span><span>)</span> <span>{</span>
  <span>if</span><span>(</span><span>!</span>optedIn<span>)</span> <span>{</span>
    optedIn = <span>true</span>;
    localStorage.<span>setItem</span><span>(</span><span>'offlineoptin'</span>, <span>'1'</span><span>)</span>;
    navigator.<span>serviceWorker</span>.<span>controller</span>.<span>postMessage</span><span>(</span><span>{</span><span>'command'</span>: <span>'offline-opt-in'</span><span>}</span><span>)</span>;
  <span>}</span>
  <span>else</span> <span>{</span>
    optedIn = <span>false</span>;
    localStorage.<span>removeItem</span><span>(</span><span>'offlineoptin'</span><span>)</span>;
    navigator.<span>serviceWorker</span>.<span>controller</span>.<span>postMessage</span><span>(</span><span>{</span><span>'command'</span>: <span>'offline-opt-out'</span><span>}</span><span>)</span>;
  <span>}</span>
<span>}</span></pre></div>

<p>So that’s the interface out of the way, and communication is set up with the service worker. Let’s look at the service worker code.</p>
<h2><a name="user-content-service-worker-implementation" href="#service-worker-implementation" aria-hidden="true"></a>Service worker implementation</h2>
<p>The service worker needs to cache some basic static resources. When the user opts in, it should also cache the most recent content. It will pull this from the home page of the site which contains a stream of the latest posts. We’ll cache this page, plus each of the latest posts, and their associated assets.</p>



<p>We cache some basic static resources, such as images, scripts, and css that will be needed just to display every page. These resources are cached during the install event that was mentioned earlier; <span class="geshifilter"><code class="geshifilter-text">waitUntil</code></span> ensures that the pages are cached before installation is completed:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">  <span>// Add the missing addAll functionality</span>
  importScripts<span>(</span><span>'sw-cache-addall.js'</span><span>)</span>;
 
  <span>// A list of paths to cache</span>
  <span>var</span> paths = <span>[</span>
    <span>'/'</span>,
    <span>'/css/main.css'</span>,
    <span>'/js/main.js'</span>,
    <span>'/img/logo.png'</span>,
    <span>'/about.htm'</span>
  <span>]</span>;
 
  self.<span>addEventListener</span><span>(</span><span>'install'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
    event.<span>waitUntil</span><span>(</span>
      caches.<span>open</span><span>(</span><span>'offline-v1'</span><span>)</span>
        .<span>then</span><span>(</span><span>function</span><span>(</span>cache<span>)</span> <span>{</span>
          <span>return</span> cache.<span>addAll</span><span>(</span>paths<span>)</span>;
        <span>}</span><span>)</span>
    <span>)</span>;
    event.<span>waitUntil</span><span>(</span>self.<span>skipWaiting</span><span>(</span><span>)</span><span>)</span>;
  <span>}</span><span>)</span>;</pre></div>

<p>Next, we’ll listen out for messages from the web page, using the <span class="geshifilter"><code class="geshifilter-text">message</code></span> event. We saw how to do this earlier:</p>

<div class="geshifilter"><pre class="geshifilter-javascript">self.<span>addEventListener</span><span>(</span><span>'message'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
  <span>//do something with the message event.data</span>
<span>}</span><span>)</span></pre></div>

<p>To make the recent content available offline, we’ll fetch and cache the home page, as well as each of the recent articles listed on that page. We’ll scrape the HTML to get these links. This is a bit ugly; ideally, instead of scraping, it would be better to have an API that we could simply query for content.</p>
<p>We can pull out the links we need with a regular expression. Because we’re in control of the site, we don't need to worry that it will be brittle.</p>

<p>For each URL we cache, we’ll want to cache its image assets, or it will look broken when served from cache, so we must take a look at the response, and also add any images included in the content to the cache. We perform this in a function <span class="geshifilter"><code class="geshifilter-text">fetchAndCache</code></span>:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>function</span> fetchAndCache<span>(</span>url, cache<span>)</span> <span>{</span>
    <span>return</span> fetch<span>(</span>url<span>)</span>.<span>then</span><span>(</span><span>function</span> <span>(</span>response<span>)</span> <span>{</span>
        <span>if</span> <span>(</span>response.<span>status</span> <span>&lt;</span> <span>400</span><span>)</span> <span>{</span>
            console.<span>log</span><span>(</span><span>'got '</span>+url<span>)</span>;
            cache.<span>put</span><span>(</span>url, response.<span>clone</span><span>(</span><span>)</span><span>)</span>;
        <span>}</span>
        <span>return</span> response.<span>text</span><span>(</span><span>)</span>;
    <span>}</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>text<span>)</span> <span>{</span>
      <span>var</span> pattern = <span>/</span>img src=<span>(</span>?:\<span>'|<span>\"</span>)<span>\/</span>((?:files|img)<span>\/</span>[^<span>\'</span><span>\"</span>]+)<span>\"</span>/g;
      var assets = getMatches(text, pattern, 1);
      return cache.addAll(assets);
    })
}</span></pre></div>

<p>This code is outlined:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"> self.<span>addEventListener</span><span>(</span><span>'message'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
    console.<span>log</span><span>(</span><span>'SW got message:'</span>+event.<span>data</span>.<span>command</span><span>)</span>;
    <span>switch</span> <span>(</span>event.<span>data</span>.<span>command</span><span>)</span> <span>{</span>
      <span>case</span> <span>'offline-opt-in'</span>:
 
        <span>// Cache homepage, and parse the top links from it</span>
        caches.<span>open</span><span>(</span><span>'static-v1'</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>cache<span>)</span> <span>{</span>
          fetch<span>(</span><span>'/'</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>response<span>)</span> <span>{</span>
              fetchAndCache<span>(</span><span>'/'</span>, cache<span>)</span>;
              <span>return</span> response.<span>text</span><span>(</span><span>)</span>;
            <span>}</span><span>)</span>
            .<span>then</span><span>(</span><span>function</span><span>(</span>text<span>)</span> <span>{</span>
              <span>var</span> pattern = <span>/</span>a href=\<span>"([^<span>\'</span><span>\"</span>]+)<span>\"</span> class=<span>\"</span>item-title<span>\"</span>/g;
              var urls = getMatches(text, pattern, 1);
              console.log('caching: ' + urls);
 
              for(var i=0;i&lt;urls.length;i++) {
                console.log('fetching '+urls[i]);
                fetchAndCache(urls[i], cache);
              }
 
            })
        });
        break;
    }
});</span></pre></div>

<p>Another helper function is used help pull out page and image URLs:</p>

<div class="geshifilter"><pre class="geshifilter-javascript"><span>//helper</span>
<span>function</span> getMatches<span>(</span>string, regex, index<span>)</span> <span>{</span>
  index <span>||</span> <span>(</span>index = <span>1</span><span>)</span>; <span>// default to the first capturing group</span>
  <span>var</span> matches = <span>[</span><span>]</span>;
  <span>var</span> match;
  <span>while</span> <span>(</span>match = regex.<span>exec</span><span>(</span>string<span>)</span><span>)</span> <span>{</span>
    matches.<span>push</span><span>(</span>match<span>[</span>index<span>]</span><span>)</span>;
  <span>}</span>
  <span>return</span> matches;
<span>}</span></pre></div>





<p>Now it’s time to start intercepting requests. We saw how to do this earlier, by listening out for the <span class="geshifilter"><code class="geshifilter-text">fetch</code></span> event. We need to think a bit about our caching policy at this point. For this example, we’ll keep it super simple: for the home page, we’ll implement <em>network, then cache, then fallback</em>. For all other pages, we’ll implement <em>cache, then network, then fallback</em>. The reasoning is: the home page is likely to change more often than article content. So, we don’t want the user to miss any new content when there is an network connection available. For the articles, we go straight to cache. A better solution would be to implement <em>cache, then network</em> and notify the user if there is newer content. But we’ll keep it simple here—this is more a proof of concept than a full-fledged offline solution.</p>

<div class="geshifilter"><pre class="geshifilter-javascript">self.<span>addEventListener</span><span>(</span><span>'fetch'</span>, <span>function</span><span>(</span>event<span>)</span> <span>{</span>
  <span>var</span> requestURL = <span>new</span> URL<span>(</span>event.<span>request</span>.<span>url</span><span>)</span>;
 
  <span>// Network, then cache, then fallback for home page</span>
  <span>if</span><span>(</span>requestURL==<span>'/'</span><span>)</span>  <span>{</span>
    event.<span>respondWith</span><span>(</span>
      fetch<span>(</span>event.<span>request</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
        <span>return</span> caches.<span>match</span><span>(</span>event.<span>request</span><span>)</span>;
      <span>}</span><span>)</span>.<span>catch</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
        <span>return</span> caches.<span>match</span><span>(</span><span>'/page/content-not-available-offline'</span><span>)</span>;
      <span>}</span><span>)</span>
    <span>)</span>;
  <span>}</span>
 
  <span>// Cache, then network, then fallback for other urls</span>
  event.<span>respondWith</span><span>(</span>
    caches.<span>match</span><span>(</span>event.<span>request</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>response<span>)</span> <span>{</span>
      <span>return</span> response <span>||</span> fetch<span>(</span>event.<span>request</span><span>)</span>;
    <span>}</span><span>)</span>.<span>catch</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>{</span>
      <span>return</span> caches.<span>match</span><span>(</span><span>'/page/content-not-available-offline'</span><span>)</span>;
    <span>}</span><span>)</span>
  <span>)</span>;
<span>}</span><span>)</span>;</pre></div>


<p>And that about wraps it up. Note that this implementation is not perfect; it’s a partial offline solution retro-fitted onto an existing website. It required a little ugly scraping of links from the homepage. If we were building this from the ground up, it would be worth considering to implement a simple content API that we could query for content without HTML page template, and which we could query for new or updated content. We could also do a better job of ensuring necessary scripts and other assets are available offline, and we could  also consider hiding functionality that is not available offline, such as the social sharing buttons. But despite these shortcomings, it still serves as a relatively accessible proof of concept of the power of service workers and in this case, their ability to build compelling offline experiences.</p>

<h2><a name="user-content-caching-patterns" href="#caching-patterns" aria-hidden="true"></a>Caching patterns</h2>
<p>Many applications may have unique caching requirements. Should you go to cache first, network first, or both at the same time? We chose a fairly simple approach for this example.Jake Archibald lists several useful patterns in his <a href="https://jakearchibald.com/2014/offline-cookbook/">offline cookbook</a> article. Every application will have its own needs, so choose, or develop, a caching solution that’s appropriate for your application.</p>
<h2><a name="user-content-debugging" href="#debugging" aria-hidden="true"></a>Debugging</h2>
<p>Two all important URLs for debugging service workers are</p>
<ul>
<li>chrome://inspect/#service-workers</li>
<li>chrome://serviceworker-internals/</li>
</ul>
<p>You can also view what caches have been opened, and what URLs are currently cached in them.
</p>
<p class="image-caption"><img src="https://mobiforge.com/files/service-worker-cache.jpg"/><br/>Remote debugging device, showing cache contents</p>
<p>You can also view registered service workers right from the Chrome Developer Tools. However, sometimes the service worker information pane disappears from Chrome developer tools altogether—be prepared to shut down Chrome and restart to bring it back!</p>



<h2><a name="user-content-updating-service-workers" href="#updating-service-workers" aria-hidden="true"></a>Updating service workers</h2>
<p>When you’re developing, you might run into trouble with the page loading the old service worker code. The trick here is that a reload won’t necessarily load the new service worker. Instead, close the current tab altogether, and then load the page again.</p>

<p>It can also be useful to unregister the registered service worker in the Developer Tools before reloading. And remember, unless you're using <span class="geshifilter"><code class="geshifilter-text">skipWaiting()</code></span>/<span class="geshifilter"><code class="geshifilter-text">clients.claim()</code></span> your sevice worker won't take control until the next time the page is loaded.</p>
<p>Read more about updating service workers here: <a href="https://jakearchibald.com/2014/service-worker-first-draft/#updating-service-workers">Updating service workers</a>.</p>

<h2>Browser support and usage in the wild</h2>
<p>While service workers aren't universally supported yet, most of the main browsers are either working on bringing support, or have it on a roadmap. The notable exception is Safari.</p>

<p>Jake Archibald's <a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker Ready?</a> page gives a very detailed run down of the level of support in the main browsers. A quick summary would be that right now there are usable implementations in Chrome, Firefox, and Opera; Microsoft's Edge browser is likely to build support soon, but there are no signs right now that Apple will bring service worker support to Safari any time soon.</p>

<p class="image-caption"><img src="https://mobiforge.com/files/service-worker-support.png"/><br/>Service Worker browser support (adapted from<a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker Ready?</a>)</p>

<p>Despite only partial support, service workers are popping up in websites everywhere. Just take a look at <a href="chrome://serviceworker-internals/">chrome://serviceworker-internals/</a> and you might be surprised to see the number of service workers listed from various sites that you've recently visited. With well planned usage, a site can function without service workers where it's not supported, but will benefit from all that service workers have to offer where it is!</p>


<h2><a name="user-content-links-and-credits" href="#links-and-credits" aria-hidden="true"></a>Links and credits</h2>


<ul>
<li><a href="http://www.w3.org/TR/service-workers/">http://www.w3.org/TR/service-workers/</a></li>
<li><a href="https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md">https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md</a></li>
<li><a href="https://jakearchibald.com/2014/service-worker-first-draft/">https://jakearchibald.com/2014/service-worker-first-draft/</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers">https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers</a></li>
<li><a href="https://jakearchibald.com/2014/offline-cookbook/">https://jakearchibald.com/2014/offline-cookbook/</a></li>
</ul>
<p>Once again, documentation by Matt Gaunt and Jake Archibald at Google has been indispensable!</p>
