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

前端 未结 1 1269
臣服心动
臣服心动 2020-12-06 23:50

services/pwa.service.ts:

import { Injectable } from \'@angular/core\';
import { SwUpdate } from \'@angular/service-worker\';
import {Observable} from \"rxjs/         


        
相关标签:
1条回答
  • 2020-12-07 00:41

    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();
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题