Skip to content

ui-sdk: a new service worker activates under a busy page and deletes the lazy chunks it still needs #2569

Open

Description

@schlessera

Context

registerDefaultRoutes activates a new worker as soon as it installs and claims every open page: skipWaiting() on install and clients.claim() on activate, under the comment "Activate new service worker immediately on deploy" (packages/ui-sdk/src/client/sw-policy.ts:191-202).

When the new worker activates, Workbox's precache controller deletes every entry of the precache that is not in the new manifest (PrecacheController.activate, workbox-precaching 7.4.1, the version packages/ui-kit/package.json pins). A page still running the previous build that then loads a lazy chunk finds it gone from the precache; the request falls through to the network, where the deploy has usually removed it. That is the failure StaleChunkFallback exists to absorb: "A new version is ready", then an automatic or manual reload, or a "held" state when the reload would lose unsent work (StaleChunkFallback, packages/ui-react/src/components/layout/page-boundary.tsx:121-170).

So a busy user, typing a draft with dictation under review, can open a view they have not opened yet and get a fallback screen instead of the view, even though the page reload itself is correctly held by the update holds (reloadWhenIdle, packages/ui-react/src/hooks/use-service-worker-updates.ts:98-106).

Two comments are also inaccurate:

  • sw-policy.ts:204-207 says cleanupOutdatedCaches() deletes "precaches from previous builds". It only deletes caches left by older, incompatible Workbox versions (-precache- names). Per-entry deletion is the controller's activate.
  • sw-policy.ts:250-257 calls navigation "network first". A navigation to / (every launch of this hash-routed app) matches the precache route first, which Workbox's directoryIndex maps to /index.html, so it is served cache-first. Only paths not in the precache reach the network-first handler (sw-policy.ts:264-287).

#2566 makes update checks more frequent while a page is open, which makes this case more common.

Guide: (no guide). The modern-web-guidance performance guide says "DON'T bypass versioning: Failing to update asset hashes/versions will trap users in infinite cache loops", which every option below respects.

