The symptom
After deploying a change to davito.dev, some browsers (mine included) kept loading the old CSS and JavaScript for hours. The HTML was already the new one and the styles weren't, so the page looked broken until you forced a reload.
What nginx was sending
For .css and .js I had expires -1;, which makes nginx send Cache-Control: no-cache. The usual idea: the browser may keep the file, but it has to ask the server before using it again.
What reached the browser
Requesting the file from outside, through Cloudflare, the response carried cache-control: max-age=14400: exactly four hours, the zone's Browser Cache TTL. Cloudflare had replaced my no-cache with its own value, and for those four hours the browser didn't even ask. It's the same header it adds when the origin sends none, as happens with robots.txt today.
The fix
no-store is respected. With it, Cloudflare doesn't cache the response (cf-cache-status: BYPASS) and the browser doesn't keep it either:
location ~* \.(css|js)$ {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
}
$ curl -sI https://davito.dev/assets/css/style.css | grep -i cf-cache-status
cf-cache-status: BYPASS
$ curl -sI https://davito.dev/robots.txt | grep -iE "cache-control|cf-cache-status"
cache-control: max-age=14400
cf-cache-status: MISS
The price is that every visit downloads the CSS and JS again. For a small site it's worth it: I'd rather see a change straight away. If it grew, the right move would be a hash in the file name (style.3f2a1c.css) cached for a year with immutable: every deploy changes the name and caching stops being a problem.
What I took away
- Check headers from outside, through the CDN, not just on the server.
no-cacheisn'tno-store: the first allows storing and forces revalidation; the second forbids storing.- A CDN in front is another layer with its own rules. I mention it in the homelab decisions too.