Service Worker Caching — When & Why
A service worker acts as a programmable proxy between your site and the network. Once registered, it intercepts every request and can serve cached responses — making repeat visits near-instant and keeping your app alive even without a connection.
Cache First vs Network First
Use Cache First for assets that rarely change (fonts, images, CSS). Use Network First for HTML and API data where freshness matters. Wrong choice = stale content or unnecessary network requests.
When to bump the version
Change v1 → v2 any time you deploy new code. This triggers the SW to re-install with fresh assets. Without versioning, users may be stuck on old cached files for days.
Scope matters
A SW at /blog/sw.js can only control pages under /blog/. To control your whole site, place sw.js at the root and keep scope as /.
Background Sync
If a user submits a form offline, Background Sync queues the request and replays it when connectivity returns. Essential for PWAs targeting unreliable networks.