901763b72f13db0bf4ffbb1dfa69e0c4b68ee99a
Three production bugs, all verified against the live site before and after.
1. CSP violations in the console
Two separate causes, only one of which is ours:
- The theme bootstrap was `<script is:inline>`, which Astro never hashes
(core/csp/common.js `trackScriptHashes` walks bundled scripts, client
directives, island prebuilts and settings.scripts — `is:inline` is in
none of them). It survived only because a <meta> CSP does not govern
markup that precedes it, and the script happened to sit at byte 237 with
the meta at byte 16119. Fragile by accident.
- The same trick did NOT save /500 and /429, whose `is:inline` scripts sit
in the body, after the meta. Verified in dist: meta@8777, script@14554.
The 500 reference code and the 429 countdown have never run in
production.
Fixes: the theme bootstrap moves to `injectScript("head-inline", ...)` via
a tiny local integration — the one path that is both rendered verbatim
inline (no FOUC) and hashed from that same string, so the CSP cannot drift.
The two error-page scripts become processed `<script>` and get bundled and
hashed; deferring is harmless for a span fill and a 1s countdown.
`scripts/check-csp-hashes.mjs` now fails the build if any inline script is
missing from its page CSP. Currently: 283 scripts / 51 pages, 0 unhashed.
NOTE: the violation actually visible in the browser console is Cloudflare's
Browser Insights beacon (`window.__CF$cv$params`), which embeds the CF-RAY
so its hash changes per request and can never be allowlisted. That one is a
dashboard setting, not code.
2. Hovering any link logged a 503
`<ClientRouter />` force-enables prefetch with `prefetchAll: true` and a
hover strategy. Cloudflare's zone Speculation Rules then refuses every
`Sec-Purpose: prefetch` request to a Worker origin:
curl -H 'Sec-Purpose: prefetch' /gaming/ -> 503 Cf-Speculation-Refused
Both /gaming and /gaming/ 503, so this was never a trailing-slash problem.
`prefetch: false` is pinned so re-adding the router cannot silently switch
hover-prefetch back on. The zone toggle still has to be turned off too.
3. Pages not finishing render on route change
Astro documents `<ClientRouter />` as unsupported with `security.csp`, and
the mechanism is concrete: swapHeadElements removes the old <meta> CSP, but
removing a meta does not revoke an enforced policy — so each navigation
ANDs another policy onto the document. style-src genuinely differs per page
(58 distinct hashes site-wide, 45-47 per page), so after one soft nav the
destination loses stylesheets. Removing the router is the fix; CSP-as-header
is not available here (the Cloudflare adapter declares no
adapterFeatures.staticHeaders, so prerendered routes are forced to meta).
Nothing used transition:persist/name/animate, so only the default fade is
lost — and 16KB of the 81KB client JS budget goes with it.
Also in this pass:
- Trailing slash: `sectionPath`/`alternatePath` now emit the canonical form, so
nav hrefs match canonicals and the sitemap instead of costing a 307 each.
`isActivePath` had to change with them or every aria-current would silently
disappear (`"/blog/" + "/"` never matches). Deliberately NOT setting
`trailingSlash: "always"`: create-manifest.js gives extension-less endpoints
the config value, which would make POST /api/report 404.
- Pre-existing bug: middleware served `/403` and `/429` through ASSETS, but
those build to `403/index.html` and `html_handling: "auto-trailing-slash"`
answers the slash-less form with a 307 and an EMPTY body — which the handler
re-wrapped as a blank 403/429. Confirmed live: `/403` -> 307, 0 bytes.
Denylisted and rate-limited visitors have been getting a blank page.
Verified: build, astro check (0 errors), eslint, prettier, knip all green;
CSP guard passes; no slash-less internal hrefs remain in the built HTML.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Languages
TypeScript
51.9%
Astro
19.4%
Svelte
16.3%
JavaScript
9.9%
CSS
2.5%