From 3d3a177b7ccd2cb2361c0761b6cc3f03fba36541 Mon Sep 17 00:00:00 2001 From: eneller Date: Wed, 5 Mar 2025 21:40:06 +0100 Subject: [PATCH] fix: ServiceWorker intercepting requests created wrapper for SW to exclude paths because ngsw-config only disables caching, but the SW would still handle the request --- angular.json | 3 ++- src/app/app.config.ts | 2 +- src/sw.js | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/sw.js diff --git a/angular.json b/angular.json index f7fa430..433ccef 100644 --- a/angular.json +++ b/angular.json @@ -30,7 +30,8 @@ { "glob": "**/*", "input": "public" - } + }, + "src/sw.js" ], "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 19cebf7..e0331cc 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -7,7 +7,7 @@ import { provideAnimationsAsync } from '@angular/platform-browser/animations/asy import { provideServiceWorker } from '@angular/service-worker'; export const appConfig: ApplicationConfig = { - providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(withEventReplay()), provideAnimationsAsync('noop'), provideServiceWorker('ngsw-worker.js', { + providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(withEventReplay()), provideAnimationsAsync('noop'), provideServiceWorker('./sw.js', { enabled: !isDevMode(), registrationStrategy: 'registerWhenStable:30000' })] diff --git a/src/sw.js b/src/sw.js new file mode 100644 index 0000000..5dc6254 --- /dev/null +++ b/src/sw.js @@ -0,0 +1,12 @@ +self.addEventListener('fetch', event => { + if (event && + event.request && + event.request.url && + // check if basename includes a dot, i.e. if it is not a file + ! event.request.url.split(/[\\/]/).pop().includes(".") + ) { + event.stopImmediatePropagation(); + } + }); + + self.importScripts('./ngsw-worker.js'); \ No newline at end of file