Service Workers
A service worker is a script the browser runs in the background, off the page. It can intercept fetch, cache responses, and show notifications. It is not a Web Worker you send CPU jobs to.
Detailed Explanation
You register from the page: navigator.serviceWorker.register("/sw.js"). The worker then cycles through install, waiting, activate. It controls pages on its scope (usually /) after a navigation or clients.claim(). HTTPS (or localhost) is required.
The useful event is fetch. The worker can event.respondWith(cache.match(req) || fetch(req)). That is how offline shells and asset caches work. Strategies: cache-first (static assets), network-first (API), stale-while-revalidate (show cache, update in background).
Cache storage is a request→response map. Version your cache names (static-v3) and delete old caches in activate. A stuck old worker serves old JS until it activates; that is why deploy stories include "skip waiting" and cache busting.
Service workers can receive push (with a server key) and open windows. They must finish work quickly or waitUntil a promise. They have no DOM. They can importScripts or be modules.
What it costs you: caching bugs that look like "my fix is not deploying". Users run yesterday's bundle until the new worker activates. Always version assets. Never cache index.html aggressively without a network-first strategy if you need instant deploys. Debugging is Application → Service Workers in DevTools.
Reach for a service worker for offline, installable PWAs, and precise cache control. Reach for a Web Worker for CPU. Reach for HTTP cache headers when a SW is too much.
clients.claim() takes control of already-open pages on activate. Without it, those tabs keep the old worker until the next navigation. That is gentler for some apps and worse for "we shipped a security fix". Choose on purpose.
Bypass in DevTools ("Update on reload") is for you, not for users. A kill-switch query param that unregisters the worker is worth shipping once you have been burned by a bad cache.
Do not cache opaque (no-cors) responses unless you understand they are write-only in the Cache API and can fill quota.
Code Examples
page.js
// page.js
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js");
}sw.js
// sw.js
const CACHE = "static-v1";
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(["/", "/app.js"])));
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))),
),
);
});
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((hit) => hit || fetch(event.request)),
);
});Common Gotchas
An old service worker can serve an old app forever
bump cache names and have a strategy for `index.html` (network-first).
Cache-first on API GET can show stale user data
use network-first or no SW for authenticated JSON.
Scope is the worker's path
`/sw.js` controls `/`; `/assets/sw.js` may only control `/assets/`.
This is not a compute worker
no `postMessage` of a sort job as the primary design. Use `Worker`.