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)"
]
}
}
]
}
I think your problem lies somewhere else. Can you show the
ngsw-config.jsonfile?In DevTools is the service worker correctly registered and active?
What exactly does not work as expected while offline? Is it just the ngsw.json file returning an exception while attempting to fetch it while offline?
UPDATE
How did you set the
scopeandstart_urlproperty in the web manifest? With the new angular CLI the values are set by default to"./"(notice the dot), while in previous versions the dot was missing and in some setups led to path issues.Even though probably is not your case, as you can add the app to the home screen, meaning everything is properly setup.