Skip to content

    Frontend System Design

    Service Worker Lifecycle and Updates

    Follow a service worker from install to activate, see why a new version waits for open tabs, and when skipWaiting, claim and clean-up are safe, with a lab.

    By Published 13 min read

    • frontend-system-design
    • service-workers
    • lifecycle
    • updates
    Share

    Standard lessonadvancedTypeScript labFrontend System Design

    Before this: Service Workers

    What you'll learn: By the end of this lesson you will be able to name the states a service worker moves through, explain how the browser decides that a new version exists, say why a new version waits and what releases it, describe what skipWaiting() and clients.claim() change and what they put at risk, clean up old caches at the right moment, and plan a release so that a page open during a deploy keeps working.

    Think of a restaurant that changes its menu. The old menu is on the tables and customers are mid-meal. The manager prints the new one and keeps it in the kitchen. She will not swap menus while people are ordering from the old one, because a dish may no longer exist. She waits until the tables are empty, then swaps them all. A service worker update works that way by default. Swapping at once is possible, and this lesson is about when it is safe.

    The running example is the shop from Service Workers: product pages under /shop/, a worker that precaches a shell, and releases that ship new hashed files. The version names, cache names and numbers in the lab are made up.

    The core idea

    1. A browser keeps at most one active worker per registration, and a new version is a different worker. Updating means installing the new script beside the old one, then replacing it.
    2. The new worker waits by default. It becomes active only when no page is controlled by the old one. That is the browser protecting pages that were loaded with the old version, as web.dev puts it, ensuring "only one version of your service worker is running at a time".
    3. Skipping the wait is a trade. skipWaiting() makes the update immediate and puts a new worker in charge of pages that loaded under an old release. Whether that is safe depends on what the old pages still need.

    Three illustrations run through the lesson: a minimal example (a first install and a page that is not controlled), a realistic example (a release that waits for open tabs) and a failure example (a release that uses skipWaiting() and deletes the files an open page still needs).

    The states

    The specification gives each worker a state, one of parsed, installing, installed, activating, activated and redundant. A worker is parsed first.

    Figure 1. The states of one worker

    stateDiagram-v2 accTitle: Service worker states accDescr: A worker starts parsed and becomes installing. If install succeeds it becomes installed; if it fails it becomes redundant. An installed worker becomes activating when it is allowed to take over, then activated. An activated or installed worker becomes redundant when a newer worker replaces it. [*] --> parsed parsed --> installing installing --> installed: waitUntil resolves installing --> redundant: waitUntil rejects installed --> activating: no old pages, or skipWaiting activating --> activated activated --> redundant: replaced installed --> redundant: a newer worker replaces it

    How to read this: the middle of the diagram is the interesting part. A worker can sit in installed for a long time, and that is the "waiting" worker people talk about.

    Three slots hold workers on a registration: installing, waiting and active, and the same words describe where a worker is. MDN notes that the install event is sent once, and that during an update "the previous version is still responsible for fetches" while the new one installs.

    How the browser decides there is an update

    An update starts with a check. The browser fetches the worker script again and compares it with the newest worker it has. web.dev: "Your service worker is considered updated if it's byte-different to the one the browser already has." MDN's update() page says the same: if the new worker "is not byte-by-byte identical to the current worker, it installs the new worker". There is no version field to bump. A changed comment is a new worker, and an unchanged script is not.

    Checks begin in these ways:

    • A navigation to an in-scope page. This is the usual one: every page load starts a check.
    • An event such as a push, if the script has not been downloaded within the last 24 hours (MDN, Service Worker API).
    • A call to registration.update(), which the page can make on a timer for a long-lived page. MDN says the fetch of the worker "bypasses any browser caches if the previous fetch occurred over 24 hours ago".

    The registration option updateViaCache controls whether the HTTP cache may answer the script request. MDN documents three values: imports (the default) does not consult the HTTP cache for the worker script but does for imported scripts, all consults it for both, and none never does. With all, a script that the server allows the cache to reuse for an hour can hide a release for that hour (the lab's section 6).

    Why a new worker waits

    When a new worker finishes installing and an old one is active, "the new version waits while the old version is still controlling clients", in MDN's words, until "there are no longer any pages loaded that are still using the old service worker". The reason is consistency: a document "starts life with or without a service worker and maintains that for its lifetime". A page that loaded under release 1 has release 1's HTML and expects release 1's files.

    Figure 2. The slots during an update

    During an update the old worker is active and controls the pages while the new worker waitsOpen pages to Active worker (fetch events); Active worker to Cache Storage (reads v1); Waiting worker to Cache Storage (filled at install)Open pages: loaded under v1Open pagesloaded under v1Active worker: v1, controls pagesActive workerv1, controlspagesWaiting worker: v2, installedWaiting workerv2, installedCache Storage: shell-v1, shell-v2Cache Storageshell-v1,shell-v2fetch eventsreads v1filled at install

    How to read this: both caches exist at the same time, which is why cache names carry a version. The waiting worker already did its install work, so it can take over at once when the pages are gone.

    A new document opened during the wait is still controlled by the old worker. If that worker serves the HTML from its cache, the new document gets the old HTML: the lab shows a reload producing "html v1" while version 2 waits. That surprises people: a reload is not enough. web.dev explains: "Even if you only have one tab open to the demo, refreshing the page isn't enough to let the new version take over", because the new page loads before the old one is closed, so the current worker is always controlling a client during a refresh. Close or navigate away from every tab that uses the old worker.

    Figure 3. A release and two tabs

    sequenceDiagram accTitle: A release waits for two open tabs accDescr: Two tabs are controlled by worker 1. A deploy puts release 2 on the server. A navigation starts an update check. The browser installs worker 2, which waits. Reloading one tab keeps a client on worker 1 because the new page opens first. After the last tab using worker 1 closes, worker 2 activates and deletes the old cache. participant T as Tabs (v1 pages) participant B as Browser participant W1 as Worker 1 (active) participant W2 as Worker 2 Note over T,W1: two tabs controlled by worker 1 B->>B: deploy puts release 2 on the server T->>B: navigation starts an update check B->>W2: install (bytes differ) W2-->>B: installed, waiting T->>W1: reload: new page opens before the old closes Note over W2: still waiting T->>B: last tab using worker 1 closes B->>W1: worker 1 becomes redundant B->>W2: activate, clean up old caches

    How to read this: nothing in the sequence is wrong, which is the point. The "stuck" update is the system doing what it was designed to do.

    skipWaiting() and clients.claim()

    Two methods change the timing, and they are often confused.

    Method Called in What it does What it does not do
    self.skipWaiting() install (usual) or any time Forces the waiting worker to become active (MDN); "it then takes over the pages controlled by the old version" It does not skip installing, "just waiting" (web.dev)
    clients.claim() activate Lets an active worker set itself as the controller of all clients in its scope, and fires controllerchange on navigator.serviceWorker in those pages It does not reload anything

    MDN's claim() page states that when a worker is first registered, "pages won't use it until they next load", and that claim() changes that, with the caution that this "results in your service worker controlling pages that loaded regularly over the network, or possibly via a different service worker". web.dev calls this "troublesome": the worker controls some clients that loaded without it. The lab's section 1 shows the first install both ways: without a claim the open page stays uncontrolled until it reloads, and with a claim it is controlled at once and gets one controllerchange.

    Their use together makes an update take effect for the current page and all others. MDN's skipWaiting() page says to use it with claim() "to ensure that updates ... take effect immediately for both the current client and all other active clients". web.dev's warning is the other half: "skipWaiting() means that your new service worker is likely controlling pages that were loaded with an older version. ... If this might break things, don't use skipWaiting()."

    Clean-up at activate, and the hazard

    activate is the place to delete old caches. MDN: its primary use is to clean up resources used by previous versions, and promises passed to waitUntil() there block other events until they finish, so the clean-up has completed by the first fetch of the new worker.

    Combine that with skipWaiting() and a page can lose files. A page loaded under release 1 may still need release 1's lazily loaded chunk. If release 2 deletes the old cache and the server no longer has the old file, nothing can answer.

    Figure 4. Is skipping the wait safe for this release?

    flowchart TD accTitle: Deciding whether to skip waiting accDescr: A decision procedure for a release. If an open page from the old release could still request files that the new release removes, do not skip waiting. If the new worker serves a different data format or message protocol to old pages, do not skip waiting. Otherwise skipping is acceptable, preferably with the old files kept for one more release or with a prompt to reload. S{"Can an old page still
    request files only
    the old release has?"} -->|"yes"| N["Do not skip waiting,
    or keep the old files"] S -->|"no"| P{"Does the new worker change
    what old pages expect
    (messages, API shapes)?"} P -->|"yes"| N P -->|"no"| Y["skipWaiting is acceptable"] Y --> R["Optionally tell the user
    to reload"]

    How to read this: the first diamond is the one that bites in practice. Static files are the common case, but a worker that talks to its pages by messages has the same problem.

    The lab's section 4 shows both outcomes for the same page.

    A failed install

    If waitUntil() rejects during install, web.dev says the new worker is thrown away "but the current one remains active". Pages keep working with the old worker, and the next check tries again with the new script. The lab's section 5 shows an update whose precache list names a file that was never uploaded: the worker becomes redundant, the active worker and its cache stay as they were, and the page still gets its chunk from the old cache. The fix is on the release side, in a check that every precached file exists before the worker script is published.

    Build it: updates, waiting, skipWaiting and a failed install

    Goal. Run a model of a registration through first install, a release that waits, a script that differs by one comment, the skipWaiting hazard, a failed install and the HTTP cache setting.

    This is a model, not a browser. Node cannot run a real service worker, and no browser is used for this lesson. The lab implements the rules listed at the top of src/registration.ts with sequential awaits. It shows the shape of the rules. It does not show when a particular browser runs update checks, how it schedules events or how long a tab takes to close.

    Setup. The lab is in the course's example workspace (Node 26.13.1, pnpm).

    cd examples/frontend
    pnpm install
    cd labs/service-worker-lifecycle
    pnpm test           # 21 tests
    node src/demo.ts

    Steps.

    1. Run the tests, then the demo, and compare it with the excerpts below. Then compare imports and all in section 6.
    2. In section 4, compare the two runs. Only skipWaiting=true ends with a 404.

    Expected output, captured from a real run (Node 26.13.1, 2026-12-01; the first excerpt, section 1 of the demo):

    === 1. First install: a page that loaded without a worker stays uncontrolled
    claim=false: after activation, page controller = none, controllerchange events = 0
      after a reload, the new page's controller = worker 1 (shell-v1)
    claim=true: after activation, page controller = worker 1 (shell-v1), controllerchange events = 1

    Expected output, captured from a real run (Node 26.13.1, 2026-12-01; the second excerpt, section 4 of the demo):

    === 4. skipWaiting and clean-up: a page can lose the files it still needs
    skipWaiting=false: active=worker 1 (shell-v1)  waiting=worker 2 (shell-v2)  pages=[1:v1/w1]
      caches=["shell-v1","shell-v2"]; the v1 page asks for its lazy chunk -> status 200 from cache ("chunk v1"), controllerchange events on the page = 0
    skipWaiting=true: active=worker 2 (shell-v2)  waiting=none  pages=[1:v1/w2]
      caches=["shell-v2"]; the v1 page asks for its lazy chunk -> status 404 from network ("not found"), controllerchange events on the page = 1

    Sections 2 and 6 of the full output (not shown here) cover the waiting release and updateViaCache: after both tabs reload, version 2 is still waiting; and with all, an HTTP-cache copy of the script that is still fresh makes the update check report "byte-identical" 60 seconds after a deploy, while imports installs version 2.

    What the lab does not show. No real document or tab, no real HTTP cache, no imported scripts (so imports and none behave the same here), no 24-hour rule and no push or sync events. Cache.addAll() stores nothing unless every response is ok; MDN does not say what a real browser keeps after a failure.

    Break it. In test/registration.test.ts, the test "with clean-up, a page of the old release loses the files only the old cache held (the hazard)" deploys release 2 with skipWaiting: true. Change that to false and the test fails, because the chunk now comes from the old cache. Change it back and re-run the tests.

    Trade-offs

    Choice Gain Cost
    Default waiting Every page keeps one consistent version Users who never close a tab stay on an old version
    skipWaiting() New code is live at once Old pages meet a new worker mid-session
    clients.claim() First install controls open pages at once A worker controls pages that loaded without it
    Delete old caches at activate Storage stays small Old pages lose files only the old cache held
    Keep the previous cache one more release Old pages survive a deploy Two shells on disk, and a rule for when to delete

    In practice: a release that broke the tabs people left open

    NOTE

    This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.

    The situation

    The shop's worker has been in production for months. Shoppers keep a product page open in a tab for days. Each release replaces hashed files and the old ones are removed from the server. The team wants releases to reach users faster, because fixes sit unseen behind the waiting worker. A developer adds skipWaiting() and clients.claim() and deletes old caches at activate.

    Starting architecture

    Figure 5. Starting point: an update waits for tabs that never close

    Starting architecture: the new worker waits while long-lived tabs keep the old one in controlLong-lived tabs to Active worker (controlled by); Active worker to Waiting worker (blocks)Long-lived tabs: loaded under v1Long-livedtabsloaded under v1Active worker: v1Active workerv1Waiting worker: v2, not in controlWaiting workerv2, not incontrolcontrolled byblocks

    How to read this: nothing is broken, but a release can take as long as the longest-lived tab.

    Options considered

    Option What it fixes What it costs Risk
    Keep waiting Nothing breaks Slow rollout of fixes Users stay on old code
    skipWaiting() plus clean-up Immediate rollout Small Old pages lose files they still need
    Keep the previous cache for one release, then offer a reload prompt Old pages survive; users move on soon A rule and a prompt to build A prompt users ignore

    The decision

    Field Decision record
    Context Releases are slow to reach long-lived tabs; the old files are removed from the server at each deploy.
    Decision Keep the waiting behaviour, but delete a cache only when it is two releases old, and show a "new version available, reload" prompt when a worker is waiting. The prompt triggers the skip.
    Consequences, good Old pages keep working; users reach new code when they accept the prompt.
    Consequences, bad Two caches on disk, and a prompt to design and measure.
    Revisit when Releases stop removing old files, or tabs stop living for days.

    Rollout and verification

    1. Test the waiting path in the developer tools, using the "update on reload" and "skip waiting" controls that web.dev describes for testing updates.
    2. Check that every precached file exists before publishing the worker script.
    3. Deploy to a small share of users first, and watch for 404s on hashed files and for page errors after controllerchange.

    The 2 a.m. failure

    After the skipWaiting() release, support reports blank sections on pages that were open during the deploy (illustrative). Error tracking shows 404s for a lazy chunk of the old release. The tabs were taken over by the new worker, which deleted the old cache, and the server no longer has the old file. This is section 4 of the lab.

    First three checks: (1) which cache names exist on an affected browser? (2) did the page's controller change (controllerchange) just before the errors? (3) does the missing file exist on the server? Mitigation: ship a worker that keeps the previous cache and does not skip waiting, then ask users to reload. Follow-up: add a release check that lists the files an old release's pages request, and keep them for one more release.

    Architect's notes

    • Version caches by release, and delete only caches that are at least two releases old.
    • Treat skipWaiting() as a compatibility promise between the new worker and every open old page.
    • Prefer a prompt to a surprise. Telling the user a new version is ready is cheaper than debugging a half-old page.
    • Keep the HTTP cache from hiding the worker script, especially with updateViaCache: "all".

    Common mistakes

    • Expecting a reload to apply the update. The new page opens before the old one closes.
    • skipWaiting() plus deleting old caches in one release.
    • A precache list with one wrong path, so installs keep failing quietly.
    • A version number in a comment only, assuming it is compared. The browser compares bytes.

    Key takeaways

    • A worker moves through parsed, installing, installed, activating, activated and redundant.
    • An update is a new script whose bytes differ; checks start on navigation, on update() and on some events.
    • A new worker waits until no page is controlled by the old one; a reload does not release it.
    • skipWaiting() removes the wait; clients.claim() takes over uncontrolled pages. Neither reloads anything.
    • Clean up old caches at activate, but only after asking what old pages still need.
    • A failed install discards the new worker and leaves the old one in place.

    Practice exercises

    Exercise 1 — Starter (45 min): In the lab, add a test that a script whose only change is a comment installs a new worker and activates when no page is open.

    Exercise 2 — Intermediate (2 hours): Change cleanup so that a cache is deleted only when it is two releases old. Show with a test that the page in section 4 keeps its chunk with skipWaiting: true.

    Exercise 3 — Advanced (half day): Add an "update ready" flag to the registration that the page can read when a worker is waiting, and a skipWaiting call that only the page can trigger. Write the test for a page that accepts the prompt and one that never does.

    In this course:

    Planned lessons in this module, named here as plain text: Service Worker Caching Strategies and Installable PWAs, the Manifest and Push Notifications.

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments