I am facing an issue with the service worker. The service worker is not registering on its own through the app.module. So I am manually registering it in main.ts. It works fine in online mode. But when I change the network to offline mode, getting ngsw.json?ngsw-cache-bust failing. Any solution will be helpful.
main.ts
platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('/ngsw-worker.js');
}
}).catch(err => console.log(err));
ngsw-config.json
{
"$schema": "./node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/manifest.webmanifest",
"/*.css",
"/*.js"
]
}
}, {
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
]
}
}
]
}
This is normal behavior once offline with the Angular service worker. What's happening is the service worker is trying to do a
GET
on thengsw.json
file using cache-busting, but it can't access it, because the app is offline. The fetch will fail and the call will look something similar to the following with a unique value:ngsw.json?ngsw-cache-bust=0.7064947087867681
It's trying to get the service worker configuration to see what needs updated, but can't access the file. However, this should not impede the service worker from serving up anything it's already cached, and operate normally until back online. Once online you will see the call to the
ngsw.json
file via aGET
succeed once again.