Skip to content

fix(react): avoid duplicate SW registration in strict mode#932

Open
cyphercodes wants to merge 1 commit into
vite-pwa:mainfrom
cyphercodes:cyphercodes/fix-925-react-strict-sw-registration
Open

fix(react): avoid duplicate SW registration in strict mode#932
cyphercodes wants to merge 1 commit into
vite-pwa:mainfrom
cyphercodes:cyphercodes/fix-925-react-strict-sw-registration

Conversation

@cyphercodes

Copy link
Copy Markdown

Description

Moves React useRegisterSW service worker registration out of the useState lazy initializer and into a guarded effect. The returned updateServiceWorker function stays stable and forwards to the registered updater via a ref, so React StrictMode development double-invocation does not register the service worker twice.

Added a focused Vitest coverage case that simulates StrictMode double-invoking state initializers/effects and verifies a single registration.

Linked Issues

Fixes #925

Additional Context

Verification:

  • pnpm run test-client
  • pnpm run lint
  • pnpm run build
  • pnpm -C examples/react-router run run-build-claims && pnpm -C examples/react-router exec vitest run
  • pnpm -C examples/react-router run run-build-sw-claims && SW=true pnpm -C examples/react-router exec vitest run
  • git diff --check

@netlify

netlify Bot commented May 8, 2026

Copy link
Copy Markdown

Deploy Preview for vite-plugin-pwa-legacy ready!

Name Link
🔨 Latest commit 42a6906
🔍 Latest deploy log https://app.netlify.com/projects/vite-plugin-pwa-legacy/deploys/69fe02a1d43fde0008e50ad2
😎 Deploy Preview https://deploy-preview-932--vite-plugin-pwa-legacy.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useRegisterSW (react) registers the SW twice in dev mode if strict mode is enabled

1 participant