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
This commit is contained in:
eneller
2025-03-05 21:40:06 +01:00
parent 07b84586b6
commit 3d3a177b7c
3 changed files with 15 additions and 2 deletions

View File

@@ -30,7 +30,8 @@
{ {
"glob": "**/*", "glob": "**/*",
"input": "public" "input": "public"
} },
"src/sw.js"
], ],
"styles": [ "styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css", "node_modules/bootstrap/dist/css/bootstrap.min.css",

View File

@@ -7,7 +7,7 @@ import { provideAnimationsAsync } from '@angular/platform-browser/animations/asy
import { provideServiceWorker } from '@angular/service-worker'; import { provideServiceWorker } from '@angular/service-worker';
export const appConfig: ApplicationConfig = { 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(), enabled: !isDevMode(),
registrationStrategy: 'registerWhenStable:30000' registrationStrategy: 'registerWhenStable:30000'
})] })]

12
src/sw.js Normal file
View File

@@ -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');