Skip to content

Cloudflare Cache Rules: what is cacheable, how TTLs work

Cloudflare caches a fixed list of static file extensions by default and ignores HTML. Cache Rules let you override that and customise the cache key. The choices around TTL, origin Cache-Control authority, and query-string handling decide your origin offload.

A team flips their site behind Cloudflare and assumes the orange cloud caches everything. A week later the origin is still serving every HTML request and the only thing on the edge is a few PNGs. This is exactly the default behaviour — Cloudflare caches a published list of static file extensions and leaves HTML alone. To cache HTML, JSON, or anything else you have to write a Cache Rule. This article walks the cacheability model, how Edge TTL and Browser TTL relate, when origin Cache-Control headers win, and how to customise the cache key without breaking hit rate.

How to verify

The fastest way to see what is being cached today is to look at cf-cache-status on a response. Run the same request twice and compare:

curl -sI https://example.com/styles/app.css | grep -iE '^(cf-cache-status|age|cache-control|etag)'
curl -sI https://example.com/styles/app.css | grep -iE '^(cf-cache-status|age|cache-control|etag)'

curl -sI https://example.com/                | grep -iE '^(cf-cache-status|age|cache-control)'

# List Cache Rules on the zone (replace IDs and token)
curl -sX GET \
  -H "Authorization: Bearer $CF_API_TOKEN" \
  "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint" \
  | jq '.result.rules[] | {description, expression, action_parameters}'

A first hit reads MISS, a second reads HIT. DYNAMIC means Cloudflare decided the response is not cacheable (the most common case for HTML out of the box). BYPASS means a rule or origin header told the edge to skip cache. EXPIRED means the object aged out and is being refreshed.

What’s happening

Cloudflare’s edge cache is keyed by URL and a small set of request attributes, and it only stores responses for content types it considers static. The default cacheable extension list covers the obvious ones — .css, .js, .jpg, .png, .gif, .webp, .svg, .woff, .woff2, .pdf, .ico, .mp4, .zip and similar. HTML, JSON, and anything without an extension is treated as dynamic by default. That keeps the platform safe for tenants who have not configured anything, because caching an authenticated /account page across users would be catastrophic.

Two TTL concepts run in parallel. Edge TTL is how long Cloudflare holds the object in the data centre cache. Browser TTL is the max-age value Cloudflare writes into the Cache-Control response header sent to the user agent. They are independent dials — a long Edge TTL with a short Browser TTL is the standard pattern for static assets you want to purge centrally rather than wait for every browser cache to expire.

Authority over TTL has a clear precedence. By default, the origin’s Cache-Control header wins — if the origin says max-age=3600, the edge respects that and uses it for both Edge TTL and Browser TTL unless overridden. A Cache Rule can override the Edge TTL, override the Browser TTL, or both — and it can choose to ignore the origin’s Cache-Control entirely. Knowing which dial is set where avoids the “I set a Cache Rule but the origin keeps winning” mystery.

RequestURL + headersCache Rulesmatch expressionset TTLs, key, bypassCache keyURL + selectedquery/headers/cookiesHITserve from edgeOrigin responseCache-Control: max-age=…Set-Cookie, VaryMISS / origin fetchEffective TTLrule wins if set,else origin Cache-Control
Cache Rules match first, then the cache key is built; on MISS the origin response sets TTL unless a rule overrides.

The procedure

  1. Decide what should be cached and key it explicitly. Open the dashboard at Caching → Cache Rules (or use the API ruleset above). Write one rule per content class: a rule for static assets, a rule for HTML you want cached, a rule for anything that must bypass. Avoid “one big rule for everything” — debugging it later is painful.

  2. Write the matcher. Cache Rules use the same expression language as WAF and Transform Rules. Example matchers:

    (http.request.uri.path matches "^/static/")
    (http.host eq "www.example.com" and http.request.uri.path eq "/")
    (http.request.uri.path matches "\\.(css|js|woff2)$" and not http.request.uri.query contains "preview=1")

    The combination of host, path, query, and request headers is enough for almost every case. Keep matchers narrow — a path-prefix match plus an extension match is more predictable than a long regex.

  3. Set Edge TTL. Three options, in order of preference:

    • Use cache-control header if present, bypass cache if not. Lets the origin drive TTL; if the origin sends no header, the response is not cached. Best when the origin is well-behaved.
    • Use cache-control header if present, otherwise use default. Falls back to a fixed TTL (e.g. one hour) when the origin is silent. Good for legacy origins.
    • Ignore cache-control header and use this TTL. Forces a value, regardless of origin. Use for HTML pages you control end-to-end and want cached for, say, 60 seconds at the edge with revalidation handled by purges.
  4. Set Browser TTL independently. Often you want a long Edge TTL (so the edge keeps serving) and a short Browser TTL (so users pick up purges quickly). Setting Browser TTL to 4 hours while Edge TTL is 1 month is a common pattern for fingerprinted static assets is unusual — usually it is the other way: long Browser TTL on fingerprinted assets, short Browser TTL on HTML. Pick consciously.

  5. Customise the cache key only when you must. By default Cloudflare keys on host + path + query string. You can:

    • Ignore the query string entirely (best for static assets where ?v=123 is a cache-buster you do not want fragmenting the cache).
    • Include specific query parameters (e.g. id, lang) and exclude the rest.
    • Include specific headers (e.g. Accept-Language for language-variant HTML, CF-Device-Type for mobile/desktop splits).
    • Include specific cookies (rare — usually a signal to bypass cache instead).

    Every dimension added to the key multiplies cache fragmentation. Adding Accept-Language with five languages gives you five times the cached objects per URL. Adding a per-user session cookie destroys cache hit rate entirely.

  6. Deploy and watch cf-cache-status. After the rule is live, hit the URL twice from different POPs and confirm the second request reads HIT. If it still reads DYNAMIC, the rule did not match — check expression syntax. If it reads BYPASS, an origin header (Cache-Control: private, Set-Cookie on the response) is forcing a skip. The cache analytics dashboard at Analytics → Cache breaks status by URL.

Common pitfalls

  • A Set-Cookie header on an otherwise-cacheable response will make Cloudflare bypass cache by default — strip the cookie at the origin or with a Transform Rule before relying on edge cache.
  • Cache-Control: private from the origin forces BYPASS regardless of your rule unless the rule explicitly ignores origin headers.
  • Putting the query string in the cache key when the site uses random analytics parameters (utm_*, gclid) means every visitor minted a fresh entry and your hit rate collapses; ignore those parameters or whitelist only the ones you actually vary on.
  • Caching HTML aggressively without a purge plan means stale content lingers — pair an HTML cache rule with the purge strategies workflow.
  • Cache Rules apply per-zone — a subdomain on a separate zone needs its own rules; do not assume inheritance.

In the engagements we run, the cache layer is where origin cost and tail latency get fixed in a single afternoon — but only after we audit which cf-cache-status codes are actually appearing on production traffic. Stack Harbor operates Cloudflare zones as part of Managed Operations: Cache Rules, purges, and the analytics that confirm the offload is real.