Domains & launch

Deep dive: Caching with your own Cloudflare

Published Updated

If your domain runs through your own Cloudflare, you can choose how long your visitors' browsers keep the files your pages load, such as your stylesheet and fonts. Keeping them for longer, a month or more, makes repeat visits a little faster and clears PageSpeed's cache warning. In return, you or your AI agent version those files, so that a change still reaches everyone.

Is this for you?

It is worth doing when all three of these are true:

If not, the standard setup is the right one and there is nothing to do.

How caching works

Three places can hold a copy of a page or file: your visitor's browser, Cloudflare, and our servers. Our servers always have the latest version from the portal, and ask for it to be kept for 6 minutes.

Your visitor's browser Cloudflare Our servers on their phone or laptop your account HubPeople Pages Files stylesheet, fonts and other files keeps a copy 6 minutes no copy passes the request on keeps a copy Standard 6 minutes Cloudflare default 4 hours With your rule longer Browser TTL keeps a copy 6 minutes, following our servers Edge TTL latest version asks for 6 minutes on pages and files asks every time when expired when expired A portal change A purge does not reach the browser's copy stays until its time runs out
A purge clears Cloudflare's copies. A copy already in a visitor's browser stays until its own time runs out, which is why the time you choose for files matters.

So there are two clocks:

With the standard setup, Browser Cache TTL set to Respect Existing Headers, both follow our 6 minutes. Anything you change in the portal reaches your visitors within minutes. If Browser Cache TTL is left on Cloudflare's own default, browsers keep files for 4 hours instead, though pages still follow our 6 minutes.

What a longer cache gives you, and what it asks

It gives youIt asks of you
  • Faster repeat visits. A returning visitor's browser uses its own copy of your files straight away, instead of first checking with the server. Pages draw sooner, most noticeably on phones.
  • No cache warning in PageSpeed. "Use efficient cache lifetimes" goes once files are kept for at least 30 days. It is marked Unscored, so your score itself will not change.
  • Set and forget. Nothing to look after while your files stay the same.
  • First visits are no faster. A new visitor has no copy yet.
  • A new version with every change. Change a file without changing its version in your pages, and returning visitors keep the old one for as long as your rule says.
  • No undo. A purge will not put a missed version right: it clears Cloudflare's copy, not the copies already in browsers.

Step 1: version your files

A version is a short code on the end of a file's address, such as global.min.css?v=3f9a1c2b7e. Our servers ignore it and always send the current file. Browsers and Cloudflare treat a new code as a new file and fetch it. That is what makes a long cache safe.

Do this before adding the rule, for every file you want kept longer. A file can be named in a few places, and every one needs the version:

A file's address must be exactly the same everywhere it appears, a font in the stylesheet and in a preload, for example. If two differ, browsers download it twice.

If your AI agent looks after your pages, ask:

Version the files my pages load from Brand Assets, such as the stylesheet and fonts, so they can be cached for a long time. Add ?v= and a short fingerprint of each file's contents to every link to them, on every page and inside other files, such as the font addresses in the stylesheet and any preloads, keeping each file's address identical everywhere. From now on, whenever you change one of these files, upload it first, then update its version everywhere it is linked.

To check, open one of your pages and press Ctrl+U (View page source). The stylesheet link should end in ?v= and a code.

Step 2: add the Cache Rule

