3
votes

I followed this guide: https://angular.io/guide/service-worker-getting-started

In an existing Angular CLI app I ran

ng add @angular/pwa --project *project-name*

ng build --prod

npm start -c-1 dist/<project-name>

Chrome Dev tools > Application Manifest - detected, no errors or warnings Service Workers - empty

Network tab loads every file with 200

ngsw-config.json - exists generated by the command assets/icons - directory generated app.module.ts - imports array contains ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })

Running PWA audit returns an error with ServiceWorker not registered.

1
I did notice that executing the same exact steps on a vm running Linux had actually worked and the service worker was registered. Windows seems to be not correctly registering the service worker - O.MeeKoh
I did the same thing as written in angular docs, and service worker is not registered inside chrome. Don't know how to fix this :/ - tzm
have you guys found a solution to this? I have started running out of ideas... - MrfksIV
according to some users in github this is because some 3rd party libraries are changing some application state and it breaks service worker registration - karoluS
Any chance you can drop a reference to these discussions? - O.MeeKoh

1 Answers

4
votes

I had the same issue. Adding this to main.ts resolved the problem.

platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
if ('serviceWorker' in navigator && environment.production) {
    navigator.serviceWorker.register('ngsw-worker.js');
}
}).catch(err => console.log(err))