Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท CDN and Caching

Cache-Control, Edge TTL and Browser TTL in Cloudflare

Master the Cache-Control header for Cloudflare: max-age versus s-maxage, no-store, private, stale-while-revalidate, ETags, Edge Cache TTL and Browser Cache TTL.

Cloudflare mostly obeys the instructions your origin sends in HTTP headers. The most important header is Cache-Control. Learn its directives once, and you can configure caching correctly on any server, whether or not you ever open the Cloudflare dashboard.

The directives that matter

A response header might read:

TEXT
Cache-Control: public, max-age=300, s-maxage=86400, stale-while-revalidate=60

Each part has a precise meaning.

Directive Meaning
max-age=N Any cache (browser and CDN) may reuse the response for N seconds
s-maxage=N Same, but only for shared caches like Cloudflare; overrides max-age there
public Shared caches may store it, even if the request had credentials
private Only the browser may store it; Cloudflare will not
no-store Nobody may store it at all
no-cache May be stored, but must be revalidated with the origin before each reuse
immutable The file will never change during its lifetime; browsers skip revalidation
stale-while-revalidate=N After expiry, serve the old copy for up to N seconds while fetching a new one in the background
stale-if-error=N If the origin fails, a stale copy may be served for N seconds

Two directives look similar but behave very differently: no-cache means "check first", no-store means "never keep". Use no-store for sensitive pages such as account settings or invoices.

A good set of defaults

For most sites, three header profiles cover nearly everything:

TEXT
# Fingerprinted assets (app.3f2a9c.js, logo.8d1e.png)
Cache-Control: public, max-age=31536000, immutable

# HTML pages that change a few times a day
Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=300

# Personal or sensitive responses
Cache-Control: private, no-store

With the middle profile, browsers keep a page for a minute while Cloudflare keeps it for an hour. When you publish new content you can purge the edge (purging) and visitors see it within a minute at worst.

Validation: ETag and Last-Modified

When a cached copy expires, it is not necessarily gone. If the response carried an ETag or Last-Modified header, the edge can send a conditional request (If-None-Match or If-Modified-Since). If nothing changed, the origin replies 304 Not Modified with no body, and the existing copy is refreshed for another lifetime. Cloudflare reports this as REVALIDATED. Sending ETag headers costs the origin almost nothing and saves bandwidth on every expiry.

What Cloudflare adds on top

Two dashboard-level settings can override the headers:

  • Edge Cache TTL decides how long Cloudflare keeps a response. You may use the origin's header when present, override it, or provide a fallback when the origin sends none. Without any header or rule, Cloudflare applies a default lifetime that depends on the status code (a successful response is kept for hours; errors such as 404 only briefly).
  • Browser Cache TTL decides what max-age Cloudflare sends to browsers. By default it respects the origin header. Setting a fixed value makes Cloudflare rewrite the header on responses.

Both are set per request pattern with cache rules. In summary:

flowchart LR
  O["Origin: Cache-Control"] --> CF["Cloudflare: Edge Cache TTL logic"]
  CF -->|"stores for edge TTL"| Cache[("Edge cache")]
  CF -->|"sends max-age = browser TTL"| B["Browser cache"]

Edge TTL governs the middle box; Browser TTL governs what the browser is told.

Computing the effective lifetime

The stand-in below parses a header and works out how long Cloudflare and the browser would keep a response, following the rules above. It is a simplified model, but it makes the precedence of s-maxage over max-age concrete.

JS Stand-in: parse Cache-Control

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try adding s-maxage=0 to a header and see how it changes the edge result while the browser lifetime stays put.

Stale content on purpose

stale-while-revalidate is a performance feature: the visitor who arrives just after expiry gets an immediate response (the stale copy) and the refresh happens in the background, so nobody waits on the origin. Cloudflare supports it, reporting UPDATING or STALE. stale-if-error adds resilience: if your origin is down for a few minutes, the edge keeps serving the last good page instead of an error.

JS Stand-in: stale-while-revalidate

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Practical rules

  • Send a long max-age and immutable only for fingerprinted files.
  • Use s-maxage to cache HTML at the edge for longer than in browsers, and purge when content changes.
  • Never put public on a response that varies per user; use private or no-store.
  • Set Vary carefully. Vary: Accept-Encoding is fine; Vary: Cookie or Vary: User-Agent on a page can make the cache useless (Cloudflare mostly ignores Vary except for a few headers, so serve different content on different URLs).
  • Check what the origin really sends: curl -sI https://origin.example/file.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which directive applies only to shared caches such as a CDN?

    s-maxage targets shared caches and overrides max-age for them.
  2. 2. What is the difference between no-cache and no-store?

    no-cache means check with the origin before reuse; no-store means do not keep a copy.
  3. 3. What does an origin 304 response to a conditional request do?

    304 Not Modified tells the cache its stored copy is still valid.
  4. 4. Which header profile suits a fingerprinted app.3f2a9c.js?

    The name changes when the content changes, so it can be cached for a year.

Summary

  • max-age serves all caches, s-maxage only shared ones; private and no-store keep content out of the CDN.
  • ETag and Last-Modified allow cheap revalidation (REVALIDATED).
  • Edge Cache TTL and Browser Cache TTL settings can override origin headers.
  • stale-while-revalidate and stale-if-error trade freshness for speed and resilience.

Next: Cache Rules