Cloudflare是否支持stale-while-revalidate?设置对应头未命中缓存咨询
stale-while-revalidate on Cloudflare Hey there! Cloudflare definitely supports the stale-while-revalidate directive—let’s unpack why your current setup isn’t producing the cache hits you expect.
First, let’s break down your Cache-Control header:
public, max-age=0, stale-while-revalidate=30
The core issue here is likely the max-age=0 value. While stale-while-revalidate lets caches return stale content after the initial expiration window, max-age=0 tells Cloudflare the response is immediately expired the moment it’s cached.
In theory, this should still let Cloudflare serve the stale response for 30 seconds after the first request while revalidating in the background—but there are a few key catches:
- Cloudflare’s edge logic might treat
max-age=0as a signal to avoid caching entirely in some scenarios, especially if other settings (like page rules or cache level configurations) override your header. - You might be misinterpreting "cache hits": when using
stale-while-revalidate, the cache status will show asSTALE(notHIT) when returning expired content. If you’re only looking forHITin theCF-Cache-Statusheader, you’ll miss these valid stale responses.
Fixes to Try:
Adjust
max-ageto a positive value
If your use case allows, setmax-ageto a small non-zero number (e.g.,public, max-age=10, stale-while-revalidate=30). This tells Cloudflare the response is fresh for 10 seconds, then can be served stale for another 30 while revalidating. You’ll seeHITstatuses during the fresh window, andSTALEstatuses afterward.Verify your Cloudflare cache configuration
Double-check if you have any page rules set to "Cache Level: Ignore Cache Control"—this would override your custom Cache-Control header. Make sure your cache level is set to "Standard" or "Cache Everything" (if appropriate) to respect your header directives.Inspect the
CF-Cache-Statusheader
Use browser dev tools orcurlto check response headers. ASTALEstatus meansstale-while-revalidateis working as intended, even if it’s not theHITyou initially expected.
内容的提问来源于stack exchange,提问作者Dan Fabulich

