Repository navigation
Description
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
ServiceWorkerRegistrationthatregister()resolves with. - Call
registration.update()whendocumentbecomes visible (visibilitychangewithvisibilityState === "visible") and onwindowonline. - 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
- Changing
skipWaiting()/clients.claim()or the precache cleanup (that is ui-sdk: a new service worker activates under a busy page and deletes the lazy chunks it still needs #2569). - Any change to the takeover,
controllerchange, update holds or the reload decision. - An "update available" banner or any new UI.
- A periodic timer while visible; foreground return and
onlineare the triggers. - Host changes (
service-worker.jscache headers, the registration URL).
Acceptance criteria
- With a registered worker, a
visibilitychangetovisiblecallsregistration.update()once; a second one within 30 minutes does not; one after 30 minutes does. Same foronline, sharing the same throttle. - A hidden
visibilitychangenever callsupdate(). - 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 fakenavigator.serviceWorkerand fake clock covers the four criteria above, and fails on the tree before the change. - A real-browser test in the
ui-react-layoutproject, built on the worker harness inpackages/ui-react/tests/browser/offline/cold-capture/, serves worker v1, swaps the served worker script to v2, dispatchesonline, and observesupdatefound, onecontrollerchangeand exactly one reload when idle. Record in the PR that the test fails with theupdate()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 navigatorguard is needed.
Verification
bun run test packages/ui-react/tests/render
bun run test:layout
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT: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