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

72 lines
3.2 KiB
TypeScript

/**
* Front Worker for the static-asset (SPA) deployment on Cloudflare Workers Static Assets.
*
* WHY THIS EXISTS
* Static Assets with `not_found_handling: "single-page-application"` serves `index.html`
* (HTTP 200, `text/html`) for ANY unmatched path — including a missing `/assets/*.js` after
* a redeploy deletes the old hashed chunks. A returning user whose cached/old `index.html`
* still references `index-OLDHASH.js` then gets HTML where a module was expected:
* "Failed to load module script: expected a JavaScript module but got MIME type text/html"
* → white screen, unrecoverable by a normal reload.
*
* FIX
* Put a Worker in front of the assets binding configured with `not_found_handling: "none"`:
* - a matched asset is served by the platform directly (this Worker is not even invoked);
* - a miss on a real file path returns the asset system's clean 404, so a stale chunk fails
* honestly and the app's runtime recovery (vite:preloadError → guarded reload) takes over;
* - a navigation (an HTML-document request) falls back to `index.html` so the hash-routed
* SPA still boots on any path, and that shell is returned with `Cache-Control: no-cache`
* so the entry document is never served stale.
*/
interface AssetsBinding {
fetch(request: Request): Promise<Response>
}
interface Env {
ASSETS: AssetsBinding
}
/**
* A navigation is a top-level document load (the browser wants HTML), not a fetch for a
* hashed asset. Prefer the explicit Fetch Metadata signal; fall back to an Accept check for
* an extensionless path so clients that omit `Sec-Fetch-Mode` still route correctly.
*/
function isNavigationRequest(request: Request): boolean {
if (request.method !== 'GET' && request.method !== 'HEAD') return false
if (request.headers.get('sec-fetch-mode') === 'navigate') return true
const accept = request.headers.get('accept') || ''
if (!accept.includes('text/html')) return false
const path = new URL(request.url).pathname
const lastSegment = path.slice(path.lastIndexOf('/') + 1)
return !lastSegment.includes('.') // no file extension → a route, not an asset file
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const assetResponse = await env.ASSETS.fetch(request)
// Anything the asset layer resolved (a real file, or a non-404) passes straight through.
if (assetResponse.status !== 404) return assetResponse
// A 404 on a navigation → serve the SPA shell so any path boots the hash router.
if (isNavigationRequest(request)) {
const url = new URL(request.url)
const indexResponse = await env.ASSETS.fetch(new Request(new URL('/', url.origin), request))
if (indexResponse.status === 404) return assetResponse // no shell to serve
const headers = new Headers(indexResponse.headers)
// The entry document must never be cached hard, so a deploy is picked up on next load.
headers.set('Cache-Control', 'no-cache')
return new Response(indexResponse.body, {
status: 200,
statusText: 'OK',
headers,
})
}
// A 404 on an asset/file path → return it. Do NOT serve index.html: a stale chunk must
// fail as a 404, not as a 200 text/html that poisons the module loader.
return assetResponse
},
}