Skip to content

fix(pwa): always register VitePWA plugin so virtual:pwa-register resolves in dev - #1260

Closed
luoliguang wants to merge 1 commit into
blinkospace:mainfrom
luoliguang:pr/fix-pwa-dev-server
Closed

luoliguang wants to merge 1 commit into
blinkospace:mainfrom
luoliguang:pr/fix-pwa-dev-server

Conversation

@luoliguang

@luoliguang luoliguang commented Oct 6, 2026 •

Copy link
Copy Markdown

Problem

Starting the dev server crashes with:

[vite] Internal server error: Failed to resolve import "virtual:pwa-register"
    at ... node_modules/vite/dist/node/chunks/dep-...

Any file that imports virtual:pwa-register (e.g. a PWA auto-update hook) will cause this error.

Root cause

vite.config.ts currently excludes the VitePWA plugin entirely when running in development:

const isDev = process.env.NODE_ENV === 'development';
...(!isDev && !process.env.DISABLE_PWA ? [VitePWA({...})] : [])

When the plugin is not loaded, Vite never registers the virtual:pwa-register module resolver, so any import of that module fails immediately.

Fix

Always include the VitePWA plugin and use its built-in disable option to suppress service-worker behaviour when needed. The virtual module is then always available regardless of environment.

VitePWA({
  disable: !!process.env.DISABLE_PWA,
  devOptions: { enabled: false },
  ...
})

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:

[vite] Internal server error: Failed to resolve import "virtual:pwa-register"
at ... node_modules/vite/dist/node/chunks/dep-...

Any file that imports virtual:pwa-register (e.g. a PWA auto-update hook) will cause this error.

Root cause

vite.config.ts currently excludes the VitePWA plugin entirely when running in development:

const isDev = process.env.NODE_ENV === 'development';
...(!isDev && !process.env.DISABLE_PWA ? [VitePWA({...})] : [])

When the plugin is not loaded, Vite never registers the virtual:pwa-register module resolver, so any import of that module fails immediately.

Fix

Always include the VitePWA plugin and use its built-in disable option to suppress service-worker behaviour when needed. The virtual module is then always available regardless of environment.

VitePWA({
disable: !!process.env.DISABLE_PWA,
devOptions: { enabled: false },
...
})

devOptions.enabled: false (already present) ensures the service worker is never activated in development — only the module resolver is registered.

…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.
@luoliguang luoliguang closed this Oct 6, 2026
@luoliguang
luoliguang deleted the pr/fix-pwa-dev-server branch October 6, 2026 13:23
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.

1 participant