From 0833b83d5740d52ee8cb3224f4b7b4887b18b794 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 16:15:01 +0000 Subject: [PATCH 1/2] Stop caching 404s under /_astro/ for a year public/_headers gives /_astro/* a year of immutable caching, and Cloudflare adds it to the 404 for a missing file too. Right after a deploy, a page can ask for a new stylesheet from an edge that still serves the previous version; the browser could then keep that 404 for a year. Run the Worker first for /_astro/* as well as /images/*. It passes every response on as the assets serve it, except a 404, which gets Cache-Control: no-store. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013MdjUbV3zudUd8ZBxydwBu --- AGENTS.md | 2 +- public/_headers | 1 + worker/index.js | 28 +++++++++++++++++++++++----- wrangler.jsonc | 7 ++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 9c19b14..6066168 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,7 +16,7 @@ Josselin Perrus's personal site: an Astro 7 static site served by Cloudflare Wor - `src/data/projects.ts`: the projects, with their screenshots in `src/assets/projects/`, and which articles belong to each, by article id. - `docs/document-sharing.md`: how the encrypted portfolio links on `/documents/` work and how to create them. - `wrangler.jsonc`: the Cloudflare Worker `meaningfool`, which serves `dist/` as static assets. -- `worker/index.js`: the only Worker code. It answers byte-range requests for the videos in `/images/`, which Safari needs to play them. +- `worker/index.js`: the only Worker code, run only for `/images/*` and `/_astro/*`. It answers byte-range requests for the videos in `/images/`, which Safari needs to play them, and sends `Cache-Control: no-store` on the 404 for a missing `/_astro/` file instead of the year of caching `public/_headers` gives those files. - `public/_headers`: headers Cloudflare adds to responses: security headers, a year of caching for the fingerprinted `/_astro/*` files, and a Content-Security-Policy in report-only mode. A new kind of outside resource, such as another video host's embed or a third-party script, must be added to that policy. The build also writes `dist/_redirects`, the permanent `/page` → `/page/` redirects, with `src/lib/trailing-slash-redirects.mjs`. ## Commands diff --git a/public/_headers b/public/_headers index 74f643e..6d839ed 100644 --- a/public/_headers +++ b/public/_headers @@ -13,6 +13,7 @@ Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' https://static.cloudflareinsights.com; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self' data: https:; media-src 'self'; frame-src https://www.youtube.com https://www.youtube-nocookie.com; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none' # Fingerprinted build files: their names change whenever their content does. +# The 404 for a missing one gets no-store instead, from worker/index.js. /_astro/* Cache-Control: public, max-age=31536000, immutable diff --git a/worker/index.js b/worker/index.js index 5d8d52b..e4050d5 100644 --- a/worker/index.js +++ b/worker/index.js @@ -1,11 +1,21 @@ -// Cloudflare serves static assets without byte ranges: a Range request gets the -// whole file with status 200. Safari won't play a