Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

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

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",
....
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This worked for me on all devices:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...