What went wrong
The site is a handful of HTML files, one stylesheet, a few scripts
and six font files, uploaded to an S3 bucket with
aws s3 sync and served through CloudFront. On the
fifteenth I added a writing section and a good deal of new CSS to
go with it, synced, invalidated the distribution, and opened the
new pages. They came up as bare HTML. A curl of the stylesheet
from the live site returned the new file. So the server was serving
the new stylesheet and my browser was not asking for it.
The cause: S3 does not send a Cache-Control header
unless you set one on the object, and I never had. With no explicit
lifetime, browsers fall back to heuristic caching, which takes a
fraction of the time since the file's Last-Modified
date. My style.css had not changed for some time, so my
browser had decided it was good for days and never went back to the
network. Invalidating CloudFront cleared the CDN's copy. It cannot
reach into anyone's browser. Every returning visitor was going to
see new pages with an old stylesheet until their heuristic timer
ran out.
Three rules
Pages and stylesheets: ten minutes. Every object
the deploy syncs goes up with Cache-Control: public,
max-age=600. A browser that has the page re-checks it within
ten minutes. That is long enough that a reader clicking around the
site is not re-downloading the stylesheet on every page, and short
enough that an edit is everywhere within the time it takes me to
write the commit message.
Fonts: a year, and immutable. The fonts are
self-hosted subsets, and a font file never changes in place. If I
change a face, it gets a new file name and the stylesheet points at
the new name. That happened the same afternoon I wrote the deploy
rule: the heading face switched, the old file left the repository,
a new one with a different name went up, and nobody's cached copy
of the old file mattered. So fonts carry public,
max-age=31536000, immutable. The immutable token
tells the browser not to bother revalidating even on a reload.
A version string on the stylesheet link. Every page
references the stylesheet as /style.css?v=20261118a.
The query string does nothing on the server. It changes the cache
key in the browser. When the stylesheet changes I bump the value in
every HTML file, and a browser that fetches a new page sees a URL
it has never cached and goes to the network for it. New HTML can
never pair with an old stylesheet, whatever the browser had
decided about style.css before. The b is
there because the stylesheet changed twice on the sixteenth.
The deploy script
Two commands do the work. The sync sets the ten-minute header on everything it uploads:
aws s3 sync "$SITE_DIR" "s3://$BUCKET" --profile "$PROFILE" \
--cache-control "public, max-age=600" \
--exclude ".git/*" --exclude "dns-backup/*" --exclude "deploy.sh"
Then a second pass over the font directory. It is needed for two
reasons. The sync applies one Cache-Control to every
object it touches, so the fonts would otherwise get ten minutes
like everything else. And the CLI guesses the content type of a
.woff2 file as application/octet-stream,
which browsers will use but which is wrong. The copy with
--metadata-directive REPLACE rewrites both headers on
the objects already in the bucket:
aws s3 cp "$SITE_DIR/assets/fonts" "s3://$BUCKET/assets/fonts" \
--profile "$PROFILE" --recursive --content-type "font/woff2" \
--cache-control "public, max-age=31536000, immutable" \
--metadata-directive REPLACE
Last, a CloudFront invalidation for /*, issued through
my infrastructure tool, so the CDN's own cache is clear the moment
the upload finishes. The invalidation handles the edge. The headers
handle the browsers. You need both.
What the live site sends
Headers are easy to describe and easy to get wrong, so check the
real thing. These are from curl -sI against the live
site today:
$ curl -sI https://stingrayio.com/style.css?v=20261118a
content-type: text/css
cache-control: public, max-age=600
$ curl -sI https://stingrayio.com/assets/fonts/newsreader-500-700-latin.woff2
content-type: font/woff2
cache-control: public, max-age=31536000, immutable
$ curl -sI https://stingrayio.com/
content-type: text/html
cache-control: public, max-age=600
One more line in the HTML belongs to this story. The heading font is the first thing a reader notices loading late, so the home page preloads it:
<link rel="preload" href="/assets/fonts/newsreader-500-700-latin.woff2"
as="font" type="font/woff2" crossorigin>
The crossorigin attribute is not optional. Browsers
fetch fonts in CORS mode even from the same origin, and a preload
without the attribute does not match the later request, so the
file is downloaded twice. With it, the preloaded copy is the one
the stylesheet uses, and it is the copy that stays for a year.
The trade-off
A returning visitor can see a page that is up to ten minutes old.
If I fix a typo and they loaded the page nine minutes ago, they see
the typo until they reload after the timer. For a site where the
content is essays and a description of the practice, that is fine,
and I would rather have it than a site that hammers the CDN for the
stylesheet on every click. If this were a page where a wrong price
or a wrong phone number for ten minutes mattered, I would drop the
pages to a shorter lifetime or to no-cache, which
still allows caching but forces a revalidation each time, and keep
the fonts exactly as they are.
The thing I would not trade away is the version string. The ten-minute window is a staleness I chose. A new page with an old stylesheet is a broken page, and the query string is what makes that impossible regardless of how any browser has been told to behave.
If you run a static site on S3
- Check what you are sending now:
curl -sIyour stylesheet and look forcache-control. If it is missing, you are relying on heuristics you did not choose. - Set a short explicit lifetime on pages and stylesheets in the sync command, so it is impossible to forget.
- Give fonts, and anything else that changes by changing its name, a year and
immutable, in a second pass that also fixes the content type. - Put a version query string on the stylesheet link and bump it whenever the stylesheet changes.
- Invalidate the CDN after every deploy, and remember that it only reaches the CDN.