fix(web): wire real PWA auto-update instead of the plugin's no-op default#86
Merged
Conversation
…ault
vite.config.ts declared `registerType: 'autoUpdate'`, but nothing in
the app ever imported `virtual:pwa-register` — so vite-plugin-pwa fell
back to injecting a bare `navigator.serviceWorker.register('/sw.js')`
script with zero update-detection or reload logic. Combined with the
generated SW's skipWaiting()+clientsClaim() (already on), a new
deploy's service worker silently took control of open tabs without
ever telling them to reload — so the app kept running the OLD JS
bundle indefinitely until every tab was fully closed. Confirmed live:
PR#85 was merged, deployed, and served correctly by the origin (bundle
hash matched a from-source rebuild of the merge commit), but a
same-session open tab still showed neither the fix nor the new delete
button, because this registration gap has nothing to do with server
caching or CDN — it's entirely client-side.
Fix: register the service worker ourselves via the official React hook
(`useRegisterSW` from `virtual:pwa-register/react`, in App.tsx) so the
`autoUpdate` mode's real behavior — auto-reload once the new SW
activates — actually runs. `injectRegister: null` stops the plugin
from also injecting its redundant no-op script.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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
vite.config.tsdeclaredregisterType: 'autoUpdate', but nothing in the app ever importedvirtual:pwa-register— so vite-plugin-pwa fell back to injecting a barenavigator.serviceWorker.register('/sw.js')script with zero update-detection or reload logic. The generated SW already callsself.skipWaiting()+clientsClaim(), so a new deploy's SW silently takes control of open tabs — but nothing ever tells those tabs to reload, so they keep running the old JS bundle indefinitely until every tab is fully closed.Found this diagnosing "PR#85 merged but I still see the old behavior live" — verified the server was serving the correct, freshly-built bundle (hash matched a from-source rebuild of the merge commit), so the gap was entirely client-side SW update propagation, not caching/CDN/deploy lag.
Fix
Register the SW via the official React hook (
useRegisterSWfromvirtual:pwa-register/react, called inApp.tsx) soautoUpdatemode's real behavior — silent auto-reload once the new SW activates — actually runs.injectRegister: nullstops the plugin from also injecting its redundant no-op script.Test plan
npx tsc -b— cleannpm run build—dist/index.htmlno longer references aregisterSW.jsscript;virtual:pwa-register/reactresolves and is bundled (workbox-windowchunk present)npx vitest run— 144/144 passingnpx oxlint— no new warnings