我正在尝试向我的(已经存在的)项目中添加一个服务 worker 。 但是当我打开浏览器时,我发现它没有获取 service worker。
这些是我遵循的步骤:
- 正在运行
ng add @angular/pwa --project <my-project-name>
到我的项目 - 正在运行
ng build --prod
- 使用
http-server -p 8080 -c-1 dist/<my-project-name>
运行应用程序
现在,应用程序正在运行,它确实获得了 list 文件,但没有获得 service worker:
list .webmanifest:
ngsw-config.json:
当我尝试在一个全新的项目上执行上述所有步骤时,它确实有效并得到了 service worker。但不是在我已经存在的项目上。
最佳答案
尝试将 service worker 设置为 registerImmediately
像这样:
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: environment.production,
registrationStrategy: 'registerImmediately',
})
如果你有一个第三方库使用类似 setInterval
的东西,service worker 将永远不会注册。这是因为默认的 registerWhenStable
依赖于 isStable
重复性任务永远不会发生这种情况。
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);
关于javascript - @angular/pwa service worker 没有在我已经存在的项目上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60549712/