Binding: D31 (foundations): a stale PWA must fail loudly. The update-hold registry (offline continuity, #1015, #1112, update proof PR #1194) defers the reload but not the activation.

Rulings (maintainer, 2026-10-22)

All three choices are settled; the rulings are in the comments.

  • Lifecycle: B, idle-gated handover. A newly installed worker no longer calls skipWaiting() on install; it waits until the open pages are idle and activates on a { type: "SKIP_WAITING" } message. Keeping immediate activation (A) and retaining one previous build's chunks (C) were not selected.
  • Multi-tab: all open app tabs idle. An idle tab alone cannot trigger handover while another app tab is busy. A suspended or unresponsive tab blocks activation; a missing readiness response is not evidence of idle. This accepts delayed updates, still bounded by the loud stale-client checks (D31).
  • Compatibility: required members, breaking. ServiceWorkerPolicyScope gains required members (an addEventListener("message", listener) overload and clients.matchAll({ type: "window" }) alongside claim()). No optional-member fallback, no opt-in flag, no parallel immediate-activation path. A host passing the real self is unaffected; custom structural scopes and mocks must add the members.
  • Release classification (maintainer, 2026-10-24): before 1.0.0 an approved break ships as a minor changeset naming the break, never a major one (docs/decisions/contract-versioning.md:14-20). This replaces the "major changeset" wording in the 2026-10-22 required-members ruling; the breaking classification stands.

Scope

Coordinate readiness across the open app tabs before activating the waiting worker. A busy, suspended or unresponsive tab blocks activation; lack of a response is not evidence of idle.

  • sw-policy.ts: remove the install skipWaiting(), add a message listener that calls skipWaiting() for { type: "SKIP_WAITING" }, and extend ServiceWorkerPolicyScope with the message/client capabilities needed for all-tabs coordination. The required additions are an addEventListener("message", listener) overload and clients.matchAll({ type: "window" }) alongside claim(). They are approved breaking requirements with no optional fallback or opt-in flag. Custom structural scope implementations and mocks must add them. The activation command must be sent only through the coordinated readiness flow.
  • use-service-worker-updates.ts and the worker: when a worker reaches installed with a controller (or registration.waiting exists at registration), coordinate readiness across all open app tabs using each tab's existing busy() test. Activate only after all participating open app tabs are confirmed idle. Re-evaluate when holds or the open-tab set change; a busy, suspended or unresponsive tab blocks activation. Do not let one idle tab independently post an unconditional activation command. Keep the existing controllerchange reload guard and update holds as a second safety check. Bound and clean up coordination requests/listeners; do not treat timeout or missing response as readiness.
  • Correct the two comments at sw-policy.ts:204-207 and sw-policy.ts:250-257.
  • Record the ruling as a dated entry in docs/decisions/design-kit/offline-continuity.md.

Out of scope

Acceptance criteria

  • The lifecycle ruling is recorded on this issue: B, idle-gated handover.
  • The multi-tab activation policy is recorded: all open app tabs must be idle; suspended or unresponsive tabs block activation.
  • The required public worker API compatibility break is approved before implementation.
  • Document migration for host scope implementations/test doubles and include a minor changeset explicitly naming the break. Update the API report and package-API contract in the same CONTRACT: commit.
  • Record the final lifecycle and tab rulings in docs/decisions/design-kit/offline-continuity.md during implementation.
  • A real-browser test in ui-react-layout, built on packages/ui-react/tests/browser/offline/cold-capture/, holds the page busy, deploys worker v2, then lazy-loads a chunk only the v1 precache holds. The chunk loads, v2 stays waiting, and after the hold ends one controllerchange and one reload follow. The test fails on the tree before the change.
  • A real-browser multi-tab test shows an idle tab cannot trigger activation while another app tab is busy; the v1-only lazy chunk remains available in the busy tab. Once every tab is idle, one activation occurs and each eligible tab reloads once.
  • Coordination tests show suspended/unresponsive tabs and missing readiness responses do not permit activation; closing a blocking tab allows the remaining idle tabs to proceed. New-tab and idle-to-busy changes during coordination cannot bypass readiness checks.
  • A unit test shows activation is never requested from local idle state alone: every open app tab must be confirmed idle, and duplicate readiness notifications do not cause duplicate activation.
  • The two comments state what Workbox actually does.

Verification

bun run test packages/ui-sdk/tests/sw-policy.test.ts packages/ui-react/tests/render
bun run test:layout
bun run typecheck
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit. registerDefaultRoutes and ServiceWorkerPolicyScope (packages/ui-sdk/src/client/sw-policy.ts:57-67) are ordinary exports recorded in api-report/ui-sdk.txt; the required members and the standard coordinated activation are an approved break and carry breaking.
  • This changes a published package's behaviour, so it needs a changeset: a minor changeset for @schlessera/brain-ui-sdk (pre-1.0 policy) that names the break, the new required scope members and the removed install-time skipWaiting(), with migration notes for custom structural scopes and test doubles. Do not write a major changeset. Update api-report/ui-sdk.txt and docs/integration-contract/package-api.md in the same commit.

2026-10-24: replaced the "major changeset" instruction with the pre-1.0 minor changeset naming the break (maintainer, 2026-10-24); replaced the "Decision needed" options section and the stacked 2026-10-22 corrections with a summary of the settled rulings (the option texts are preserved in a comment).

Activity

  1. added
    type: fix
    area: ui-sdk
    area: ui-react
    priority: p2
    needs: decision
    on Oct 22, 2026
  2. added
    contract
    on Oct 22, 2026
  3. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    2026-10-22 triage at main aa8c8966b59f031f054f4876dbca84048b40ae4f: corrected the claim that an ordinary exported service-worker scope is outside the package contract. Verified the exported declarations and recorded signatures, plus the existing install/activate listeners. Add contract for the proposed B API change; preserve needs: decision and the open A/B/C and multi-tab choices. No lifecycle option or breaking change is approved by this review.

  4. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-10-22): Select option B: idle-gated service-worker handover. A newly installed worker waits rather than activating immediately, so busy pages retain the cached code their current build needs. Options A (keep immediate activation) and C (retain one previous build while activating immediately) are not selected.

    The multi-tab policy remains open: one idle tab versus all open tabs idle. The existing scope is only an outline of the one-idle-tab variant, not approval of that policy. Any required public API compatibility ruling also remains open. The body and acceptance criteria now distinguish the settled lifecycle from these unresolved choices; needs: decision stays.

  5. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-10-22): Multi-tab policy: option B, all open app tabs must be idle before service-worker activation. An idle tab alone cannot trigger handover while another tab is busy. A suspended or unresponsive tab blocks activation; missing readiness evidence must not be treated as safe. This accepts additional coordination complexity and potentially delayed updates.

    The body, implementation scope and acceptance criteria now require all-tabs coordination and multi-tab runtime proof, replacing the previous one-idle-tab outline. Existing reload holds and loud stale-client checks remain. The public API compatibility approach is still unresolved, so needs: decision stays.

  6. added
    breaking
    agent-ready
    and removed
    needs: decision
    on Oct 22, 2026
  7. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-10-22): Compatibility option B: approve the required breaking public worker API/scope changes for the selected idle-gated, all-open-app-tabs-idle handover. Make coordinated activation standard behavior; no compatibility adapter or parallel immediate-activation path is required. Host implementations and test doubles may need updates.

    Use breaking and contract, migration notes, a changeset explicitly naming the break, a CONTRACT: commit prefix and same-commit API report/package-API contract updates. The pre-1.0 policy permits the approved break in a minor release.

    All three rulings are settled: idle-gated activation; every open app tab confirmed idle, with unresponsive tabs blocking; and the required compatibility break approved. The body and acceptance criteria reflect them. Remove needs: decision and add agent-ready; no implementation or runtime verification is claimed. Record the final rulings in offline-continuity.md during implementation.

  8. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-10-22): public API compatibility. Required members; the break is accepted.

    ServiceWorkerPolicyScope gains required members for the idle-gated, all-tabs handover:

    • an addEventListener("message", listener) overload
    • clients.matchAll({ type: "window" }), alongside claim(), for readiness coordination across every open app tab

    There is no optional-member fallback and no opt-in flag: one lifecycle, and every host gets the fix. A host passing the real self is unaffected. Custom structural scope implementations and mocks must add the members.

    Rationale: D31 states there is no backwards-compatibility burden, and a silent fallback to immediate activation would keep the stale-chunk failure in exactly the hosts that cannot see it.

    Required handling, before code:

    • Add the breaking label (contract is already set).
    • Add a major changeset for @schlessera/brain-ui-sdk, naming the new required scope members and the removed install-time skipWaiting().
    • Update api-report/ui-sdk.txt and the package-API contract in the same CONTRACT: commit as the code.
    • If brain-hosting-template's service-worker.js calls skipWaiting() itself or passes a narrowed scope, file the host issue there.

    All rulings on this issue are now recorded: lifecycle B, all tabs idle, and the break accepted. needs: decision is removed.

  9. added
    agent-ready
    and removed
    agent-ready
    on Oct 22, 2026
  10. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    2026-10-22 readiness reconciliation: folded the maintainer ruling into the body, replacing the unresolved-choice specification and aligning scope/criteria with the accepted branch. The remaining work is explicitly scoped; add agent-ready. Preserve priority, native parent, assignment and release attribution. This is issue preparation, not implementation, runtime proof or delivery.

  11. schlessera commented on Oct 23, 2026

    @schlessera
    OwnerAuthor

    History, moved from the body on 2026-10-24. The options as offered before the 2026-10-22 rulings:

    Decision needed

    Should a newly installed worker stay waiting until an open page is idle and tells it to activate, instead of calling skipWaiting() on install? That partly reverses the deliberate "Activate new service worker immediately on deploy" behaviour.

    Options for the maintainer:

    • A: keep today's behaviour. Activation is immediate, and a busy page may hit StaleChunkFallback. Closes this issue with a documented no; only the two comment fixes land.
    • B: idle-gated handover. The worker no longer calls skipWaiting() on install. It listens for a { type: "SKIP_WAITING" } message and calls skipWaiting() when it gets one. useServiceWorkerUpdates posts that message to registration.waiting when the page is idle under the same busy() test that gates the reload today, and the existing controllerchange reload follows. Trade-offs: a second open tab that is busy loses chunks when an idle tab triggers the handover, because skipWaiting is per worker, not per tab; a page that never goes idle runs the old version indefinitely, which the stale-client checks still bound (D31); and a host whose service-worker.js does not use registerDefaultRoutes keeps immediate activation.
    • C: immediate activation, but keep the previous build's chunks for one generation. Keep skipWaiting(), and copy the outgoing precache entries into a bounded "previous build" cache that a route consults before the network for script/style requests, dropped on the next activation. The lifecycle does not change, but the SDK keeps code the server no longer serves.

    The dated triage and correction notes that followed in the body are the comments above.

    Release classification (maintainer, 2026-10-24): before 1.0.0 an approved breaking change ships as a minor changeset naming the break, never a major one; under the lockstep fixed group a major changeset would publish 1.0.0 (docs/decisions/contract-versioning.md:14-20). The required-members break stands; only the changeset level in the 10:11 ruling changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactarea: ui-sdkbreakingcontractpriority: p2type: fix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions