Skip to content

Commit 3d3a177

Browse files
committed
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
1 parent 07b8458 commit 3d3a177

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

angular.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
{
3131
"glob": "**/*",
3232
"input": "public"
33-
}
33+
},
34+
"src/sw.js"
3435
],
3536
"styles": [
3637
"node_modules/bootstrap/dist/css/bootstrap.min.css",

src/app/app.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { provideAnimationsAsync } from '@angular/platform-browser/animations/asy
77
import { provideServiceWorker } from '@angular/service-worker';
88

99
export const appConfig: ApplicationConfig = {
10-
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(withEventReplay()), provideAnimationsAsync('noop'), provideServiceWorker('ngsw-worker.js', {
10+
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(withEventReplay()), provideAnimationsAsync('noop'), provideServiceWorker('./sw.js', {
1111
enabled: !isDevMode(),
1212
registrationStrategy: 'registerWhenStable:30000'
1313
})]

src/sw.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
self.addEventListener('fetch', event => {
2+
if (event &&
3+
event.request &&
4+
event.request.url &&
5+
// check if basename includes a dot, i.e. if it is not a file
6+
! event.request.url.split(/[\\/]/).pop().includes(".")
7+
) {
8+
event.stopImmediatePropagation();
9+
}
10+
});
11+
12+
self.importScripts('./ngsw-worker.js');

0 commit comments

Comments
 (0)