Tying to update PWA: swUpdate.isEnabled is true but not calling the subscribed method even when ngsw-config.json is altered

北慕城南 提交于 2019-11-26 23:44:02

问题


services/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());
  }
}

services/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(just minor change from lazy to prefetch) to trigger update:

....
"installMode": "prefetch",
....

来源:https://stackoverflow.com/questions/55797611/tying-to-update-pwa-swupdate-isenabled-is-true-but-not-calling-the-subscribed-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!