fix: register the Semaphore service worker when hydration finishes after load - #242
Merged
Merged
Conversation
…ter load The registration lived in an inline next/script tag with the default afterInteractive strategy, so it ran after React hydration and only added a window "load" listener. Whenever hydration finished after "load" had already fired (always in `next dev`, and in production on slow devices) the listener never ran: /sw.js was never registered, the Semaphore wasm and zkey were never cached, retryDownload() hung on navigator.serviceWorker.ready and the "Downloading Semaphore" chip never cleared. Move registration into a React effect via a small helper that registers immediately when document.readyState is already "complete" and otherwise waits for "load", removing the listener on cleanup. Registration failures are logged with the script URL and consequence. Dropping the inline script also removes the CSP nonce dependency for this code path. Add node tests for both branches, cleanup, the rejection path and browsers without service worker support. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Checkouts nested inside another copy of the repo (for example under .claude/worktrees) made ESLint cascade into the parent's config and fail with a conflicting "tailwindcss" plugin. No effect where no parent config exists, such as CI. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Semaphore service worker was never registered in
next dev, and could be skipped in production on slow hydration.src/pages/_app.tsxregistered/sw.jsfrom an inlinenext/scripttag. With the defaultafterInteractivestrategy that script runs after React hydration and only attaches a windowloadlistener. When hydration finishes afterloadhas already fired, the listener never runs, so:/sw.jsis never registered and/semaphore/semaphore.wasm+.zkeyare never cached,retryDownload()insrc/lib/utils.tshangs onnavigator.serviceWorker.ready,Changes
src/lib/register-service-worker.ts(new): registers immediately whendocument.readyState === "complete", otherwise waits forloadwith aoncelistener, and returns a cleanup that removes the pending listener. Registration failures are logged with the script URL and consequence, never swallowed.src/pages/_app.tsx: calls the helper from a dedicated effect and drops the inline<Script>. No inline script means no CSP nonce is needed for registration.getInitialProps/pageProps.nonceare intentionally left in place:next buildreports that thisgetInitialPropsopts every route out of Automatic Static Optimization, so removing it is a separate decision.tests/register-service-worker.test.ts(new): fivenode:testcases covering the immediate branch, the deferred branch, cleanup, the rejection path, and browsers without service worker support..eslintrc.json:"root": true, in its own commit. Checkouts nested inside another copy of the repo made ESLint cascade into the parent config and fail on a conflictingtailwindcssplugin. No effect in CI; easy to drop if unwanted.Verification
Before the fix, in
next devwithpublic/sw.jsgenerated: page hydrated,document.readyState === "complete", inline#swscript element present,getRegistrations()empty, no caches, chip stuck.After the fix, from a clean state (worker unregistered, caches deleted):
next devnext build+next startnavigator.serviceWorker.getRegistrations()/sw.js,activated/sw.js,activatedsemaphore-cachecontentspnpm lint,pnpm typecheck,pnpm test(25/25),pnpm spellcheckandpnpm buildall pass.Notes
public/sw.jsis gitignored and generated bypnpm build; for dev, runpnpm exec workbox injectManifest workbox-config.cjsfirst.public/service-worker.jsconfiguresExpirationPlugin({ maxEntries: 1 })on a cache that must hold two files, so one entry is likely evicted after the first proof and re-downloaded on later visits.🤖 Generated with Claude Code