Safari and Early Hints
As I was getting this site set up on Cloudflare Pages, I came across documentation on their support for Early Hints.
I’m not extremely familiar with Early Hints, or the
associated standard, but I’ve been
sort-of aware since
Rails automatically generates Link
headers with preload
for served assets. It seemed neat, and a feature of my chosen platform, so I
decided to give it a try.
The Bug
After adding a <link rel="preload">
tag to the <head />
for my sites
stylesheet, I confirmed that Cloudflare was now adding Link
headers to the
HTTP responses, and all seemed well.
When I came back later to update that stylesheet, I couldn’t get my changes to stick when viewing the site in Safari. Thinking the response must be getting cached, I opened up the network inspector:
Request
No request, served from the memory cache.
Response
...
Cache-Control: private, max-age=0, proxy-revalidate, no-store, no-cache, must-revalidate
Seeing “served from memory cache” and Cache-Control: ..., no-cache
in the same
network request summary was perplexing to say the least.
Nothing I did stopped Safari from using the cached response. I tried enabling
“Disable Caches” in the dev tools, but to no avail. Finally, I did some
sleuthing, and
found a report in the WebKit bug tracker.
Turns out if you preload an asset in Safari, it will become cached, regardless
of the Cache-Control
headers that were returned in the original response.
Is Safari Really the New IE?
It’s always fun when you stumble across a browser bug, especially one that was reported almost four years ago. I don’t want to bad-mouth Safari too much though, since I’m one of those weirdos that actually, kinda likes using it.
I think if my assets included a digest in the file name, this likely wouldn’t be
a problem. Since changes to the file would be reflected in the request path, I
wouldn’t care if Safari permanently cached it. I’m also guessing this is why
Rails gets away with preloading assets using Link
headers by default, since
all assets get an appended digest.
As a fix, I just removed the preloading. I could add asset digests, but one of my stated goals for this site was to keep things simple, and honestly, the site loads plenty fast.