javascript - @angular/pwa service worker 没有在我已经存在的项目上工作

标签 javascript angular browser service-worker progressive-web-apps

我正在尝试向我的(已经存在的)项目中添加一个服务 worker 。 但是当我打开浏览器时,我发现它没有获取 service worker。

这些是我遵循的步骤:

  1. 正在运行 ng add @angular/pwa --project <my-project-name>到我的项目
  2. 正在运行 ng build --prod
  3. 使用 http-server -p 8080 -c-1 dist/<my-project-name> 运行应用程序

现在,应用程序正在运行,它确实获得了 list 文件,但没有获得 service worker:

enter image description here

list .webmanifest:

enter image description here

ngsw-config.json:

enter image description here

当我尝试在一个全新的项目上执行上述所有步骤时,它确实有效并得到了 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/

相关文章:

javascript - onclick 也重定向到 "form action"

javascript - Angular - 错误类型错误 : _co. timeIn 不是函数

.net - 在 .NET Windows 服务中解析 HTML 和获取资源

javascript - jQuery - 如何动态添加到列表元素

javascript - 使用selenium通过window.open下载文件

javascript - 如何在 ngFor 循环中使用 ngb-accordion?

javascript - 如何将 HTML 保存到变量中

javascript - Internet Explorer 不会读取对象属性

css - 我可以为不属于我的网站设置 CSS 规则吗?

javascript - Ajax 驱动的 JavaScript 运行时断言框架