Files
KomediruzeckiandGitHub 41fbe3a519 fix: recover from stale-cache white screens after a deploy (#339)
Returning users hit a white screen after a deploy: the service worker served a
stale index.html referencing hashed chunks that were gone, and the assets host
returned index.html (200, text/html) for those missing chunks — so the module
loader failed with a MIME error, unrecoverable by a normal reload (only a desktop
hard-reload, which bypasses the SW, fixed it). This adds a layered strategy.

Good path (prevent the break):
- Front Worker (frontend/server/assets-worker.ts) + assets binding: a missing
  /assets/* now returns a real 404, never index.html; navigations still get the
  SPA shell. Confirmed via wrangler dev.
- _headers cache tiers: /assets/* immutable; index.html, sw.js, version.json
  no-cache (previously everything was max-age=0, must-revalidate).
- Service worker hardening: cleanupOutdatedCaches, skipWaiting + clientsClaim,
  navigateFallback denylist for /assets, /api, version.json; cache only 200s;
  fresh v4 cache names so a pre-fix poisoned entry can't be replayed.
- version.json + poller (core/appVersion.ts): on a new deploy, reload at a safe
  moment (next navigation) with a toast fallback — kills the mid-session
  dead-chunk navigation crash.

Bad path (recover gracefully):
- Pre-JS boot watchdog in index.html: if an app asset errors or the app never
  boots, clear the SW + caches and reload once, then show a manual recovery
  panel — works even when the bundle itself never loads.
- Runtime vite:preloadError / chunk-error handler (core/bootRecovery.ts): one
  guarded auto-reload, else escalate to the crash screen.
- CrashModal: add "Clear cache & reload" (SW + Cache Storage, keeps user data)
  and tailored "Update available" messaging for chunk/MIME errors.

Hygiene:
- Self-host fonts (@fontsource Inter/Fraunces/JetBrains Mono, latin + latin-ext):
  removes the cross-origin Google Fonts dependency and its CSP violations; drops
  the Google origins from the report-only policy.

Verified: front-worker 404 vs SPA fallback via wrangler dev; typecheck/lint/build
clean; 550 tests pass; app boots with self-hosted fonts and no CSP violations.
2026-07-15 15:41:59 +02:00

53 lines
3.2 KiB
Plaintext

# Headers for the static-asset (SPA) responses (audit S2/B1). Vite copies this into dist/,
# which the Cloudflare Worker serves as the assets root.
#
# CACHING — the core fix for "white screen / stale build after a deploy":
# - /assets/* are content-hashed and immutable → cache hard, forever. A new deploy ships
# new filenames, so an immutable cache can never serve stale code.
# - index.html (entry document), sw.js, and version.json must ALWAYS revalidate (no-cache),
# so a deploy is picked up on the very next load and the fresh HTML points at fresh assets.
# (Previously EVERYTHING was served max-age=0, must-revalidate — assets needlessly
# revalidated on every load, and nothing was cached immutably.)
#
# CSP — the enforcing policy stays minimal (framing/base-uri only) so it can't white-screen the
# app. The full content policy ships REPORT-ONLY (browser reports, blocks nothing) — safe to
# observe. Fonts are now SELF-HOSTED, so the policy no longer needs the Google Fonts origins.
# Note: `upgrade-insecure-requests` is intentionally NOT in the report-only policy — browsers
# ignore it there and log a warning; it lives only in the enforcing policy above, where it works.
# Before promoting the report-only policy to an enforcing `Content-Security-Policy`, resolve the
# one remaining known violation: a bundled dependency trips `unsafe-eval` via a benign
# `Function("return this")` global-object probe — either drop/patch that dep or add 'unsafe-eval'
# (a real weakening) knowingly. The allow-list covers the app's own assets plus Stripe
# (js.stripe.com + payment frames), Cloudflare Turnstile, Google sign-in, the Worker API origins,
# and the exchange-rate API.
/*
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Content-Security-Policy: frame-ancestors 'none'; base-uri 'self'; upgrade-insecure-requests
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'unsafe-inline' https://js.stripe.com https://challenges.cloudflare.com https://accounts.google.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https://api.dev.tokencircles.com https://api.tokencircles.com https://open.er-api.com https://challenges.cloudflare.com; frame-src https://js.stripe.com https://challenges.cloudflare.com https://accounts.google.com; frame-ancestors 'none'; base-uri 'self'; object-src 'none'
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()
Strict-Transport-Security: max-age=31536000; includeSubDomains
# Content-hashed build output (JS, CSS, self-hosted fonts, images) — safe to cache forever.
/assets/*
Cache-Control: public, max-age=31536000, immutable
# Entry document — never cache; revalidate every load so a deploy is seen immediately.
/
Cache-Control: no-cache
/index.html
Cache-Control: no-cache
# Service worker registration must revalidate so SW updates are detected promptly.
/sw.js
Cache-Control: no-cache
# Version stamp polled by the app for update detection — must always be fresh.
/version.json
Cache-Control: no-cache
# Workbox runtime is content-hashed → immutable.
/workbox-*.js
Cache-Control: public, max-age=31536000, immutable