Repository navigation
fix(pwa): always register VitePWA plugin so virtual:pwa-register resolves in dev - #1260
Closed
luoliguang wants to merge 1 commit into
Closed
luoliguang wants to merge 1 commit into
luoliguang wants to merge 1 commit into
Conversation
…lves in dev When the VitePWA plugin is excluded entirely in development (`!isDev && …`), Vite never registers the `virtual:pwa-register` module. Any import of that module (e.g. in a PWA auto-update hook) causes the dev server to crash with: [vite] Internal server error: Failed to resolve import "virtual:pwa-register" The fix is to always include the VitePWA plugin and use its built-in `disable` option (or `devOptions.enabled: false`) to suppress the actual service-worker behaviour in development. The virtual module is then always available, the dev server starts cleanly, and the SW is never activated until a production build.
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.
Problem
Starting the dev server crashes with:
Any file that imports
virtual:pwa-register(e.g. a PWA auto-update hook) will cause this error.Root cause
vite.config.tscurrently excludes the VitePWA plugin entirely when running in development:When the plugin is not loaded, Vite never registers the
virtual:pwa-registermodule resolver, so any import of that module fails immediately.Fix
Always include the VitePWA plugin and use its built-in
disableoption to suppress service-worker behaviour when needed. The virtual module is then always available regardless of environment.devOptions.enabled: false(already present) ensures the service worker is never activated in development — only the module resolver is registered.Problem
Starting the dev server crashes with:
Any file that imports
virtual:pwa-register(e.g. a PWA auto-update hook) will cause this error.Root cause
vite.config.tscurrently excludes the VitePWA plugin entirely when running in development:When the plugin is not loaded, Vite never registers the
virtual:pwa-registermodule resolver, so any import of that module fails immediately.Fix
Always include the VitePWA plugin and use its built-in
disableoption to suppress service-worker behaviour when needed. The virtual module is then always available regardless of environment.devOptions.enabled: false(already present) ensures the service worker is never activated in development — only the module resolver is registered.