Skip to content

Commit 545b5bf

Browse files
committed
refactor: only check if service-worker.ts is removed to unregister
1 parent 1333911 commit 545b5bf

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

.changeset/thin-horses-bake.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
'@builder.io/qwik-city': patch
33
---
44

5-
FIX: Your service-worker.js won't be unregistered anymore if you added custom logic to it.
5+
FIX: Your service-worker.js won't be unregistered anymore if you added custom logic to it.
6+
7+
> Note: if you didn't add custom service-worker logic, you should remove your service-worker.ts file(s) for the `ServiceWorkerRegister` Component to actually unregister the service-worker.js and delete its related cache.

packages/qwik-city/src/buildtime/runtime-generation/generate-service-worker.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import type { BuildContext } from '../types';
22

3-
export function generateServiceWorkerRegister(
4-
ctx: BuildContext,
5-
swRegister: string,
6-
didNotAddCustomCode: boolean
7-
) {
3+
export function generateServiceWorkerRegister(ctx: BuildContext, swRegister: string) {
84
let swReg: string;
95
let swUrl = '/service-worker.js';
106

11-
// Also unregister if the developer removed the service-worker.ts file or did not add custom code to it; since Qwik 1.14.0 and above now use modulepreload by default
12-
if (ctx.isDevServer || didNotAddCustomCode || ctx.serviceWorkers.length === 0) {
7+
// Also unregister if the developer removed the service-worker.ts file since Qwik 1.14.0 and above now use modulepreload by default
8+
if (ctx.isDevServer || ctx.serviceWorkers.length === 0) {
139
swReg = SW_UNREGISTER;
1410
} else {
1511
swReg = swRegister;

packages/qwik-city/src/buildtime/vite/plugin.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -186,35 +186,12 @@ function qwikCityPlugin(userOpts?: QwikCityVitePluginOptions): any {
186186
}
187187

188188
if (isSwRegister) {
189-
// Detect if the developer added custom code in their service-worker.ts file
190-
let didNotAddCustomCode = false;
191-
const clientOutDir = qwikPlugin!.api.getClientOutDir();
192-
if (clientOutDir) {
193-
const basePathRelDir = api.getBasePathname().replace(/^\/|\/$/, '');
194-
const clientOutBaseDir = join(clientOutDir, basePathRelDir);
195-
for (const swEntry of ctx.serviceWorkers) {
196-
try {
197-
const swClientDistPath = join(clientOutBaseDir, swEntry.chunkFileName);
198-
const swCode = await fs.promises.readFile(swClientDistPath, 'utf-8');
199-
const swCodeTrimmed = swCode.replace(/\s+/g, '');
200-
if (
201-
swCodeTrimmed ===
202-
'addEventListener("install",()=>self.skipWaiting());addEventListener("activate",()=>self.clients.claim());' ||
203-
swCodeTrimmed ===
204-
'self.addEventListener("install",()=>self.skipWaiting());self.addEventListener("activate",()=>self.clients.claim());'
205-
) {
206-
didNotAddCustomCode = true;
207-
}
208-
} catch (e) {
209-
console.error('Error reading service worker file', e);
210-
}
211-
}
212-
}
213189
// @qwik-city-sw-register
214-
return generateServiceWorkerRegister(ctx, swRegister, didNotAddCustomCode);
190+
return generateServiceWorkerRegister(ctx, swRegister);
215191
}
216192
}
217193
}
194+
218195
return null;
219196
},
220197

packages/qwik-city/src/runtime/src/sw-component.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import type { JSXOutput } from '@builder.io/qwik';
55
* JS extensions are allowed) will be picked up, bundled into a separate file, and registered as a
66
* service worker.
77
*
8+
* Qwik 1.14.0 and above now use `<link rel="modulepreload">` by default. If you didn't add custom
9+
* service-worker logic, you should remove your service-worker.ts file(s) for the
10+
* `ServiceWorkerRegister` Component to actually unregister the service-worker.js and delete its
11+
* related cache.
12+
*
813
* @public
914
*/
1015
export const ServiceWorkerRegister = (props: { nonce?: string }): JSXOutput => (

0 commit comments

Comments
 (0)