Skip to content

ui-react: an installed app left open never checks for a new service worker #2566

Open

Description

@schlessera

Context

useServiceWorkerUpdates registers the worker and keeps nothing else: the registration is used only to watch updatefound (serviceWorker.register("/service-worker.js"), packages/ui-react/src/hooks/use-service-worker-updates.ts:131-142). Nothing in packages/ui-react/src calls registration.update().

The browser checks for a new worker on navigations into scope and, at most once a day, on functional events. An installed Android PWA is a single-page app that Android resumes from the background without navigating, so a page left open for days does not see a deploy until the user happens to relaunch it. Every launch is served cache-first from the precache (see the epic), so nothing else brings the new version in.

Once a new worker is found, the existing chain already does the right thing: track the installing worker, arm the takeover reload on installed with a controller, and reload on controllerchange only when every update hold is idle (use-service-worker-updates.ts:96-129, registerUpdateHold, packages/ui-react/src/lib/update-holds.ts). This task only makes the check happen.

Related: #2569. More frequent checks make a mid-session takeover more likely while a page still needs lazy chunks from the old build; that failure is already absorbed by StaleChunkFallback (packages/ui-react/src/components/layout/page-boundary.tsx:121-170), and the handover question is decided there, not here.

Guide: (no guide). The modern-web-guidance performance guide's Service Workers section covers caching strategies, not update checks.

Binding: D31 (foundations) and the update-hold registry (offline continuity, #1015). Neither changes.

Scope

  • Keep the ServiceWorkerRegistration that register() resolves with.
  • Call registration.update() when document becomes visible (visibilitychange with visibilityState === "visible") and on window online.
  • Throttle those calls to at most one per 30 minutes per page, counted from the last call made (registration counts as a check). Name the interval as a constant.
  • Swallow and log an update() rejection (offline, server error) the way registration failures are logged today; a rejection does not count against the throttle.
  • Remove both listeners in the effect's cleanup, and make no call after dispose.

Out of scope

Acceptance criteria

  • With a registered worker, a visibilitychange to visible calls registration.update() once; a second one within 30 minutes does not; one after 30 minutes does. Same for online, sharing the same throttle.
  • A hidden visibilitychange never calls update().
  • After the hook unmounts, neither event calls update(), and both listeners are removed.
  • A rejected update() is caught (no unhandled rejection) and the next trigger may call again.
  • A unit test in packages/ui-react/tests/render/ with a fake navigator.serviceWorker and fake clock covers the four criteria above, and fails on the tree before the change.
  • A real-browser test in the ui-react-layout project, built on the worker harness in packages/ui-react/tests/browser/offline/cold-capture/, serves worker v1, swaps the served worker script to v2, dispatches online, and observes updatefound, one controllerchange and exactly one reload when idle. Record in the PR that the test fails with the update() call removed.
  • registration.update() is available on every target (Chrome for Android 45, Samsung Internet 4, Firefox for Android 44 per MDN BCD); no feature detection beyond the existing "serviceWorker" in navigator guard is needed.

Verification

bun run test 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.
  • This changes a published package's behaviour, so it needs a changeset.

No contract surface: the hook's options and exports do not change.

Activity

  1. added
    type: fix
    area: ui-react
    priority: p2
    agent-ready
    on Oct 22, 2026
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-reactpriority: p2type: fix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions