Serve Stale Content limits on origin error codes and Cache-Control interaction
0 reputation · 11 Jun 2021, 09:25 UTC
0 reputation · 11 Jun 2021, 09:25 UTC
Cloudflare's Serve Stale Content setting controls whether a cached response is returned when an origin is unreachable or returns an error, but the boundary between stale hits and fresh misses depends on both the error class and any origin Cache-Control directives. The feature separates background revalidation from origin failure handling, and directives such as stale-while-revalidate and stale-if-error can extend stale-serving windows independently of the cache rule toggle.
The exact behavior is unclear when an origin returns a 4xx status rather than a 5xx, because stale content is generally not served for client errors, yet a cached 404 may still be treated as a valid fresh asset. It is also uncertain whether a plan-level or Cache Rules toggle overrides, supplements, or is overridden by origin stale-if-error values, and whether the legacy Cache settings page and newer Cache Rules evaluate these directives differently. Behavior differences across Cloudflare plans add further uncertainty for operators designing a consistent stale-serving policy.
Under these conditions, does Cloudflare treat a cached 404 as a fresh response rather than a stale candidate, and do origin stale-if-error directives apply when the dashboard Serve Stale Content option is disabled? How should Cache Rules and legacy cache settings be reconciled to avoid conflicting stale-serving outcomes across plans?
34775 reputation · 11 Jun 2021, 10:00 UTC
Cloudflare treats a cached 404 as a fresh response (not a stale candidate) as long as its TTL has not expired; it will not serve a previously cached 200 OK in place of a 404 unless the origin returns a 5xx error or times out.
When the dashboard Serve Stale Content toggle is disabled, the stale‑if-error Cache‑Control directive can still trigger stale serving, but only for errors that Cloudflare considers “origin failure” (5xx or timeout). A 404 does not satisfy that condition, so stale‑if-error has no effect on a 404 response.
stale‑if-error directive is evaluated per‑response and works independently of the global toggle.Because Cloudflare distinguishes between background revalidation (stale‑while‑revalidate) and failure recovery (stale‑if-error), the latter only activates when the origin is deemed unavailable. A 4xx status is interpreted as an intentional client‑error signal, not a failure, so the directive is ignored for those responses. Plan‑level entitlements affect the maximum stale‑serving window (e.g., Free plan limited to 1 hour, Enterprise can be longer), but they do not change the 4xx vs 5xx classification.
Cache‑Control: stale‑if-error=... (or use the rule’s “Serve stale content” action).stale‑if-error header that Cloudflare will treat as a failure signal.stale‑if-error value does not exceed it.If you are on a Free or Pro plan and need to serve stale longer than the plan‑allowed maximum, please confirm your current plan tier; this may change the recommendation to upgrade or to adjust the stale‑if-error value.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.