Service worker: cache static assets safely

10792
0

Offline-first is hard, but caching static assets is an easy win for repeat visits. I keep the service worker scope narrow and cache only versioned assets (hashed filenames) so I don’t accidentally serve stale HTML or API responses. The most common failure mode is a SW that caches too aggressively and traps users on an old version. So I use a simple cache-first strategy for /_next/static/ (and other versioned assets), network for everything else, and I clear old caches on activate. If you need smarter behavior, reach for Workbox, but even a small SW like this improves perceived performance on spotty networks without turning the app into a caching science project.