simonw / simonwillisonblog

The source code behind my blog
https://simonwillison.net/
Apache License 2.0
203 stars 19 forks source link

Improved caching policy for static.simonwillison.net #503

Closed simonw closed 1 month ago

simonw commented 1 month ago

From https://pagespeed.web.dev/

CleanShot 2024-10-23 at 13 01 34@2x

simonw commented 1 month ago

static.simonwillison.net is already behind Cloudflare, but it's not sending Cache-Control: max-age=31536000 style headers.

simonw commented 1 month ago
curl -I https://static.simonwillison.net/static/2024/prompt-gemini-extract.gif
HTTP/2 200 
date: Wed, 23 Oct 2024 20:02:52 GMT
content-type: image/gif
content-length: 160263
x-amz-id-2: LpMSRQox/dMz/qS2p2jb6OmP7/YsEa4YZnOfEVgB5biGOPynNxqs8eY5EgWW0xzbclBnOI6LTAQ=
x-amz-request-id: BK8F63EJYFP5K1YB
last-modified: Wed, 23 Oct 2024 18:18:49 GMT
etag: "38a67c2deb66fe4b6f57796c2f2f5c44"
cf-cache-status: HIT
age: 149
accept-ranges: bytes
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v4?s=xu4hL1Hr5W2ZDo6VSCd4NBfPp2tmCYiTNbct64mFfU6AAkBofX%2BHSZ6P%2FpIo3QKX5CqweeBhgS%2BYNOuzWW2E4pb4O3V1pISkOHzU0NPM%2B9QgGyuquDc%2FOl2aZM5HL3MjBPm0oKJD2AWWeyY%3D"}],"group":"cf-nel","max_age":604800}
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: 8d7453c3481b9e5e-SJC
alt-svc: h3=":443"; ma=86400
server-timing: cfL4;desc="?proto=TCP&rtt=23918&sent=5&recv=9&lost=0&retrans=0&sent_bytes=2907&recv_bytes=585&delivery_rate=124913&cwnd=177&unsent_bytes=0&cid=ec38180c3ba3b0e7&ts=47&x=0"

I already have a policy of changing the filename if I update a file on there, so I can safely put a long cache expiry on everything.

simonw commented 1 month ago

I'm trying a "transform rule":

CleanShot 2024-10-23 at 13 06 33@2x

https://dash.cloudflare.com/6f057ad2bb65bccc304820611c01dae5/simonwillison.net/rules/transform-rules/modify-response-header

simonw commented 1 month ago

Seems to have worked:

curl -I https://static.simonwillison.net/static/2024/prompt-gemini-extract.gif
HTTP/2 200 
date: Wed, 23 Oct 2024 20:07:12 GMT
content-type: image/gif
content-length: 160263
x-amz-id-2: LpMSRQox/dMz/qS2p2jb6OmP7/YsEa4YZnOfEVgB5biGOPynNxqs8eY5EgWW0xzbclBnOI6LTAQ=
x-amz-request-id: BK8F63EJYFP5K1YB
last-modified: Wed, 23 Oct 2024 18:18:49 GMT
etag: "38a67c2deb66fe4b6f57796c2f2f5c44"
cf-cache-status: HIT
age: 409
accept-ranges: bytes
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v4?s=2HCHW9L52YEhmN%2FbP1ZVxQLL%2FtLrpf5GTN1J%2B8dUnvC53hhSyW2PDiAHv%2B43DvJnsUsnrInVAxdZCgTsIfQ7JCg98jFzHMdu6xb3uOGAHPdC1aE9utyUGvAedNa6RVxO9yx30qkUSuiwR2k%3D"}],"group":"cf-nel","max_age":604800}
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
cache-control: max-age=31536000
server: cloudflare
cf-ray: 8d745a2099107ac4-SJC
alt-svc: h3=":443"; ma=86400
server-timing: cfL4;desc="?proto=TCP&rtt=23766&sent=5&recv=8&lost=0&retrans=0&sent_bytes=2907&recv_bytes=585&delivery_rate=131337&cwnd=117&unsent_bytes=0&cid=bd0475734101defd&ts=55&x=0"
simonw commented 1 month ago

https://pagespeed.web.dev/ complains less now:

CleanShot 2024-10-23 at 13 08 21@2x

simonw commented 1 month ago

And hitting "refresh" on my homepage is WAY faster because almost all of the heavy assets are served from my browser cache:

CleanShot 2024-10-23 at 13 09 13@2x

simonw commented 1 month ago

Extracted a TIL: https://til.simonwillison.net/cloudflare/cache-control-transform-rule