HMR for Service Worker without PWA #692
-
Is it possible to get HMR (Or at least an automatic update of the SW) running without the full PWA capabilities? The docs (https://vite-pwa-org.netlify.app/guide/service-worker-without-pwa-capabilities.html) imply that it would be:
But after following the steps in that guide that's not what's happening. In fact looking at the output of the The config in dev mode is: {
srcDir: 'app/client/service-worker',
filename: 'service-worker.ts',
strategies: 'injectManifest',
injectRegister: false,
manifest: false,
injectManifest: { injectionPoint: undefined },
outDir: 'app/public/bundle',
devOptions: { enabled: true, type: 'module' }
} and it's being loaded with the following code: if ('serviceWorker' in navigator) {
void navigator.serviceWorker.register(import.meta.env.MODE === 'production' ? '/service-worker.js' : '/bundle/dev-sw.js?dev-sw', {
type: import.meta.env.MODE === 'production' ? 'classic' : 'module',
});
} How can I get reloads to work without having to build a full PWA with this plugin? Is there some event I can set the service worker to listen to? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
if ('serviceWorker' in navigator) {
const reg = await navigator.serviceWorker.register(import.meta.env.MODE === 'production' ? '/service-worker.js' : '/bundle/dev-sw.js?dev-sw', {
type: import.meta.env.MODE === 'production' ? 'classic' : 'module',
});
await reg.update();
} It seems the missing piece of the puzzle was calling update after the worker has been registered. |
Beta Was this translation helpful? Give feedback.
It seems the missing piece of the puzzle was calling update after the worker has been registered.