Architecture · Guides

Cache-Control for a static site with no build step: why I cut CSS caching to one hour.

Long cache lifetimes speed up repeat visits but break deploys when filenames never change. Sensible Nginx Cache-Control settings for fonts, CSS, JS and images.

Key takeaways

  • Long cache lifetimes are safe only for files whose name changes when the content changes.
  • Without a build step, style.css stays style.css, so a long cache can serve stale code for days.
  • Cache fonts for a year, images for weeks and CSS/JS for a short time with revalidation.
  • Always test what visitors really get after a deploy.

Why caching matters

Browsers can store a copy of a file and reuse it instead of downloading it again. That makes repeat visits faster and saves bandwidth. PageSpeed Insights flags files with no cache lifetime, as it did for my fonts, CSS and JavaScript, so I added long ones everywhere. That turned out to be a mistake for two of the three.

Fonts: cache for a year

Font files almost never change, and a changed font would get a new filename. I serve them with a one-year, immutable cache:

location ~* \.woff2$ {
    expires 1y;
    add_header Cache-Control "public, immutable" always;
}

immutable tells the browser not to even revalidate the file during that period.

CSS and JavaScript: the trap

Build tools solve caching by putting a hash in the filename, such as app.4f3a9c.css. When the content changes, the name changes, so the browser fetches the new file even under a one-year cache.

This site has no build step. style.css is always style.css. With a seven-day cache, a returning visitor's browser could keep running the old file for up to a week after I deployed a fix, without even contacting the server. A change I thought was live would not reach people who had visited recently.

The compromise

I changed CSS and JS to a one-hour cache with must-revalidate:

location ~* \.(css|js)$ {
    expires 1h;
    add_header Cache-Control "public, must-revalidate" always;
}

An hour is short enough that a deploy shows up quickly, and long enough to avoid re-downloading on every page of a single visit. PageSpeed still reports a cache lifetime, though a shorter one than a build-hashed setup would allow.

Images: a month

Images change rarely, and when they do I normally give them a new name.

location ~* \.(png|jpe?g|svg|webp|avif|ico)$ {
    expires 30d;
    add_header Cache-Control "public" always;
}

Better option: version your files

If you want long caches for CSS and JS without the risk, version the filename or add a query string you change on deploy, for example style.css?v=12. With a static site this means editing every page when you change it, unless you use a small script or generator. That is the point where a build step starts to earn its place.

HTML pages

Do not cache HTML for long. HTML references everything else, so stale HTML means stale links and content. Let it revalidate on each visit, or use a short lifetime.

Avoiding duplicate location errors

Control panels such as aaPanel often generate their own static-file location block. If you paste a second one that matches the same extensions, Nginx refuses to reload with a duplicate location error. Remove the panel's block or skip the duplicates and only add your own headers.

Verify

curl -I https://example.com/css/style.css

Look for Cache-Control and Expires. Test in a private window after deploying, and check that a fresh load shows your latest change.

Frequently asked questions

What does immutable mean?

It tells the browser the file will never change at that URL, so it need not check for updates during the cache lifetime.

Will a short CSS cache hurt my PageSpeed score?

It can flag "efficient cache lifetime" in some reports. The trade-off is reliable deploys. Versioned filenames let you have both.

Should I cache HTML at the CDN?

Only if you can purge it on deploy. Otherwise keep lifetimes short.

Need performance work that does not break deploys?

Get in touch