You ship a fix, and a user tells you they still have the bug. You check the deploy and the new code is on the server, so the usual explanation is “the service worker didn’t update.”
That phrase actually covers four different situations, and each one has its own fix. A lot of people reach for self.skipWaiting() plus clients.claim() as the cure for all four. It doesn’t cure them, and it can make one of them worse.
The four states
| State | What’s happening | Where to look |
|---|---|---|
| 1. Not fetched | The browser never saw a new sw.js, or saw one that was byte-identical |
Network tab, sw.js response headers |
| 2. Waiting | New worker installed, but an open page still uses the old one | DevTools > Application > Service Workers, registration.waiting
|
| 3. Active, stale cache | New worker is in control, but the cache still hands out an old HTML shell or old assets | Cache Storage, fetch handler strategy |
| 4. Updated, data incompatible | Code is new, but the data on the device has an old shape | IndexedDB version, migration code |
1. No newer script fetched
The browser checks for an update when the user navigates to a page in scope, and when you call registration.update(). It compares the new script with the installed one byte for byte. If nothing changed, no install happens.
This is how people get caught: they change app.js and leave sw.js alone. If the service worker precaches files by a fixed list, or by a manifest that lives outside sw.js, the worker script stays identical and the browser has no reason to install anything. Build tools that inject a revision hash into the precache list inside sw.js avoid this, because any asset change also changes the worker.
Serve sw.js with Cache-Control: no-cache and make sure your CDN isn’t holding onto it. By default the update check skips the browser’s HTTP cache for the main worker script, but that does nothing about a CDN edge serving the old file.
2. New worker waiting behind an open page
A new worker installs and then waits. It won’t activate while any client (tab, window, installed PWA window) is still controlled by the old worker.
Refreshing a single tab usually doesn’t help. During a reload the old page stays alive until the new response arrives, so the old worker never loses its last client. The new worker only takes over once every controlled tab is closed. For an installed PWA that people leave open for days, “waiting” can last a long time.
3. Active, but the cache serves an old shell
The new worker is running, but the fetch handler serves index.html cache-first out of a cache the old worker filled. That HTML points to the old hashed bundles. The page loads fine and is still the old app.
The opposite failure is just as common: the activate step deletes old caches, the server has already dropped the old hashed files, and a page still on the old HTML tries to lazy-load a chunk that no longer exists anywhere.
4. App updated, data incompatible
The code is current, but IndexedDB or localStorage still holds data written by the previous version. If the new code assumes a new field or a new object store, it breaks even though every file is fresh. This one is not a caching problem at all, and clearing caches won’t fix it.
Why skipWaiting + clients.claim is risky
This snippet is everywhere:
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim()));
It gets rid of state 2 by force. The new worker activates right away, and the tabs that were open stay open, still running the old JavaScript, while the new worker handles their requests. You now have two app versions in one tab. Here’s what goes wrong:
-
Lazy chunks. The old page asks for
editor.3f9a1.js. The new worker has cleaned up the old cache, and the server no longer has that file. The dynamicimport()fails and the user sees a broken screen. - API contracts. The old client code sends requests in the old format to an API, or to worker logic, that now expects the new one.
-
IndexedDB migrations. The new version opens the database with a higher version number. The old tab’s connection gets a
versionchangeevent, and if it doesn’t close, the upgrade is blocked. If the old tab later tries to open the database at its lower version, it gets aVersionError. Either way, one of your two tabs is stuck.
skipWaiting() is fine when old and new versions are truly interchangeable. For an app with code-split bundles and a local database, that’s rarely true.
A production pattern
Here’s what I’d do instead:
-
Versioned precache. Put a version in the cache name so old and new caches can exist side by side, and only delete old ones in
activate. - Network-first for navigations. Get HTML from the network when you can and fall back to the cached shell when offline. This keeps the HTML pointing at the current bundles.
-
Cache-first for hashed assets. A file named
editor.3f9a1.jsnever changes, so serving it from cache is always safe.
// sw.js
const VERSION = 'v42'; // injected by the build
const SHELL_CACHE = `shell-${VERSION}`;
const ASSET_CACHE = 'assets'; // hashed files, safe to share across versions
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(SHELL_CACHE).then((c) => c.addAll(['/', '/offline.html']))
);
// no skipWaiting() here
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((k) => k.startsWith('shell-') && k !== SHELL_CACHE)
.map((k) => caches.delete(k))
)
)
);
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.mode === 'navigate') {
event.respondWith(
fetch(req).catch(async () => {
const cache = await caches.open(SHELL_CACHE);
return (await cache.match('/')) || cache.match('/offline.html');
})
);
return;
}
if (new URL(req.url).pathname.startsWith('/assets/')) {
event.respondWith(
caches.match(req).then(
(hit) =>
hit ||
fetch(req).then((res) => {
if (res.ok) {
const copy = res.clone();
caches.open(ASSET_CACHE).then((c) => c.put(req, copy));
}
return res;
})
)
);
}
});
self.addEventListener('message', (event) => {
if (event.data?.type === 'SKIP_WAITING') self.skipWaiting();
});
The shared asset cache will grow over time, so prune it occasionally. The point is that an old tab can still find its old chunks during the overlap.
Detect the waiting worker, let the user reload
Calling skipWaiting() is fine when the user asks for it and the page reloads right after. That way you never end up with two versions running in one tab.
// main.ts
async function registerSW(onUpdate: (apply: () => void) => void) {
if (!('serviceWorker' in navigator)) return;
const reg = await navigator.serviceWorker.register('/sw.js');
const prompt = (worker: ServiceWorker) =>
onUpdate(() => worker.postMessage({ type: 'SKIP_WAITING' }));
// A worker may already be waiting from an earlier visit
if (reg.waiting && navigator.serviceWorker.controller) prompt(reg.waiting);
reg.addEventListener('updatefound', () => {
const installing = reg.installing;
installing?.addEventListener('statechange', () => {
// "installed" + an existing controller = an update, not a first install
if (installing.state === 'installed' && navigator.serviceWorker.controller) {
prompt(installing);
}
});
});
let reloaded = false;
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (reloaded) return;
reloaded = true;
window.location.reload();
});
// Long-lived PWA windows: check now and then
setInterval(() => reg.update(), 60 * 60 * 1000);
}
onUpdate shows an “Update available” bar with a Reload button. In a notes app, don’t reload while the user is typing. Save first, then reload.
Other tabs on the old version will also get controllerchange and reload. If one of them has unsaved state, it needs to flush that before reloading too.
For state 4, write migrations so the new code can read old data and the old code doesn’t crash on new data for at least one release. Add fields, don’t rename them. Handle onversionchange by closing the connection and telling the user to reload, so an old tab doesn’t block the upgrade.
Reproduce it with two tabs
Single-tab testing hides most of this. What I do:
- Uncheck “Update on reload” in DevTools > Application > Service Workers. With it checked you are testing a lifecycle your users never see.
- Open the app in two tabs on the old version.
- Deploy a change that touches a lazy-loaded chunk and, ideally, the IndexedDB version.
- Reload one tab and watch the panel: installing, then waiting. Trigger the update prompt and watch it go to activating/activated, then see what happens in the other tab.
- In that other tab, open a route that lazy-loads code you haven’t hit yet.
If that second tab keeps working or reloads cleanly, you’re in decent shape.
Unregister is the emergency switch
If a bad worker gets out and traps users on a broken version, you can deploy a worker whose only job is to remove itself:
// sw.js (kill switch)
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
const keys = await caches.keys();
await Promise.all(keys.map((k) => caches.delete(k)));
await self.registration.unregister();
const clients = await self.clients.matchAll({ type: 'window' });
clients.forEach((c) => c.navigate(c.url));
})());
});
Keep it at the same URL as the old worker so browsers pick it up through the normal update check. This is the one place where skipWaiting() makes sense, because the whole goal is to get out of the way immediately. Don’t use it for routine releases: it throws away offline support, and for a local-first app that’s most of the reason to have a worker.
Takeaway
When someone says “the service worker didn’t update,” find out which of the four states they’re in before changing anything. Make sure sw.js actually changes and isn’t cached. Let a waiting worker wait until the user agrees to reload. Use network-first for HTML and cache-first for hashed assets. Treat data migrations as a compatibility problem, not a caching one. And test with two tabs.
I deal with this while building PenPage, a free, local-first Markdown notes app that runs in the browser. Notes stay on your device. Disclosure: it’s my project.
This article was drafted with AI assistance.
