Service Worker and Cache Storage API: Coordinating asset updates during live migration
0 reputation · 06 Mar 2020, 20:34 UTC
0 reputation · 06 Mar 2020, 20:34 UTC
The goal is to deploy a new version of a small HTML5 application without downtime while guaranteeing that all active pages immediately receive the freshly cached assets, avoiding mixed‑version serving.
Constraints include relying on self.skipWaiting() to promote a waiting worker and clients.claim() to take control of existing pages, but browser implementations vary in how quickly the waiting worker becomes active and whether a page refresh is required for the claim to affect cache lookups. This uncertainty raises the possibility that some requests may still hit the old cache after an update.
Does invoking self.skipWaiting() followed by clients.claim() in the activate event guarantee that all existing page controls instantly see the new cache across Chrome, Firefox, and Safari? If not, what additional steps are needed to prevent mixed‑version asset serving? Is a soft page reload sufficient to achieve consistency without introducing perceived downtime?
33275 reputation · 07 Mar 2020, 05:57 UTC
No. Invoking self.skipWaiting() followed by clients.claim() in the activate event does not guarantee that every open page immediately serves the new cache across Chrome, Firefox, and Safari.
clients.claim(), new network requests from that page are routed to the new worker, but the document’s in‑memory cache references and already‑loaded assets remain from the previous build.myapp-v1, myapp-v2). Store the build identifier in the service worker scope and use it for all caches.open() calls.cache.addAll(); if any request fails, the whole step aborts, leaving the old cache untouched.activate, delete superseded caches after the new worker has taken control of all in‑scope clients, or use a controllerchange listener to delete them once no old pages remain.controllerchange event on navigator.serviceWorker. Once fired, issue a single location.reload() guarded by a flag so each page re‑fetches its document under the new worker.A single location.reload() after controllerchange is the practical way to achieve consistency without perceiving downtime; the user sees a brief re‑render but no full‑reload loop. However, the reload is not instantaneous and depends on the browser’s update‑check cycle. If your requirement is sub‑second, zero‑notice transition, you must version the cache and delete old caches only after all clients have been claimed, then rely on the reload pattern described above.
Please confirm whether you are using a static cache name or a versioned one, so I can tailor the exact delete‑order and reload guard logic.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.