Cloudflare
Last reviewed: 2026-10-27 · Applies to: Cloudflare CDN (Cache Rules, Cache Response Rules, Tiered Cache, Cache Reserve), Workers (Workers Caching, Cache API, KV), all plans unless noted
Contents
- When to read this
- How to detect
- Default recipe
- Rules and gotchas
- Default behavior
- Configuration surface
- Cache keys
- Purge and invalidate
- Tiering, stale serving, Cache Reserve
- Workers caching options
- Status headers
- Version notes
- Verify
- Sources
When to read this
When the zone is proxied by Cloudflare (orange cloud), the app runs on Workers/Pages, or cf-cache-status appears in responses. Vendor-neutral concepts: references/layers/cdn-concepts.md. Workers KV and edge data: references/layers/serverless-edge-data.md.
How to detect
wrangler.toml/wrangler.json(c), @cloudflare/* or @opennextjs/cloudflare deps, Terraform cloudflare_ruleset/cloudflare_zone, response headers cf-cache-status, cf-ray, server: cloudflare.
Default recipe
Origin headers (preferred control):
# Public HTML/API
Cache-Control: no-cache
Cloudflare-CDN-Cache-Control: max-age=300, stale-while-revalidate=3600, stale-if-error=86400
Cache-Tag: page:home,section:news,all
ETag: "<hash>"
# Personalized
Cache-Control: private, no-cacheCache Rules (dashboard, API or Terraform; full examples in assets/snippets/cdn/cloudflare-cache-rules.md):
- Rule "bypass private":
http.cookie contains "session=" or starts_with(http.request.uri.path, "/account") or starts_with(http.request.uri.path, "/api/private")→ Bypass cache. - Rule "cache public HTML":
http.host eq "www.example.com" and http.request.method eq "GET"→ Eligible for cache; Edge TTL "use cache-control header if present"; cache key: ignore query string order, Cache Deception Armor on.
Then enable Smart Tiered Cache (free) and, for cloud origins, the cloud region hint.
Rules and gotchas
- HTML and JSON are never cached by default; caching is by file extension, not MIME type. You need a Cache Rule ("Eligible for cache") to cache HTML.
- Migrating from Page Rules: "Eligible for cache" means Cache Everything by default. Matching Cache Rules stack; order bypass rules deliberately.
- Stale serving needs
stale-while-revalidatefrom the origin and is disabled when the same header also carriess-maxage,must-revalidate,proxy-revalidateorno-cache(you getEXPIREDinstead ofUPDATING). UseCloudflare-CDN-Cache-Control: max-age=…, stale-while-revalidate=…and keepCache-Controlfor browsers. Always Online also disables SWR/SIE. Set-Cookieon the response →BYPASSby default.Cache-Control: private="Set-Cookie"keeps the response cacheable and drops only that header. Better: don't send cookies on cacheable routes.- A request with
Authorizationis cached only if the response haspublic,s-maxageormust-revalidate. SettingCloudflare-CDN-Cache-Controlturns Origin Cache Control on, so this rule applies. - An Edge TTL rule "ignore cache-control and use this TTL" overrides
no-store/privateand stripsSet-Cookie: it will cache personalized pages unless a bypass rule runs first. - Response Header Transform Rules that change
Cache-Controldon't affect caching (the decision happens first). Use Cache Response Rules. - Cache Deception Armor only activates when freshness is heuristic (no explicit
Cache-Control). Correct origin headers are the real protection. - Use integer values in cache directives (docs disagree on whether floats are ignored or rounded).
- Purge rate limits (hostname/tag/prefix/everything): Free 5 requests/min (bucket 25), Pro 5/s, Business 10/s, Enterprise 50/s; 100 items per request. Batch and debounce webhook purges.
- Sort query string is off by default and can break order-dependent endpoints.
Default behavior
- Cached extensions include css, js, images, fonts, media, archives, pdf (not html/json).
robots.txtis cached by default. - Without
Cache-Control/Expires: 200/206/301 cached 120 min; 302/303 20 min; 404/410 3 min; others not cached. - Max object: 512 MB Free/Pro/Business; 5 GB Enterprise default. Over the limit →
BYPASS. BYPASSwhen: over size;no-storeor bareprivatein any of the three cache headers;Set-Cookie;Vary: *; requestAuthorizationwithout an allowing directive.no-cache/max-age=0with Origin Cache Control on (always on for Free/Pro/Business): stored and revalidated (REVALIDATED/EXPIRED); with OCC off (Enterprise default) →BYPASS.- Header precedence:
Cloudflare-CDN-Cache-Control(Cloudflare only, stripped) >CDN-Cache-Control(passed downstream) >Cache-Control. A Cache Response Ruleset_cache_controlbeats all of them.
Configuration surface
- Cache Rules decide eligibility, edge/browser TTL, cache key, serve-stale, Cache Reserve eligibility. Fields include
http.request.uri.path,http.request.uri.query,http.cookie,http.host,http.request.headers,http.request.uri.path.extension. - Cache Response Rules (March 2026, phase
http_response_cache_settings) act on the origin response before caching: set or overrideCache-Control(optionally Cloudflare-only), manage cache tags (including converting another CDN's tag header), stripSet-Cookie. They win over Cache Rules on conflict; last match wins. They can make contentno-storebut can't make it eligible. - Workers
fetch()can override cache settings per request via thecfobject. - Terraform
cloudflare_rulesetwithset_cache_settings:edge_ttl { mode = "override_origin" | "respect_origin" ... },browser_ttl,serve_stale { disable_stale_while_updating },respect_strong_etags,cache_key.
Cache keys
| Option | Plans |
|---|---|
| Cache Deception Armor, cache by device type, ignore query string, sort query string | All |
| Custom query string include/exclude, headers, cookies, host, user features (device/geo/lang) | Enterprise |
Vary honored in Cache Rules with per-header normalize/passthrough/bypass (July 2026) |
All |
Varysupport: originVaryheader names join the key.normalizecollapsesAccept-Language: en-US, fr;q=0.8andfr;q=0.8, en-GBtoen,fr; limits: ≤ 10Acceptmedia types, ≤ 20 languages, ≤ 50 header entries.- Non-Enterprise plans can't exclude individual params: strip
utm_*with a URL rewrite (Transform Rule/Snippet) or move variants into the path. - Prefetch URLs always use the default key (incompatible with custom keys). Cloudflare Trace shows the computed key.
Purge and invalidate
- All purge methods on all plans (since April 2025): URL, hostname, tag, prefix, everything. Instant Purge ≈ 150 ms globally.
- Invalidate (soft purge), September 2026:
POST /zones/{zone_id}/invalidate_cache, same body as purge. Marks stale; next request revalidates (304 reuses the stored body, needs originETag/Last-Modified); can serve stale meanwhile or on origin errors. Shares purge rate limits. Use hard purge only when content must never be served again.
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/invalidate_cache" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \
--data '{"tags":["product:123"]}'Cache-Tag: comma-separated, ≤ 16 KB total (~1,000 tags), case-insensitive, stripped before visitors; API tag ≤ 1,024 chars.- URL purge on keys that include headers must pass those header values. Cache Rules matching only
GETdon't match purge requests: addhttp.request.method eq "PURGE"if rules depend on method. - Purging a redirect URL purges the redirect, not its destination. With Tiered Cache, the first post-purge status may be
EXPIRED.
Tiering, stale serving, Cache Reserve
- Smart Tiered Cache: free, all plans; falls back to Generic when the origin location is unknown; cloud region hint (April 2026, e.g.
aws/us-east-1); R2 origins get an upper tier near the bucket. Packaged with Regional Tiered Cache and Cache Reserve as "Smart Shield". Not compatible with Regional Services (data localization). - Async SWR (February 2026): first request after expiry gets stale with
UPDATING, revalidation runs in the background. - Cache Reserve: R2-backed persistent upper tier, paid; eligible objects need a freshness TTL ≥ 10 h and
Content-Length; 30-day retention reset on access; image-transformation variants aren't eligible (originals are); no Origin Range Requests. Purge forces a full miss; invalidate allows 304 reuse.
Workers caching options
| Need | Use |
|---|---|
| Cache whole Worker responses | Workers Caching ("cache": { "enabled": true } in wrangler config): read-through, tiered, collapses requests, purge via ctx.cache.purge()/ctx.cache.invalidate() |
| Cache a subrequest or fragment | Cache API (caches.default): explicit put, data-center local, no tiering, no collapsing, cache.delete only local |
| Read-mostly config/data | Workers KV (eventually consistent, up to 60 s; min cacheTtl 30 s) |
| Strong consistency | Durable Objects/D1, with outputs behind Workers Caching |
Workers Caching traps: the host is not in the key (same cache for all domains); the Worker version is in the key by default, so every deploy starts cold (cache.cross_version_cache shares across versions); zone Cache Rules/Cache Response Rules don't apply; purges always use Free-tier rate limits; no pre-warm API. For tenants on the same path, pass tenant (or user) ID via ctx.props, which is part of the key.
Authenticated gateway pattern: an uncached entrypoint authenticates, strips Authorization, and calls a cached entrypoint that returns Cache-Control: public, max-age=60.
Also see Hyperdrive (DB query caching) in references/layers/database-orm.md and APO for WordPress in references/cms/wordpress.md.
Status headers
cf-cache-status: HIT, MISS (cacheable, not stored yet), EXPIRED, STALE, UPDATING (stale served during async revalidation), REVALIDATED, BYPASS (uncacheable), DYNAMIC (not eligible, no lookup). Compute hit ratio over cacheable requests without BYPASS/DYNAMIC. Logpush fields: CacheTieredFill, CacheLockWaitedMs.
Version notes
- 2025-04: all purge methods on all plans. 2026-01: KV minimum
cacheTtl30 s. 2026-02: async SWR. 2026-03: Cache Response Rules. 2026-04: Smart Tiered Cache cloud region hint. 2026-05: cache on Pingora; consistentBYPASS. 2026-06:cf.varyfor Workers subrequests. 2026-07:Varyin Cache Rules. 2026-09: invalidate (soft purge) for zones and Workers Caching.
- Page Rules with "Cache Level: Cache Everything" (replaced by Cache Rules).
- Transform Rules to change
Cache-Controlfor caching purposes (never affected caching). - Expecting
MISSto mean "uncacheable" (before May 2026 oversize objects showedMISSforever).
Verify
curl -s -o /dev/null -D - https://www.example.com/ | grep -iE 'cf-cache-status|age|cache-control' # MISS then HIT
curl -s -o /dev/null -D - -H 'Cookie: session=x' https://www.example.com/ | grep -i cf-cache-status # BYPASS or DYNAMIC
curl -s -o /dev/null -D - https://www.example.com/account/x.css | grep -iE 'cf-cache-status|cache-control' # never HIT