diff --git a/.changeset/early-eels-listen.md b/.changeset/early-eels-listen.md new file mode 100644 index 00000000000..1e6e43578c8 --- /dev/null +++ b/.changeset/early-eels-listen.md @@ -0,0 +1,6 @@ +--- +'@builder.io/qwik': patch +'@builder.io/qwik-city': patch +--- + +PATCH: Keeping the service worker components now properly unregisters them. diff --git a/packages/qwik-city/src/runtime/src/sw-register.ts b/packages/qwik-city/src/runtime/src/sw-register.ts index a670d0f2944..7a88ecd67e8 100644 --- a/packages/qwik-city/src/runtime/src/sw-register.ts +++ b/packages/qwik-city/src/runtime/src/sw-register.ts @@ -2,7 +2,14 @@ (() => { if ('serviceWorker' in navigator) { - navigator.serviceWorker.register('__url').catch((e) => console.error(e)); + navigator.serviceWorker.getRegistrations().then((regs) => { + for (const reg of regs) { + const url = '__url'.split('/').pop(); + if (reg.active?.scriptURL.endsWith(url || 'service-worker.js')) { + reg.unregister().catch(console.error); + } + } + }); } else { // eslint-disable-next-line no-console console.log('Service worker not supported in this browser.'); diff --git a/packages/qwik/src/core/components/prefetch.ts b/packages/qwik/src/core/components/prefetch.ts index 679152242b5..01b104cb93f 100644 --- a/packages/qwik/src/core/components/prefetch.ts +++ b/packages/qwik/src/core/components/prefetch.ts @@ -42,7 +42,7 @@ export const PrefetchServiceWorker = (opts: { // the file 'qwik-prefetch-service-worker.js' is not located in /build/ resolvedOpts.path = baseUrl + resolvedOpts.path; } - let code = PREFETCH_CODE.replace("'_URL_'", JSON.stringify(resolvedOpts.path)); + let code = PREFETCH_CODE.replace('"_URL_"', JSON.stringify(resolvedOpts.path.split('/').pop())); if (!isDev) { // consecutive spaces are indentation code = code.replaceAll(/\s\s+/gm, '');