@angular/pwa service worker 没有在我已经存在的项目上工作
@angular/pwa service worker not working on my already existing project
我正在尝试向我的(已经存在的)项目中添加一个 service worker。
但是当我打开浏览器时,我发现它没有获取 service worker。
这些是我遵循的步骤:
- 运行
ng add @angular/pwa --project <my-project-name>
到我的项目
- 运行
ng build --prod
- 运行 应用
http-server -p 8080 -c-1 dist/<my-project-name>
现在,应用程序 运行,它确实获取了清单文件,但没有获取 Service Worker:
manifest.webmanifest:
ngsw-config.json:
当我尝试在一个全新的项目上执行上述所有步骤时,它确实有效并得到了服务人员。但不在我已经存在的项目上。
尝试将 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);
我正在尝试向我的(已经存在的)项目中添加一个 service worker。 但是当我打开浏览器时,我发现它没有获取 service worker。
这些是我遵循的步骤:
- 运行
ng add @angular/pwa --project <my-project-name>
到我的项目 - 运行
ng build --prod
- 运行 应用
http-server -p 8080 -c-1 dist/<my-project-name>
现在,应用程序 运行,它确实获取了清单文件,但没有获取 Service Worker:
manifest.webmanifest:
ngsw-config.json:
当我尝试在一个全新的项目上执行上述所有步骤时,它确实有效并得到了服务人员。但不在我已经存在的项目上。
尝试将 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);