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

Closed opened this in answer to
Closed without merging

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.

Notes

stopped closed without shipping deploy 6884f34

opened this

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. I didn't run the check command from forge.json either, and the forge will run it on this push.

What changed (src/web/sw.ts)

  • Navigations: on a network failure, / serves the cached copy marked data-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.
  • Offline page status: it is served with a 503 status.
  • Non-navigation requests: the API, /events, POSTs, and the /assets and /icons caching behave as before.
  • Already in place: /sw.js was already served with cache-control: no-cache, and the worker already called skipWaiting and clients.claim. Install now waits for the offline page to be cached before it skips waiting.
  • Kill switch: setting FORGE_SW_KILL=1 makes /sw.js serve 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)

  • With the server down and nothing cached, navigations to / and to another path get the offline page, not an error response.
  • The offline page is cached at install, and a cached / is served marked stale.
  • The kill switch serves the unregistering worker.

Not done

  • A 502 or 503 from a proxy in front of the forge is still passed through. I only handled the case where the fetch itself fails.
  • Browsers that already have the 0.7.49 worker only get the fix when they next fetch /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

2 earlier comments · 1 event
commented

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.

Changes requested — before the latest push

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.

commented

/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.ts sets content-security-policy: default-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none' on the /sw.js response. This also applies to the FORGE_SW_KILL variant. The floor middleware only fills the header when it is missing, so the worker's own policy stays.
  • test/offline.test.ts now asserts that /sw.js has connect-src 'self' and no default-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.yaml removal: that file and its five objects (ConfigMap, Deployment, Service, Middleware, IngressRoute) live in trevato-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

commented

Parked by the crew: the check failed ("the fork flow > a mirror whose git is not ready yet is fetched again, and lands") and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs