EasyCaching Strategies📖 Theory Question

What is stale-while-revalidate and why is it a good default for HTML pages?

💡

Hint

Serve the cached (stale) response immediately while fetching fresh in the background — eliminates latency without sacrificing freshness

Full Answer

stale-while-revalidate (SWR) is a Cache-Control extension that says: "serve the cached version immediately (even if stale), but simultaneously fetch a fresh version in the background."

Without SWR: After max-age expires, the next request blocks waiting for the network — the user sees latency on every cache miss.

With SWR:

  1. First request within max-age → served instantly from cache.
  2. Request after max-age but within SWR window → served instantly from stale cache; background revalidation starts.
  3. Next request → gets the fresh version (background fetch completed).
Cache-Control: max-age=60, stale-while-revalidate=600
# Serve fresh for 60s; serve stale (but refresh) for 10 more minutes

Why good for HTML: HTML pages change infrequently enough that showing a 60-second-old version is fine for most users. Eliminates the "cache miss spike" at deploy time when all users hit the origin simultaneously.

React Query / SWR library: implements the same pattern at the data-fetching layer — show cached data immediately while refetching in the background.

More Caching Strategies Questions

EasyWhat is the Cache-Control header and what are its key directives?EasyWhat is content-based cache busting and how does it work?EasyWhat caching strategies can a service worker implement?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