Skip to content
Go back

Two Cache Rules Matched One File — And Cloudflare Applied Both 🧾

(updated )

One _headers file. Two hosting vendors. The file moved across without a single character changing.

The behaviour did not.

That is the entire bug, and it embarrassed me because nothing in the file was wrong. Every rule was individually correct and individually sensible. The problem was what happens when two of them match the same URL, and I had never asked that question, because on the old vendor it had never come up.

The file, and why it looked fine

When the site moved off Netlify I brought public/_headers along unchanged. It had four blocks: a catch-all, then three specific ones.

/*
  Cache-Control: public, max-age=0, must-revalidate

/_astro/*
  Cache-Control: public, max-age=31536000, immutable

/diagrams/*
  Cache-Control: public, max-age=86400, must-revalidate

/assets/*
  Cache-Control: public, max-age=86400, must-revalidate

Read top to bottom, it is a story about specificity. Everything revalidates, except the things I know are safe to keep. Astro puts a content hash in every asset filename — Footer.GBl5a7uF.css — so a file whose contents change gets a brand new name every time. Caching a name like that for a year is not a risk, it is the point of the hash.

Diagrams and /assets/ are not fingerprinted. They can change in place, so they get a day and then a revalidation.

Nothing there is exotic. It is the same file thousands of Astro sites ship.

The assumption I did not know I was making

I wrote that file believing the specific rule wins. /_astro/* is more precise than /*, so for an asset under /_astro/, the precise one should decide and the catch-all should quietly step aside.

I never checked that. I want to be clear that I did not check it, because the checking is the whole article.

Here is Cloudflare’s documentation on _headers, and I am quoting it because the sentence is short and I had to read it three times:

An incoming request which matches multiple rules’ URL patterns will inherit all rules’ headers.

And, one paragraph later:

If a header is applied twice in the _headers file, the values are joined with a comma separator.

Both of my rules matched /_astro/Footer.GBl5a7uF.css. So the response inherited both, and the duplicate header was comma-joined into a single line carrying two instructions that cannot both be true.

Diagram showing how Cloudflare resolves overlapping header rules. On the left, a request for a fingerprinted CSS asset. In the middle, two rule boxes stacked vertically: a catch-all rule setting Cache-Control to public, max-age=0, must-revalidate, and a fingerprinted-asset rule setting Cache-Control to public, max-age=31536000, immutable. Both rules are marked as matching the request. Two arrows converge on a response box on the right showing the two Cache-Control values joined by a comma into one header line, with a note that the two directives contradict each other. A caption underneath explains that Cloudflare inherits headers from every matching rule rather than picking the most specific one, and that removing the catch-all leaves one value per path.

What that actually does

A Cache-Control header is not a vote. There is no tally where the longest max-age wins. When a response carries max-age=0, must-revalidate and max-age=31536000, immutable in the same field, you have handed every cache in the chain a contradictory instruction and left the interpretation to whoever reads it first.

The practical result is the conservative one. A cache that cannot reconcile two directives will revalidate. So my fingerprinted CSS was not cached for a year. It was checked with the origin, every time, exactly as it had been before I wrote the file.

Which is the part that stings. I had done the work — identified the right paths, understood fingerprinting, written the rules — and then wrote one block at the top that undid it for the files that mattered most.

The catch-all was doing a job that was already done

The reason /* was in the file at all is that I wanted a guarantee: HTML must never be served stale, because a stale HTML document means a published post that nobody can see.

Cloudflare already guarantees that. From that same documentation, the default header on a static asset response is:

Cache-Control: public, max-age=0, must-revalidate

That is the catch-all. It is what you get for free, on every file, before _headers is even read. My first block was not adding safety. It was restating a default and then dragging that default onto files I had just exempted from it.

So the fix was subtraction:

/_astro/*
  Cache-Control: public, max-age=31536000, immutable

/diagrams/*
  Cache-Control: public, max-age=86400, must-revalidate

/assets/*
  Cache-Control: public, max-age=86400, must-revalidate

Three rules. Each path matches exactly one of them. HTML matches none, and falls through to the platform default that was already correct.

Deleting lines to fix a caching problem feels wrong the first time. It is the correct move more often than it feels.

What the live site says now

Rules are only interesting if the response agrees. Checked against production:

PathCache-Controlcf-cache-status
/_astro/Footer.GBl5a7uF.csspublic, max-age=31536000, immutableMISS, then HIT
/public, max-age=0, must-revalidateHIT
/diagrams/vendor-chain.svgpublic, max-age=86400, must-revalidateMISS

One value per path, no joins, no contradictions. The second request for the CSS asset comes back a HIT, which is the behaviour the fingerprint was supposed to buy me two weeks earlier.

One caveat before that column talks you into trusting it: cf-cache-status is an indicator, not a proof. It reports the cache decision made for that particular request at that particular edge, and I have watched a first request report MISS and the identical second request report HIT with nothing deployed in between. Treat it as a strong hint. The header value itself is the fact; the cache status is the weather.

The bit worth keeping

A config file is not a standard. It is one vendor’s implementation wearing a syntax that happens to look portable — and _headers is unusually good at hiding that, because the format is plain text, the field names are real HTTP fields, and both vendors accept the file without complaint.

The file transferred perfectly. What did not transfer was the rule for what happens when two entries overlap, and that rule lives in the docs of whoever is reading the file, not in the file.

If you have carried a config between two vendors and assumed it meant the same thing on both — I would like to hear what it cost you. 👇


Share this post on:

Previous Post
Netlify Pulled The Plug — And Exposed The Vendor I Never Chose 🔌
Next Post
My CI Was Configured For Months And Had Never Run Once 💀