Lesson 1 of 9 · 26m
Service Worker Lifecycle and Update Control
Treat registration, installation, activation, and updates as a deployment system with user-visible state, not as browser trivia.
The worst service worker bug I ever inherited was not offline logic. It was an update loop: users kept a six-week-old bundle alive because a well-meaning skipWaiting() collided with lazy-loaded chunks. Half the app was new, half was old, and every error looked like a backend outage. Service workers are not a cache trick. They are a client-side deployment system that happens to intercept network traffic.
The lifecycle is the product
Registration, installation, activation, and redundancy are separate states. The browser can install a new worker while the old one continues controlling open pages:
const reg = await navigator.serviceWorker.register('/sw.js', {
scope: '/',
updateViaCache: 'none',
});
reg.addEventListener('updatefound', () => console.log('installing:', reg.installing));register() returns quickly. The browser fetches sw.js byte-for-byte on update checks, installs it, and usually keeps it waiting while existing clients remain open. That waiting state is where teams lose control.
Version updates like deployments
Give every deployment a build identity and make activation policy explicit:
const BUILD = '2026.11.30-8f4d21c';
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
console.log(`activated ${BUILD}`);
});skipWaiting() is not inherently wrong. It is wrong when the app cannot tolerate mixed versions. If your new shell lazy-loads chunk editor-a91c.js while old chunk URLs have disappeared, activation can break navigation.
A safer pattern is to detect a waiting worker and ask the user to reload at a predictable moment:
function showUpdateReady(reg) {
const updateBar = document.querySelector('#update-bar');
updateBar.hidden = false;
updateBar.querySelector('button').onclick = () => {
reg.waiting?.postMessage({ type: 'SKIP_WAITING' });
};
}
navigator.serviceWorker.addEventListener('controllerchange', () => location.reload());In the worker, handle only that explicit message:
self.addEventListener('message', (event) => {
if (event.data?.type === 'SKIP_WAITING') self.skipWaiting();
});This gives the app a chance to persist drafts first. Silent reloads destroy trust; explicit updates feel native.
Scope is part of the design
The worker controls only URLs under its scope. /sw.js normally controls the whole origin; /app/sw.js controls /app/ and below. Scope mistakes produce the classic mystery where offline works on /app/home but not /settings. Keep one worker per origin unless you have a compelling isolation requirement.
Field notes
- Hard refresh does not bypass an active service worker in the way many developers expect.
updateViaCache: 'none'protects the worker script from stale HTTP cache behavior; it does not disable runtime caching.- iOS Safari can evict storage and service workers aggressively for unused sites. Test real devices.
- Never deploy a worker without a removal plan. A bad worker can keep serving itself after application files are fixed.
Was this lesson helpful?