How do service workers make offline pages work?
Useful first: HTTP · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALExplain what a service worker can do without treating it as an automatic offline switch.
An offline feature needs a plan
A browser-side request handler
A page normally asks for resources through HTTP. For pages it controls, a service worker can handle requests and provide responses, including ones saved earlier. It runs separately from the page. It is not the website's remote server, and it cannot directly edit the page's document.
[1] [4]Save the pieces an offline view needs
Imagine a walking guide whose developers deliberately save a map, its page and the files needed to display it. Their request-handling code can use those saved responses when the network is unavailable. A live ticket purchase still needs its server. Merely registering a worker saves none of those resources: the application must choose what to store and what to show when something is missing.
[1] [5]A helper that wakes for work
A service worker responds to events; it is not a process guaranteed to run continuously. The browser can stop an idle worker and start it again for another event. Service workers require a secure context, normally HTTPS; browsers also support trustworthy local development contexts such as localhost.
[1] [7]An offline feature is an application design, not a side effect of registration.
Adding a service worker makes every page usable offline.
Offline behavior depends on the responses and fallback logic the application actually provides.
Show the explanation
Registration alone provides no offline guarantee. The developer must implement the saved responses and fallback behavior. Registering a script does not supply those features.
YOUR GOALDistinguish registering a worker from controlling a page, and predict which requests a controlled page can send through it.
Scope, control and requests
The script location sets the default scope
On a site, navigator.serviceWorker.register("/guide/sw.js") defaults to the scope /guide/. A page at /guide/map can fall within that registration; /news does not. The script and scope must be same-origin with the registering page. A broader scope than the script directory requires permission through the server's Service-Worker-Allowed response header.
Registered does not mean controlling this page
The registration promise returns a registration object, not proof that this document is controlled. By default, a page that first registers a worker remains uncontrolled even after activation; a later in-scope navigation can use it. Check navigator.serviceWorker.controller for this page's controller: null means it is not currently controlled.
During installation, event.waitUntil(promise) can make required preparation part of installation. If that promise rejects, installation fails instead of approving an incomplete worker.
Scope is about clients, not a resource-path filter
Once /guide/map is controlled, its request for /assets/map.svg can trigger the worker's fetch handler even though the image URL is outside /guide/. The handler can inspect the request and choose a response. It must call respondWith() synchronously during event handling, but may pass a promise that resolves later to a Response. Waiting first and then calling respondWith() is too late.
Check the page's controller separately from its registration, then reason about requests from that page.
A /guide/ scope only lets the worker handle resource URLs starting with /guide/.
Scope selects eligible clients. Requests from a controlled page can include resources outside that URL prefix.
Show the explanation
The image request can reach the worker because the requesting page is controlled. A controlled page's subresource requests can trigger fetch events. This does not expand the registration's scope to other pages.
YOUR GOALDiagnose stale saved responses and waiting updates without assuming continuous execution or forcing incompatible versions together.
Manage data and worker versions separately
A Cache entry does not expire itself
The Cache API stores request/response pairs under named caches. Unlike assumptions you might bring from HTTP caching, this API does not automatically honor HTTP caching headers or expire entries. If an application keeps returning its saved map, a past max-age deadline alone does not replace that entry. The application needs an update and cleanup policy. Browser storage can also be evicted, so a missing saved response needs a handled outcome.
Do not put durable state only in worker globals
An in-memory flag such as mapReady = true disappears when the worker stops. Read the relevant stored data when handling later events instead of treating that flag as durable proof. A service worker cannot use the synchronous localStorage API available to pages. waitUntil() associates a promise with event work; it is not a promise that a process will remain alive forever or evade browser execution limits.
Installing an update is not switching every tab
Suppose version 2 installs while an open guide still uses version 1. Normally the new worker waits until the old worker controls no clients; an ordinary refresh can leave overlapping clients. skipWaiting() skips that wait, not installation. clients.claim() lets an activated worker take control of eligible uncontrolled clients. These changeover choices need compatibility planning: an older page may receive responses from newer worker code.
Design cache freshness, missing-data handling and worker activation as separate decisions.
A waiting worker is broken, so skipWaiting and clients.claim are always the fix.
Waiting is part of the update lifecycle. Forcing a changeover can combine an existing page with different worker behavior.
Show the explanation
The normal lifecycle can delay activation while the previous worker still controls clients. A successfully installed update can wait for old clients to leave. Cache entry expiry is a separate concern, and skipWaiting changes activation timing rather than repairing installation.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- MDN — Service Worker API
- MDN — ServiceWorkerContainer.register()
- web.dev — The service worker lifecycle
- MDN — ServiceWorkerGlobalScope: fetch event
- MDN — Cache
- MDN — ExtendableEvent.waitUntil()
- W3C — Service Workers draft specification: lifetime
- MDN — ServiceWorkerContainer: controller property