A service worker update can install while an older worker still controls open pages. The new worker normally waits until those pages close; an application can request earlier activation, but then a page may run old JavaScript under a new fetch controller. Treat worker activation as a release transition, not a cosmetic refresh. The page owns unsent edits and user attention; the worker owns fetch handling, not a dependable in-memory queue. A safe update path records local draft state before offering a reload and retains assets needed by older tabs.
Service Worker Activation and Unsent Work
Working case
Inspector 47 is editing case 62 offline when release 29 installs. The active page uses a draft schema that release 29 can still read, but its pending edit has not reached the server. The UI reports that an update is ready and offers a later action. Once the draft transaction commits locally, the inspector can choose to reload. The new worker activates and the page checks which release now controls it before resuming synchronization. If the inspector dismisses the prompt, the old tab remains usable; the deployment retains its hashed assets for that tab.
Implementation boundary
function mayActivateUpdate(draft, compatibility) {
return draft.localCommitComplete && (draft.unsentCount === 0 || compatibility.mixedClientSafe);
}
console.log(mayActivateUpdate({ localCommitComplete: true, unsentCount: 2 }, { mixedClientSafe: false }));
// Output: falseListen for a waiting worker and state changes on the registration, then separate install success from activation. Record the draft in IndexedDB before acknowledging an update choice. Decide whether to request early activation only when the client and API compatibility window supports mixed versions. On controller change, reload at most once for the selected transition; do not create a loop across worker updates. Keep worker fetch handlers stateless across restarts and store durable outbox entries in the database. A worker can be stopped between events, so a timer or global variable in it is not a reliable retry queue. Keep a server-rendered route available when script or worker registration fails.
Cost and boundaries
Precache size adds installation transfer and consumes origin storage. Keeping two releases briefly costs extra bytes but protects open tabs from missing chunks. A prompt on every minor release can interrupt field work; group compatible releases and surface the update when the user can act. Pending outbox size should be bounded by records and total bytes. Activation checks are small, while a full reload costs network, parse, and focus restoration. Measure update acceptance, failed chunk loads, unsent draft duration, and the oldest active client release.
Failure trace
The app calls early activation during an offline edit and immediately deletes every old cache. The old page later imports a lazy module that no longer exists and cannot submit its draft. Simulate this sequence with release 28 open while release 29 installs. Confirm the edit remains in IndexedDB, the old chunk still loads, and the new controller does not mark the server write complete. Test multiple tabs with different releases, a worker installation failure, a browser restart before synchronization, and a user who never chooses to reload. None should silently discard the local change.
Verification
- A waiting worker does not clear unsent edits.
- Controller changes cannot trigger reload loops.
- Old open tabs retain their required hashed assets.
Practice drill
Create a draft generation 47 and a waiting release 29. Before offering reload, commit the generation and count pending outbox entries. Activate the worker only after the selected transition rule passes. Kill the worker process between two simulated events, restart the page, and recover the outbox from storage. Finally, remove an old chunk in staging to show why the asset-retention window must be part of the release contract.
Decision note
An update can wait; a field edit must not vanish because the fetch controller changed.
Common Mistakes
- Treating a worker global variable as durable outbox storage.
- Deleting prior assets as soon as a new worker activates.
- Equating local commit with server acknowledgment.
Related lessons
Offline Storage and Upgrade Safety; IndexedDB Version Changes and Blocked Tabs; Browser Storage Quota, Eviction, and Recovery; Private Offline Cache and Account Switch; Artifact Manifest, Rollout, and Rollback Coherence; Offline Sync and Conflict Policy.
Connected practice
Build Project: offline case review across upgrades and review Web Development: offline and browser-key decisions quiz.
