Public: anyone can read this; the crew can change it.
The service worker turns a forge restart into a Safari error page: never answer a navigation with Response.error() #468
A forge restart no longer reaches the browser as an error page: the service worker answers a failed navigation with a page instead of Response.error(). test/offline.test.ts passes (14 tests). The full bun test run hit my 10-minute limit, so I didn't see it finish.
stopped closed without shipping deploy 6884f34
2 earlier comments · 1 event
The service worker now answers a navigation with a 503 "restarting" page that refreshes itself, instead of Response.error(). It also caches an offline page at install and serves a kept home page stale. A kill switch (FORGE_SW_KILL=1) serves a self-unregistering worker.
This push fixes the failed check, a type error in test/offline.test.ts at line 280. The handlers map is typed with optional entries, so calling handlers.install(...) was flagged as possibly undefined. I changed it to handlers.install!(...). The test was wrong, not the worker, and no worker code changed.
bun x tsc --noEmit is clean and test/offline.test.ts passes (14 tests). I did not run the full shard list from forge.json's check, only that file. To try it, stop the app and load any page: you should get the restarting page, not a browser error.
The core fix is right: a navigation now gets an inline 503 offline page instead of Response.error(), cached at install, with the home page still served stale first. The tests cover it. One thing misses the bar.
Fix first: the FORGE_SW_KILL switch is documented nowhere. A grep finds it only in the worker source and the test. The README does not say it exists, what it does, or when to set it. Add it next to the other FORGE_ settings and mention it in the pull.
Not blocking: test that the kill response has no etag; add a Try again link on the offline page for keyboard users; set a colour scheme so it is not white in dark mode.
/sw.js now sends its own Content-Security-Policy, so fetch() inside the worker is no longer refused. I only ran test/offline.test.ts (14 pass); the full bun test timed out at 120s and I didn't re-run it. The change is pushed.
Cause: the worker takes its CSP from its own script response. The server's floor policy is default-src 'none', so connect-src fell back to 'none' and every worker fetch was blocked. That is why navigations and /assets/* came back as Response.error().
Change:
src/web/sw.tssetscontent-security-policy: default-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'on the/sw.jsresponse. This also applies to theFORGE_SW_KILLvariant. The floor middleware only fills the header when it is missing, so the worker's own policy stays.test/offline.test.tsnow asserts that/sw.jshasconnect-src 'self'and nodefault-src 'none'. It would fail on the old header.- The worker's script bytes are unchanged from the 0.7.49 worker, but they differ from the retire script, so the browser replaces the retire worker.
Not done:
- Playwright Chromium and WebKit test: Playwright isn't in the repo and I didn't add a dependency for it. The header test covers the cause but not worker-served styling in a real browser. Say if you want the dependency and I'll add it with a README justification.
- Skipping re-registration on every load (optional item): left alone.
register()is idempotent and cheap. sw-retire.yamlremoval: that file and its five objects (ConfigMap, Deployment, Service, Middleware, IngressRoute) live intrevato-apps/gitops, not here. Delete them in that repo once this ships.
To try: run the app, then curl -I /sw.js and check the content-security-policy header. In a browser, register the worker, reload /, and confirm forge.css is served by the worker.
🤖 Generated with Claude Code
A forge restart no longer reaches the browser as an error page: the service worker answers a failed navigation with a page instead of
Response.error().test/offline.test.tspasses (14 tests). The fullbun testrun hit my 10-minute limit, so I didn't see it finish. I didn't run thecheckcommand fromforge.jsoneither, and the forge will run it on this push.What changed (
src/web/sw.ts)/serves the cached copy markeddata-stale="1". If nothing is cached, and for every other navigation, it serves an inline offline page. The page says the forge is restarting and retries itself with<meta refresh>every 5 seconds. It is cached at install and the worker rebuilds it if the cache entry is missing./events, POSTs, and the/assetsand/iconscaching behave as before./sw.jswas already served withcache-control: no-cache, and the worker already calledskipWaitingandclients.claim. Install now waits for the offline page to be cached before it skips waiting.FORGE_SW_KILL=1makes/sw.jsserve a worker that clears the caches, unregisters itself and reloads open pages. It is read on each request, so it needs a restart with the variable set but no deploy.Tests (
test/offline.test.ts)/and to another path get the offline page, not an error response./is served marked stale.Not done
/sw.js, which can take up to the old 4 hours.Try it: stop the app, load
/in a browser that has registered the worker, and the "restarting" page appears and reloads itself once the app is back.🤖 Generated with Claude Code
Closes #457