events - 绑定更新 PWA:swUpdate.isEnabled 为 true,但即使 ngsw-config.json 更改也不调用订阅的方法
问题描述
服务/pwa.service.ts:
import { Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
import {Observable} from "rxjs/Observable";
import "rxjs/add/observable/interval";
@Injectable()
export class PwaService {
public promptEvent: any;
constructor(private swUpdate: SwUpdate) {
alert('swUpdate isEnabled:' + swUpdate.isEnabled);// => alerts true
if (swUpdate.isEnabled) {
Observable.interval(10)
.subscribe(() => swUpdate.checkForUpdate().then(() => alert('checking for swUpdate')));//<= Not triggered
}
}
public checkForUpdates(): void {
this.swUpdate.available.subscribe(event => this.promptUser());
}
private promptUser(): void {
alert('updating to new version');//<=Not triggered either
this.swUpdate.activateUpdate()
.then(() => document.location.reload());
}
}
服务/index.ts:
providers: [
....
{ provide: SwUpdate, useClass: SwUpdate }
]
app.modules.ts:
imports: [
....
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
]
providers: [
...
PwaService,
]
app.component.ts:
import { PwaService } from './services/pwa.service';
....
constructor(public Pwa: PwaService) {
this.Pwa.checkForUpdates();
}
ngsw-config.json(只是从lazy
到的微小变化prefetch
)来触发更新:
....
"installMode": "prefetch",
....
解决方案
这在所有设备上都对我有用:
export class PwaUpdateService {
updateSubscription;
constructor(public updates: SwUpdate) {
}
public checkForUpdates(): void {
this.updateSubscription = this.updates.available.subscribe(event => this.promptUser());
if (this.updates.isEnabled) {
// Required to enable updates on Windows and ios.
this.updates.activateUpdate();
interval(60 * 60 * 1000).subscribe(() => {
this.updates.checkForUpdate().then(() => {
// console.log('checking for updates');
});
});
}
// Important: on Safari (ios) Heroku doesn't auto redirect links to their https which allows the installation of the pwa like usual
// but it deactivates the swUpdate. So make sure to open your pwa on safari like so: https://example.com then (install/add to home)
}
promptUser(): void {
this.updates.activateUpdate().then(() => {
window.location.reload();
});
}
}
推荐阅读
- math - 获取顶点之间的边(外多边形)
- json - .post 之前的法拉第 Json 验证
- powershell - 将文本文件重命名为内容文本(批处理)(powershell)
- email - 詹金斯 - 构建失败后发送电子邮件与多个收件人
- c# - 是否可以使用 CodeEffects 仅返回 rule 的文本?
- java - 我可以得到帮助来解决这个问题吗?
- powerapps - 日期功能不适用于 PowerApps 中的标签
- java - Java - 未为 do-while 循环初始化变量
- python - 向 matplotlib 中的图形添加文本
- typescript - 为我选择的每个项目设置样式?