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

ric*_*son 2 javascript browser service-worker progressive-web-apps angular

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:

在此处输入图片说明

manifest.webmanifest:

在此处输入图片说明

ngsw-config.json:

在此处输入图片说明

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.

abr*_*ham 5

尝试将 service worker 设置为registerImmediately这样:

ServiceWorkerModule.register('ngsw-worker.js', {
  enabled: environment.production,
  registrationStrategy: 'registerImmediately',
})
Run Code Online (Sandbox Code Playgroud)

如果您有一个使用类似setIntervalservice worker的第三方库,则永远不会注册。这是因为默认registerWhenStable依赖于isStable重复性任务永远不会发生。

如果您在应用程序启动时启动任何类型的循环异步任务,应用程序将永远不会稳定(例如,对于轮询过程,以 setInterval、setTimeout 或使用 RxJS 运算符(如 interval)启动);

  • 我花了大约 5 - 8 个小时尝试完成这项工作,在网上到处搜索,你的答案为我解决了这个问题。我无法告诉你我是多么感谢你的回答!谢谢 (2认同)