A service worker is a JavaScript file that runs in the background, completely independent of your web page. Think of it as a programmable network proxy: every fetch() call your page makes passes through it, and it decides whether to serve from cache, forward to the network, or return a custom response. Google Docs uses this to let you edit documents offline. Twitter uses it to show cached tweets when you lose signal. Spotify uses it to stream cached playlists without data.
But service workers are not like normal JavaScript. They have no DOM access. They require HTTPS. They follow a strict lifecycle (install → wait → activate → fetch) controlled entirely by the browser. They can be killed at any moment when idle. And they can only communicate with the page via postMessage().
Here is what a production-grade service worker looks like in practice — the full pipeline from registration to strategy routing:
This is Workbox — Google's library that wraps the raw service worker APIs. I use it in production because it handles cache versioning, routing, and response cloning correctly — all things that are easy to get wrong with raw APIs. Let me walk through how I arrived at this configuration.