In your Cloudflare account, open your domain, then:

  1. Go to Caching › Cache Rules and press Create rule.
  2. Give it a name, for example Fonts and stylesheet, a year (2).
  3. Under When incoming requests match, set Field to URI Path, Operator to wildcard, and Value to /fonts/* (3).
  4. Press Or, then add URI Path, wildcard, and your stylesheet's address, for example /global.min.css (4). Add one more condition, joined with Or, for each other file or folder you have versioned.
  5. Check the Expression Preview (5) reads like this: (http.request.uri.path wildcard r"/fonts/*") or (http.request.uri.path wildcard r"/global.min.css")
  6. Under Cache eligibility, choose Eligible for cache (6).
  7. Under Browser TTL, press Add setting, choose Override origin and use this TTL, and pick how long (7). 1 month or more clears PageSpeed's warning; this example uses 1 year.
  8. Leave everything else as it is and press Deploy at the bottom of the page.
Cloudflare's New Cache Rule form. Markers show the rule name (2), the first condition URI Path wildcard /fonts/* (3), Or with the second condition URI Path wildcard /global.min.css (4), the expression preview (5), Eligible for cache (6), and Browser TTL set to Override origin, 1 year (7).
The finished rule: the two conditions (3, 4), Eligible for cache (6) and a year for browsers (7).
Use URI Path, not URI Full. URI Full is the whole address, including https:// and your domain, so /fonts/* would never match it. If your fonts live in a different folder, use that folder's name instead.

Changing a file later

  1. Upload the changed file first.
  2. Then change its version everywhere it is linked. For a font, that means the stylesheet and any preload, which changes the stylesheet too, so give the stylesheet a new version on every page.

Your visitors pick up the new pages within minutes, and the new file with them.

What goes wrong
  • Pages changed before the file. Visitors fetch the new address before the new file is there, get the old file, and keep it for as long as your rule says.
  • A page missed. Returning visitors to that page keep the old file.
  • Caching Level changed. Under Caching › Configuration, Caching Level must stay on Standard, which treats a new ?v= as a new file. Ignore Query String would undo your versioning.

What about your pages and images?

The rule only matches the files you name. Your pages and images work differently, and need nothing from you:

Caching pages for longer at Cloudflare would hold back your edits and those refreshes until you purged, so leave pages out of the rule.

Check it is working

In Chrome, open your site, press F12 and choose Network. Untick Disable cache and tick Keep log (1), then load a page and click through to another.

Chrome's Network panel. Keep log is ticked and Disable cache unticked (1). On the first page the stylesheet is downloaded at 10.5 kB (2). On the next page the same stylesheet comes from memory cache in 0 ms (3).
The first page downloads the stylesheet (2). The next page uses the browser's own copy, memory cache, 0 ms (3).

Click the stylesheet, then Headers, and look at Response Headers:

The response headers for a stylesheet served through Cloudflare. Age is 19 (1). Cache-Control is public, max-age=360 (2), noted as 6 minutes on the standard setup, and max-age=31536000, a year, with the Step 2 rule. Cf-Cache-Status is HIT (3).
This example site has no rule, so it shows our 6 minutes (2). With your Step 2 rule it shows a year.
  1. Age: how many seconds Cloudflare has held its copy.
  2. Cache-Control: how long browsers may keep the file, in seconds. The example shows max-age=360, 6 minutes, because that site has no rule. Once your Cache Rule from Step 2 is in place, the files it matches show your time instead: max-age=31536000 for a year, max-age=2592000 for a month. That is how you know the rule is working.
  3. Cf-Cache-Status: what Cloudflare did. HIT means it used its copy.
Cf-Cache-StatusMeaning
HITServed from Cloudflare's copy
MISSCloudflare had no copy yet, so it asked our servers
REVALIDATEDCloudflare checked with our servers, nothing had changed, so it used its copy
EXPIREDCloudflare's copy was out of date, so it fetched a fresh one
DYNAMICNot cached at all, which is how your pages show

Quick checklist

WhereWhat
Your pages and filesA ?v= on every link to a file you version, wherever it appears
Caching › Cache RulesOne URI Path condition for each versioned file or folder, joined with Or; Eligible for cache; Browser TTL, Override origin, 1 month or more (the example uses 1 year)
Caching › ConfigurationCaching Level Standard; Browser Cache TTL Respect Existing Headers
When a file changesUpload it first, then give it a new version everywhere it is linked

Further reading

Cloudflare's own documentation on the settings this guide uses:

And Google's page on the PageSpeed warning: Use efficient cache lifetimes.