Lesson 3 of 9 · 24m
Precache, Runtime Cache, and Emergency Rollback
Design installation payloads, versioned runtime storage, cleanup, and worker escape hatches before a bad deployment forces the lesson.
A service worker is most dangerous when it is successful. Once installed, it can keep answering requests after your deploy pipeline has moved on. That is wonderful when it protects a train passenger from a tunnel, and terrible when it protects every user from your bug fix. Senior PWA work includes rollback design before you need rollback.
Precache the smallest reliable shell
Precaching installs known files before activation:
const BUILD = '2026.11.30-8f4d21c';
const SHELL_CACHE = `shell-${BUILD}`;
const SHELL = [
'/offline.html',
`/assets/app.${BUILD}.js`,
`/assets/app.${BUILD}.css`,
'/manifest.webmanifest',
];
await caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL));cache.addAll() is unforgiving: one 404 or network failure rejects installation. That is often correct for a shell because partial shells create impossible states. Keep the list small. Do not precache every image because someone said "offline" in a meeting.
Separate shell and runtime cache lifetimes
The shell is versioned by deployment. Runtime data is versioned by schema or policy:
shell-2026.11.30 immutable deployment files
read-api-v3 schema-aware API responses
media-v1 user-facing media with a size budget
outbox-v2 IndexedDB mutation queue, not Cache StorageThis separation avoids deleting user-visible data every time static assets change. If read-api-v2 entries have different field names, either migrate or version reads so old data cannot be interpreted incorrectly.
Clean up narrowly and keep an escape hatch
Activation cleanup should delete only obsolete cache names:
const keep = new Set([SHELL_CACHE, 'read-api-v3', 'media-v1']);
await Promise.all((await caches.keys())
.filter((name) => !keep.has(name))
.map((name) => caches.delete(name)));
await self.clients.claim();Also expose an emergency diagnostic that unregisters workers and clears Cache Storage. Do not clear IndexedDB casually; that may contain unsynchronized user work. Put the reset behind support tooling and document what will survive.
Server headers still matter
The worker script itself should be revalidated:
/sw.js Cache-Control: no-cache
/assets/app.8f4d21c.js Cache-Control: public, max-age=31536000, immutableMisconfigured server caching can make even a correct worker update slowly. Test installation failure too: throttle the network, block one asset, and confirm the old worker continues cleanly.
Field notes
- Never precache URLs that require per-user authorization unless the shell itself is intentionally personalized.
- Version names are operational data. Include them in support diagnostics and error reporting.
- A rollback can require serving a new worker whose only job is cleanup.
- Cache deletion during activation can race pages using those caches.
- Do not confuse storage eviction with uninstall. Browsers may evict data while registration remains.
Was this lesson helpful?