Service Worker
A browser-managed script that runs separately from a web page. It can intercept requests and return saved responses, allowing parts of a site to work without an internet connection.
What it does
Imagine opening a documentation site on a train after losing your connection. An ordinary network request might fail. A service worker can return a previously saved page or a dedicated offline screen instead. It acts like a programmable proxy inside the browser, so it can respond without reaching your server. Registering one does not, by itself, make a site work offline: your code decides what to save and when to use it.
Registration and lifecycle
A page calls navigator.serviceWorker.register('/sw.js') in a secure context, typically HTTPS or localhost during development. The registration’s scope determines which pages on the same origin it can control. With the script at /sw.js, the default scope is /; putting it at /docs/sw.js normally limits the scope to /docs/.
The browser fires install when it installs a new version. This is a good time to fetch files the worker needs before it can take over. The activate event is a good place to remove obsolete caches. An update normally waits to activate until the old worker no longer controls any clients. On the first visit, the page that registers the worker is normally not controlled until a subsequent navigation.
Example: an offline navigation fallback
After registering /sw.js, put the following in that file. The worker saves one HTML file during installation and uses it if a GET navigation fails at the network level.
self.addEventListener('install', event => {
event.waitUntil(caches.open('offline-v1')
.then(cache => cache.add('/offline.html')));
});
self.addEventListener('fetch', event => {
if (event.request.mode !== 'navigate' ||
event.request.method !== 'GET') return;
event.respondWith(fetch(event.request)
.catch(() => caches.match('/offline.html', {
cacheName: 'offline-v1'
})));
});
waitUntil() ties installation to the cache operation: if the file cannot be saved, installation fails. respondWith() supplies the response for that request. A server response with a 404 or 500 status is still a response, so it does not trigger this fallback. Keep the offline page self-contained if it needs to work without loading additional assets.
Common uses and misconceptions
PWAs use service workers for offline screens, repeat loads, and push notifications on supported browsers. The Cache API is separate from the browser’s HTTP cache: application code explicitly stores, retrieves, and deletes responses. Unlike a dedicated Web Worker, which you might use to move calculations off the main thread, a service worker is started by the browser to handle events. It has no DOM access, and the browser can stop it between events, so global variables are not reliable storage.
In frontend interviews
A typical question is why users still see an old build after deployment. Separate two possibilities: the updated worker may be installed but waiting, or the active worker may still be serving old cached files. Forcing activation with skipWaiting() can make an already open page use a worker from a different build. A good answer explains how you would keep the page, its assets, and the worker compatible during the update.