DEV Community

静亭雅苑
静亭雅苑

Posted on

The Cache Header I Set Was Not the One Browsers Got

The Cache Header I Set Was Not the One Browsers Got

For two weeks I believed the cache configuration was correct. The file in the repository said what I wanted. The local server returned what I wanted. Production returned something else, and nobody noticed until a logo replacement refused to appear.

This is not a story about caching policy. It is a story about a value with two owners.

The symptom was small and boring

A one-kilobyte logo, replaced. The deploy finished. The page still showed the old one. Hard refresh fixed it, which is the classic sign that someone is caching a URL that never changes.

So I ran the command everyone should run before theorizing:

curl -I https://example.com/images/logo.png
Enter fullscreen mode Exit fullscreen mode
cache-control: public, max-age=86400
Enter fullscreen mode Exit fullscreen mode

One day. Meanwhile the stylesheet next to it returned:

cache-control: public, max-age=31536000, immutable
Enter fullscreen mode Exit fullscreen mode

Both were "correct" by someone's definition. Neither matched the file I had edited, which said images should be no-cache with an ETag.

Two owners for one header

The site is a small Express app behind nginx. That architecture has a property that is easy to forget: every response header has a potential author at each hop, and the last one to write wins.

In my case there were two authors, both alive:

  1. The app set Cache-Control explicitly per file type in its static middleware. Images got max-age=86400 hardcoded — not from a config file, from a literal in the handler.
  2. The nginx template in the repository had a proper static-serving block with its own cache rules and gzip settings.

The template was the one I edited. The app was the one answering.

And it got worse: the deploy script did not copy that template. It generated a fresh proxy-only config on the server, replacing whatever nginx had before. So the repository template was documentation for a file that production never read. Two config sources, one live server, and no error message when they disagreed.

Why local testing cannot see this

Locally I ran node server.js and opened localhost:3101. No nginx in the path. Every header came from the app, which is exactly the component that was wrong in production but looked authoritative locally.

The proxy is where the disagreement lives. If you only ever test at the origin, you are testing half the system.

The minimum discipline that would have caught it in one minute:

# origin
curl -sI http://127.0.0.1:3101/images/logo.png | grep -i cache-control
# through the proxy
curl -sI https://example.com/images/logo.png | grep -i cache-control
Enter fullscreen mode Exit fullscreen mode

Two lines. If the two answers differ, you have found the owner of the bug, not just the bug.

The first version was wrong in the other direction

Before this, the app had gone the opposite way: no-store on everything, etag: false. Nothing was ever cached. That "solved" stale assets by making every navigation re-download a one-megabyte charting library, and by removing the 304 that would have made revalidation nearly free.

Both versions share a root cause: a single global policy applied to files that have different lifecycles. HTML must be fresh on every deploy. Content-hashed JS and CSS can be cached for a year. Images with human-chosen names have no hash, so they need revalidation, not a TTL.

Three classes, three contracts:

Asset Header Why
HTML, small JSON config no-cache + ETag must reflect the deploy; 304 is cheap
JS/CSS with content hash in URL public, max-age=31536000, immutable URL changes when bytes change
Images, favicons, uploads no-cache + ETag name is stable, bytes change

no-cache keeps the copy and revalidates. With ETag, an unchanged file costs one conditional request and an empty 304. A replaced file is live on the next load, with no purge step and no cache-busting query string invented by hand.

Pick one owner, then make the other one read-only

The fix was not a better header value. It was removing the second author.

  • The app stopped setting cache headers for static files entirely. It owns the API only: no-store there, because a JSON response with user data should never be reused.
  • nginx owns static responses, from a template that lives in the repository.
  • The deploy script renders that template instead of inventing a config, so there is exactly one file to edit and exactly one file on the server.

A useful rule: if a repository contains a config file that the deploy process does not copy verbatim, that file is a comment, not a configuration.

The constraint that makes immutable safe

max-age=31536000, immutable is a promise that the bytes at that URL will never change. If you break the promise, you have pinned a stale script in browsers for a year, and no amount of redeploying fixes it for users who already loaded the page.

So hashing is not optional. Generate the version from the content:

  • take the first eight characters of the file's SHA-1;
  • rewrite ?v= on every <script src> and <link href> in the HTML;
  • skip absolute URLs and third-party libraries pinned to a semantic version;
  • make it idempotent: a second run must print "0 rewrites";
  • run it before git add, so the version and the code land in the same commit.

If the bump script is not wired in yet, immutable is strictly worse than no caching at all. The two changes ship together or not at all.

Compression belongs to the same owner

While the proxy was misconfigured, gzip was also missing. A one-megabyte library was crossing the wire uncompressed because the generated config had no gzip directive — the template that did have one never ran.

The header chain now ends like this:

gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types text/css application/javascript application/json image/svg+xml text/plain;
gzip_vary on;
Enter fullscreen mode Exit fullscreen mode

Verify with an explicit accept-encoding header rather than assuming:

curl -sI -H 'Accept-Encoding: gzip' https://example.com/js/app.js | grep -i content-encoding
Enter fullscreen mode Exit fullscreen mode

One detail worth knowing: an upstream that sets Cache-Control will have it passed through a proxy by default. If you want the proxy to override, you must say so with proxy_hide_header. Silence means "trust the upstream", which is how the app's hardcoded max-age survived a proxy rewrite without anyone noticing.

The verification habit

The bug survived because verification happened at the wrong layer. The habit that replaces it:

  1. After any cache change, curl -I the URL through the domain, not the local port.
  2. Check one file from each class: HTML, hashed asset, image.
  3. Replace an image, reload without a hard refresh, confirm the new bytes.
  4. Change a JS file, confirm the URL's ?v= changed and the old URL is no longer referenced.
  5. Confirm gzip with an explicit Accept-Encoding header.
  6. Confirm nothing in the repository sets the same header twice — grep for the header name and count the owners.

That last step is the general one. When a configuration "does not take effect", the question is rarely "is the syntax wrong". It is almost always "who else writes this value".

The same reasoning, applied to a static site's cache layering, is written up at wuhuajin.com.


Originally published on wuhuajin.com.

Top comments (0)