I shipped a smaller version of an image. Went to check it, and the old image was still loading.
The deploy log showed success. The file inside the build output was the new one. But fetching it through the browser still got the old one.
My first guess was wrong
I figured "the deploy must have uploaded a mix of old and new files." Seemed plausible — a partial upload failure could do that.
To check, I looked at the response headers.
cf-cache-status: HIT
age: 92931
age was 92,931 seconds. I was getting something that had been sitting in cache for over 25 hours. The deploy was completely fine — the CDN was just serving something stale.
Confirming it was easy. I appended a random query string to the URL.
| Request | Size received |
|---|---|
/media/cover-s7.jpg |
106,224 bytes |
/media/cover-s7.jpg?x=48213 |
23,311 bytes |
Adding one query param got me the new file. That's because the cache is keyed on the full URL. Change the URL even slightly, it's a cache miss, and the CDN goes back to the origin.
Diagnosis done. This wasn't a deploy problem — it was a caching problem.
Turning off caching isn't the answer
The obvious fix is to shorten the cache TTL. But that defeats the whole point of caching. Images barely change once uploaded — making every request re-fetch them just in case one eventually changes is a net loss.
The real problem wasn't "the cache lives too long." It was "the content changed but the URL didn't."
Bake the content into the URL
I generated a hash from the file's contents and appended it to the URL.
/media/cover-s7.jpg?v=3a9a9bfe
3a9a9bfe is the first 8 characters of a hash of that file's contents. If the content changes, the hash changes. If the hash changes, the URL changes. A new URL is a cache miss, so it fetches the fresh version automatically. No need to purge anything — it just steps around the stale entry on its own.
Since the URL now permanently points to one immutable piece of content, I set the cache lifetime as long as it'll go.
/media/*
Cache-Control: public, max-age=31536000, immutable
One year. immutable is a declaration that "the content at this URL will never change," which means the browser won't even re-validate on a hard refresh.
I deliberately left HTML out
I considered applying the same trick to HTML. I didn't.
A post's URL is /posts/things-that-went-wrong/, and it has to stay that way forever. Hashing it would break every shared link and throw off search indexing. And if the URL can't change but the cache TTL is long, readers would keep seeing stale content for days after an edit — which is especially bad for something like a policy page.
So what about a short TTL instead? I measured it.
| Target | 1st run | 2nd run | 3rd run |
|---|---|---|---|
| HTML (no cache) | 421ms | 48ms | 48ms |
| Fonts (1-year cache) | 1,021ms | 48ms | 60ms |
Response times were identical whether caching was on or off. The slow first run is just TLS handshake cost — unrelated to caching.
The reason turned out to be the hosting setup. The static asset storage itself is already distributed across edge locations worldwide. Stacking another cache layer in front of it doesn't shorten the distance to fetch from.
Nothing to gain, and something real to lose. So I left it alone.
Where things landed
The header config settled into this:
| Target | Cache | Why |
|---|---|---|
/astro-assets/* |
1 year, immutable | filename itself is hashed |
/assets/fonts/* |
1 year, immutable | filename and content are both fixed |
/media/* |
1 year, immutable | content hash is in the query string |
| HTML | none | URL can't change |
One rule covers all of it: if the URL can change along with the content, cache forever. If it can't, don't cache at all.
HTML isn't missing from that list by accident — it's a deliberate choice. I left a comment in the config too, so next time I'm staring at this file I don't go "wait, why isn't HTML in here" and add it back in.



