Created
May 5, 2021 14:23
-
-
Save LuckeeDev/044999223e8c68e2a5da96bda34d7044 to your computer and use it in GitHub Desktop.
Angular - Check for PWA updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { SwUpdate } from '@angular/service-worker'; | |
import { DialogService } from '@csl/ui'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class UpdatesService { | |
public loading = false; | |
constructor( | |
private readonly updates: SwUpdate, | |
private dialog: DialogService | |
) { | |
this.updates.available.subscribe(() => { | |
this.showAppUpdateAlert(); | |
}); | |
} | |
showAppUpdateAlert() { | |
this.dialog.open('Do you want to update the app?').onConfirm(() => { | |
this.updates.activateUpdate().then(() => document.location.reload()); | |
}); | |
} | |
checkForUpdates() { | |
this.loading = true; | |
this.updates.checkForUpdate() | |
.then(() => this.loading = false) | |
.catch(() => { | |
console.log('SW are disabled'); | |
this.loading = false; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment