OneWebDesk

Stale-While-Revalidate Planner

Plan max-age and stale-while-revalidate values and output the header.

stale-while-revalidate (SWR) is a caching strategy that, right after a cached response expires, still serves the stale copy to the user instantly while quietly fetching a fresh version in the background. This removes the slow requests that pile up at the expiry boundary and lets you respond at cache speed almost every time while keeping content reasonably fresh.

This planner takes max-age, stale-while-revalidate, an optional stale-if-error, and a public/private scope, then assembles a ready-to-use Cache-Control header. It also converts each window into seconds and minutes and explains, on a timeline, how the response behaves depending on when a request arrives.

Cache-Control header
Cache-Control: public, max-age=60, stale-while-revalidate=600
Window conversions
Fresh window (0 to max-age)60s (1 min)
Background refresh window (swr)600s (10 min)
Total stale allowance (max-age + swr)660s (11 min)
Served on error (stale-if-error)
Behavior timeline
  1. 0 to 60s: fresh. The cache responds instantly with no origin call.
  2. 60 to 660s: serve stale immediately + revalidate origin in the background. No user wait.
  3. after 660s: SWR window closed. The next request does a synchronous revalidation (waits for origin).

What each directive means

In the Cache-Control response header, these directives tell caches (browsers and CDNs) how to treat the response.

  • max-age=N: how long (in seconds) the response is considered fresh. During this window the cache answers directly without asking the origin.
  • stale-while-revalidate=N: for N seconds after max-age expires, the cache may return the stale response immediately and, at the same moment, revalidate (re-fetch from origin) in the background. The user never waits.
  • stale-if-error=N: if the origin errors (5xx) or is unreachable, the cache may serve the stale response for up to N seconds past expiry, masking the outage. It is an availability insurance.
  • public / private: public allows shared caches like CDNs to store the response; privaterestricts storage to the end user's browser (per-user content).

The response timeline

Calling the moment a response is stored in the cache time 0, the cache behaves as follows depending on when the next request arrives.

  1. 0 to max-age: the fresh window. The cache responds instantly with no origin call.
  2. max-age to (max-age + swr): the background refresh window. The user gets the stale response immediately while the cache fetches a new copy from the origin. Perceived latency is zero.
  3. after (max-age + swr): the SWR window has closed, so a stale response is no longer allowed. The next request waits for a synchronous revalidation to complete.

So a short max-age with a long stale-while-revalidategives you "almost always fast responses plus steady background refresh." Pair it with the Cache-Control builder when assembling the full header, and design expiry times for temporary-access signed URLs with the signed URL expiry calculator.

Benefits and caveats

Key benefits of SWR:

  • It removes the "thundering herd" of cache misses and the slow first request at the expiry edge.
  • Nearly every response is served at cache speed, so TTFB stays stable.
  • Adding stale-if-error lets you ride out an origin outage on the last good copy.

Things to watch:

  • It does not guarantee users always see the newest copy. During the SWR window they may get data that is one step behind, so it is unsuitable for responses that need instant accuracy like prices or stock counts.
  • Not every browser or CDN supports SWR identically. Unsupported caches usually treat the response as fresh only up to max-age.
  • Background refreshes still produce origin requests. With a very long swr window, low-traffic resources may refresh rarely and serve increasingly stale responses.

Frequently asked questions

What happens with max-age=0, stale-while-revalidate=60?
The response is considered stale the moment it is stored, but for 60 seconds the cache serves the stale copy immediately while triggering a background revalidation on each request. In effect it always responds at cache speed yet refreshes within a minute, a common pattern for content that changes often but tolerates a one-minute lag.
Is stale-while-revalidate added to max-age or included in it?
It is added. The total period during which a stale response may be served is max-age + stale-while-revalidate. So max-age=60, swr=600 allows SWR behavior for 600 seconds past expiry (up to 660 seconds total).
Do I need stale-if-error?
It is optional. Add it if you want to ride out an origin (server) failure such as a 5xx on the last good response. It is useful for availability-sensitive pages and does not affect normal behavior.
Does this tool send my input to a server?
No. The header string assembly and window conversions are computed entirely in your browser; your input is never transmitted anywhere.
Can the CDN and the browser cache behave differently?
Yes. With public, both the CDN (shared cache) and the browser may store the response, but SWR support and timing can vary by implementation. Verify the actual response headers and behavior before going to production.

Related tools