What is service worker caching strategy?
Answer
A service worker is a JavaScript file that runs in the background, intercepting network requests and enabling programmatic caching. Key caching strategies: Cache First: check cache first, fall back to network. Best for: static assets, rarely-changing resources. Network First: try network, fall back to cache if offline. Best for: HTML, API data that must be fresh but also work offline. Stale While Revalidate: serve from cache immediately (fast), then update cache in background. Best for: non-critical content that can be slightly stale. Cache Only: only serve from cache, never go to network. Best for: pre-cached resources that are definitely available. Network Only: bypass cache entirely. Best for: real-time data, analytics. Workbox (by Google): library that implements these strategies declaratively: registerRoute(({ request }) => request.destination === "image", new CacheFirst({ cacheName: "images" })). Service workers also enable offline functionality, push notifications, and background sync. Next.js, Gatsby, and Astro have Workbox integration via plugins.
Previous
What is code splitting and bundle optimization?
Next
What is HTTP/2 and HTTP/3 and how do they improve performance?