← Back to Frontend, page 2

frontend-0023 · Hard senior

After a deploy, some users still see the old version of a site that has a service worker. How do you debug it and fix it?

Usually an old worker is still controlling open tabs, or the worker cached the HTML first and keeps serving it. Version the cache name, delete old caches on activate, serve sw.js with no-cache, and decide deliberately whether to use skipWaiting and clients.claim.

Full answer

Several separate causes produce the same symptom, so check them one by one.

  1. The old worker is still in control. A new worker installs and then waits while any page uses the old one. Users with a long-lived tab or a pinned tab stay on the old version. Check Application, Service Workers in DevTools: a worker in the waiting state is the sign.
  2. The worker caches the HTML. With cache first on navigations, the old HTML and the old file names are served until the worker itself changes. Use network first for navigation requests, or revalidate them.
  3. The sw.js file is cached by a CDN or a proxy. Browsers check the worker file on navigation and at most about every 24 hours, and they bypass the HTTP cache for the main script by default, but an edge cache in front of the origin can still serve an old copy. Serve sw.js with Cache-Control: no-cache.
  4. The cache is never cleaned. A cache named static is reused, so old and new files mix. A versioned name such as app-v2 lets the activate handler delete every other cache.
  5. The byte content of sw.js did not change. The browser only starts an update when the file differs. A build that embeds a release id or a manifest hash in the file solves this.

Fix choices:

  • skipWaiting() in install makes the new worker activate at once, and clients.claim() in activate takes control of open pages. This gets users to the new version fast, but an open page can now run old JavaScript with new cached files. Safer: show a banner such as "A new version is ready" and call skipWaiting when the user agrees, then reload.
  • Delete old caches in activate.
  • Keep a kill switch: a worker that unregisters itself and clears the caches, ready to deploy if a worker breaks the site.

The test starts a local server that serves a sw.js with a version in it. It registers version 1, changes the server to version 2, calls registration.update() and checks that the cache list changes from app-v1 to app-v2.

javascript
// sw.js (the release id is written in by the build)
const VERSION = 'v1';
const CACHE = `app-${VERSION}`;

self.addEventListener('install', (event) => {
  self.skipWaiting();
  event.waitUntil(caches.open(CACHE).then((cache) => cache.add('/data.json')));
});

self.addEventListener('activate', (event) => {
  event.waitUntil((async () => {
    for (const key of await caches.keys()) if (key !== CACHE) await caches.delete(key);
    await self.clients.claim();
  })());
});

self.addEventListener('fetch', (event) => {
  if (event.request.mode === 'navigate') return; // let the network handle pages
  event.respondWith(caches.match(event.request).then((hit) => hit || fetch(event.request)));
});

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. A local server returned sw.js with version v1, then v2. After registration.update() the cache list changed from app-v1 to app-v2. Several open tabs, CDN caching of sw.js and an update banner were not run.

Key points

  • An old worker keeps control while its tabs are open, and a new one waits.
  • Cache-first HTML pins users to old file names.
  • Serve sw.js with no-cache and make sure its bytes change per release.
  • Version cache names and delete old ones in activate.
  • Prefer a user-approved update over a forced skipWaiting.

Common mistakes

  • Hard refreshing the page and thinking the problem is solved for every user.
  • Caching sw.js for a long time.
  • Using skipWaiting without thinking about pages that already run old code.
  • Changing the files but not the worker bytes, so no update starts.

Follow-ups

  • What is a kill switch worker?
  • How would you tell users that a new version is ready?
  • What does updateViaCache control?

Tips

  • Give a checklist in order of likelihood, then the fix and the safer UX.
  • Say how you would verify the fix on a real user's browser with DevTools.