Angular Service Worker SwUpdate.available not triggered

前端 未结 6 2378
旧巷少年郎
旧巷少年郎 2020-12-13 18:51

I\'m having a hard time integrating angulars service worker into my application. I followed the guide and it works so far. I can create a shortcut on my homescreen and launc

6条回答
  •  孤街浪徒
    2020-12-13 19:18

    This worked for me on all devices mac/windows/ios/android

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

提交回复
热议问题