Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): type module and custom sw not working when not using virtual modules #361

Merged
merged 7 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export function generateSimpleSWRegister(options: ResolvedVitePWAOptions, dev: b
const path = dev ? `${options.base}${DEV_SW_NAME}` : `${options.base}${options.filename}`

// we are using HMR to load this script: DO NOT ADD window::load event listener
if (dev)
return `if('serviceWorker' in navigator) navigator.serviceWorker.register('${path}', { scope: '${options.scope}' })`

if (dev) {
const swType = options.devOptions.type ? options.devOptions.type : 'classic'
return `if('serviceWorker' in navigator) navigator.serviceWorker.register('${path}', { scope: '${options.scope}', type: ${swType} })`
}
return `
if('serviceWorker' in navigator) {
window.addEventListener('load', () => {
Expand Down
31 changes: 21 additions & 10 deletions src/plugins/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { generateSimpleSWRegister, injectServiceWorker } from '../html'
import { generateWebManifestFile } from '../assets'
import {
DEV_SW_NAME,
FILE_SW_REGISTER, VIRTUAL_MODULES_RESOLVE_PREFIX,
FILE_SW_REGISTER,
VIRTUAL_MODULES_RESOLVE_PREFIX,
} from '../constants'
import type { ResolvedVitePWAOptions } from '../types'
import { generateServiceWorker } from '../modules'
Expand Down Expand Up @@ -37,7 +38,12 @@ export function DevPlugin(ctx: PWAPluginContext): Plugin {

return html.replace(
'</body>',
`<script type="module" src="${options.base}${DEV_SW_VIRTUAL}"></script></body>`,
`
<script type="module">
Copy link
Member

@userquin userquin Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antfu should we check CORS here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could leave it for now, and bring it back if there is really a valid case.

import registerDevSW from '${DEV_SW_VIRTUAL}';
registerDevSW();
</script>
</body>`,
)
},
},
Expand Down Expand Up @@ -190,17 +196,19 @@ function createSWResponseHandler(server: ViteDevServer, ctx: PWAPluginContext):
scope,
inlinePath: `${base}${DEV_SW_NAME}`,
registerPath: `${base}${FILE_SW_REGISTER}`,
swType: options.devOptions.type,
},
})
}
}
}

function generateSWHMR() {
return `import.meta.hot.on('${DEV_REGISTER_SW_NAME}', ({ inline, inlinePath, registerPath, scope }) => {
return `
import.meta.hot.on('${DEV_REGISTER_SW_NAME}', ({ inline, inlinePath, registerPath, scope, swType = 'classic' }) => {
if (inline) {
if('serviceWorker' in navigator) {
navigator.serviceWorker.register(inlinePath, { scope });
navigator.serviceWorker.register(inlinePath, { scope, type: swType });
}
}
else {
Expand All @@ -209,11 +217,14 @@ function generateSWHMR() {
document.head.appendChild(registerSW);
}
});

try {
import.meta.hot.send('${DEV_READY_NAME}');
} catch (e) {
console.error('unable to send ${DEV_READY_NAME} message to register service worker in dev mode!', e);
}`
function registerDevSW() {
try {
import.meta.hot.send('${DEV_READY_NAME}');
} catch (e) {
console.error('unable to send ${DEV_READY_NAME} message to register service worker in dev mode!', e);
}
}
export default registerDevSW;
`
}