EasyCaching Strategies📖 Theory Question

What caching strategies can a service worker implement?

💡

Hint

Cache-first, network-first, stale-while-revalidate, cache-only, network-only — each suits different resource types

Full Answer

A service worker intercepts network requests and can apply different caching strategies per resource type:

Cache-first — serve from cache, fall back to network. Best for: versioned static assets (JS, CSS, fonts). Users get instant loads; stale risk is low since filenames change on update.

Network-first — try network, fall back to cache if offline. Best for: API responses where freshness matters. If network is slow, the user waits.

Stale-while-revalidate — serve cache immediately, update cache from network in background. Best for: content that can tolerate being one refresh cycle stale (blog posts, product listings).

Cache-only — only serve from cache, never hit network. Best for: pre-cached app shells in offline-first PWAs.

Network-only — never use cache. Best for: analytics, payment requests where you must not serve stale data.

// Workbox helper (used by Next.js PWA plugin)
registerRoute(
  ({ request }) => request.destination === 'image',
  new CacheFirst({ cacheName: 'images', plugins: [new ExpirationPlugin({ maxEntries: 50 })] })
);

More Caching Strategies Questions

EasyWhat is the Cache-Control header and what are its key directives?EasyWhat is stale-while-revalidate and why is it a good default for HTML pages?EasyWhat is content-based cache busting and how does it work?EasyWhat is ETags and conditional requests, and when does the browser use them?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint