@angular/pwa service worker not working on my already existing project

Viewed 2836

I'm trying to add a service worker to my (already existing) project. but when i open the browser, i see that it doesn't get the service worker.

These are the steps i followed:

  1. Running ng add @angular/pwa --project <my-project-name> to my project
  2. Running ng build --prod
  3. Running the app with http-server -p 8080 -c-1 dist/<my-project-name>

Now, the app is running, and it does get the manifest file but not the service worker:

enter image description here

manifest.webmanifest:

enter image description here

ngsw-config.json:

enter image description here

When i tried to do all of the steps above on a brand new project it did work and got the service worker. but not on my already existing project.

1 Answers

Try setting the service worker to registerImmediately like this:

ServiceWorkerModule.register('ngsw-worker.js', {
  enabled: environment.production,
  registrationStrategy: 'registerImmediately',
})

If you have a third-party library that uses something like setInterval the service worker will never register. This is because the default registerWhenStable relies on isStable which will never happen with recurring tasks.

the application will never be stable if you start any kind of recurrent asynchronous task when the application starts (for example for a polling process, started with a setInterval, a setTimeout or using RxJS operators like interval);

Related