Removing a Service Worker
Published: 2020-11-03 16:42:55

---
title: Removing a Service Worker
published: true
date: 2020-11-03 12:30:00 UTC
tags: 
canonical_url: https://kevincox.ca/2020/11/03/removing-a-service-worker/
---

<main><h2 id="background"><a href="https://kevincox.ca/2020/11/03/removing-a-service-worker/#background">Background</a></h2>

<p>I used to use a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API">Service Worker</a> on this blog to provide some minor features. This worker did a grand total of two things:</p>

<ol>
  <li>It allowed using a state page while checking the network for an updated version, or on a network error.</li>
  <li>It preloaded the homepage (but no related assets).</li>
</ol>

<p>The first is fairly useful, it provided a snappier experience for anyone revising a page, and the second I guess was a slight experience upgrade.</p>

<p>However times have moved on and the first feature can be provided by <a href="https://tools.ietf.org/html/rfc5861">stale-while-revalidate and stale-if-error</a>. The former of which is <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Browser_compatibility">now supported by browsers</a>. Since <code>stale-if-error</code> isn’t supported I am losing a minor feature but as a static site my website isn’t down very often (🤞).</p>

<p>The second feature can probably be supported via a <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/prefetch">prefetch link</a> but I’m not even sure I want to do that at this point.</p>

<h2 id="goals"><a href="https://kevincox.ca/2020/11/03/removing-a-service-worker/#goals">Goals</a></h2>

<p>I have seen a lot of solutions online, however none were clear and all had problems.</p>

<ol>
  <li>Remove the service worker at some point in the future. Since my service worker was working (mostly) correctly there was no urgent need to remove it. However I would like it to eventually go away.</li>
  <li>Avoid leaving code on the live pages. I would like to have an optimized experience for future views.</li>
  <li>Ideally clean up the caches. I only had one cache, and the documents in it were tiny but it would be nice to clean up after ourselves.</li>
</ol>

<h2 id="solution"><a href="https://kevincox.ca/2020/11/03/removing-a-service-worker/#solution">Solution</a></h2>

<ol>
  <li>Delete the code that registers the service worker.</li>
  <li>Replace the service worker script with the following file. The new code must be available from the same URL the previous service worker was at. If you had multiple service worker URLs in the past you should duplicate the code at all of them.</li>
</ol>

<div><pre><code><span>console</span><span>.</span><span>log</span><span>(</span><span>"</span><span>Cleanup Service Worker Starting</span><span>"</span><span>);</span>

<span>caches</span><span>.</span><span>keys</span><span>()</span>
	<span>.</span><span>then</span><span>(</span><span>keys</span> <span>=&gt;</span>
		<span>Promise</span><span>.</span><span>all</span><span>(</span>
			<span>keys</span><span>.</span><span>map</span><span>(</span><span>async</span> <span>key</span> <span>=&gt;</span> <span>console</span><span>.</span><span>log</span><span>(</span><span>"</span><span>caches.delete</span><span>"</span><span>,</span> <span>key</span><span>,</span> <span>await</span> <span>caches</span><span>.</span><span>delete</span><span>(</span><span>key</span><span>)))))</span>
	<span>.</span><span>then</span><span>(</span><span>async</span> <span>()</span> <span>=&gt;</span> <span>{</span>
		<span>console</span><span>.</span><span>log</span><span>(</span><span>"</span><span>registration.unregister</span><span>"</span><span>,</span> <span>await</span> <span>registration</span><span>.</span><span>unregister</span><span>());</span>
	<span>})</span>
	<span>.</span><span>then</span><span>(()</span> <span>=&gt;</span> <span>console</span><span>.</span><span>log</span><span>(</span><span>"</span><span>DONE</span><span>"</span><span>))</span>
	<span>.</span><span>catch</span><span>(</span><span>console</span><span>.</span><span>error</span><span>);</span>
</code></pre></div>
<p>This code is fairly straight forward. First it deletes all the caches, then it unregisters itself.</p>

<p>Users’ browsers will automatically check for an updated service worker <a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API#Download_install_and_activate">the next time they load your website, or the next event 24h after the last service worker check</a>. This means that existing users will run this cleanup on their next visit.</p>

<h3 id="avoided-issues"><a href="https://kevincox.ca/2020/11/03/removing-a-service-worker/#avoided-issues">Avoided Issues</a></h3>

<h4 id="live-code"><a href="https://kevincox.ca/2020/11/03/removing-a-service-worker/#live-code">Live Code</a></h4>

<p>All the other solutions I saw online replaced the registration code with code that listed all of the registrations and removed them. The major issue with this is that it means that this unregister code will run on every future page load of your site.</p>

<p>Another issue is that in theory you can also never remove this code, as someone who hasn’t visited your side in the meantime will still have their service worker active. So while running that code on every page load is a trivial amount of resources I would prefer to avoid it if possible.</p>

<h4 id="deleting-the-service-worker"><a href="https://kevincox.ca/2020/11/03/removing-a-service-worker/#deleting-the-service-worker">Deleting the Service Worker</a></h4>

<p>Simply deleting the worker file <strong>will not</strong> remove the worker. I couldn’t find the revelant section of the sepcification however <a href="https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#updates">Google’s Web Fundamentals page clearly states that a 404 will leave the current service worker active</a>. I suspect that it won’t survive forever, as that seems like a poor behaviour, but it doesn’t seem that there is consensus that it will be removed at some point.</p>

<p>So I plan to keep my <a href="https://kevincox.ca/service-worker.js">/service-worker.js</a> file around forever …or at least until I decide that I want a service worker again 😆</p>