Skip to content

Commit

Permalink
fix: respect base to register dev service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Mar 28, 2023
1 parent b37206a commit e59f088
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function createAPI(ctx: PWAPluginContext): VitePluginPWAAPI {
let shouldRegisterSW = options.injectRegister === 'inline' || options.injectRegister === 'script'
if (ctx.devEnvironment && ctx.options.devOptions.enabled === true) {
type = ctx.options.devOptions.type ?? 'classic'
script = generateRegisterDevSW()
script = generateRegisterDevSW(ctx.options.base)
shouldRegisterSW = true
}
else if (shouldRegisterSW) {
Expand Down
5 changes: 3 additions & 2 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ export function generateRegisterSW(options: ResolvedVitePWAOptions, dev: boolean
return undefined
}

export function generateRegisterDevSW() {
export function generateRegisterDevSW(base: string) {
const path = `${base.endsWith('/') ? base : `${base}/`}${DEV_SW_VIRTUAL.slice(1)}`
return `<script id="vite-plugin-pwa:register-dev-sw" type="module">
import registerDevSW from '${DEV_SW_VIRTUAL}';
import registerDevSW from '${path}';
registerDevSW();
</script>`
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function DevPlugin(ctx: PWAPluginContext): Plugin {

return html.replace(
'</body>',
`${generateRegisterDevSW()}
`${generateRegisterDevSW(options.base)}
</body>`,
)
},
Expand Down

0 comments on commit e59f088

Please sign in to comment.