Cache with a Service Worker
A service worker is the only way to make a repeat visit paint from disk in under 100 ms. It also gives you a cache you can corrupt, so versioning discipline is the whole job.
Procedure
Generate the precache manifest at build time so every hashed asset is listed, not hand-typed:
npx workbox-cli generateSW workbox-config.jsSet the strategy per route: precache the app shell,
stale-while-revalidatefor GET API data,network-firstfor volatile config,cache-firstfor immutable static hashes.Cap cache growth so a long-lived user does not fill the disk quota:
workbox.routing.registerRoute(({request}) => request.destination === 'image', new workbox.strategies.CacheFirst({cacheName: 'imgs', plugins: [new workbox.expiration.ExpirationPlugin({maxEntries: 80, maxAgeSeconds: 30*24*3600})]}));Beat the 24-hour cache update rule: ship a small
sw.jsthat changes each deploy (a build hash in a comment) so the browser byte-compares and updates.Handle the
activatelifecycle by deleting caches whose name is not in the current list; otherwise old shells accumulate.Guard
skipWaiting/clients.claimbehind an explicit reload prompt so users are not swapped mid-session.
Pitfalls
- Precaching before hashing is applied, so the manifest lists stale filenames and every fetch misses.
- Deleting all caches in
activatewithout excluding the newly created one, blanking the offline page. - A cache-first strategy on
/api/returning yesterday's prices forever. - Forgetting that the SW intercepts navigation, so a bad deploy serves a cached 200 for a route that now 404s.
- No cache size cap, letting a chatty image route quietly exhaust the origin quota and evict everything.
Verification
# DevTools -> Application -> Service Workers: reload twice offline
curl -sI https://shop.example.com/sw.js | grep -i 'cache-control'
sw.js must be served no-cache so the browser can byte-check it. Report the precache entry count and a cold-reload repeat-visit timing from the Network panel.