Skip to content

Instantly share code, notes, and snippets.

@whisher
Created August 7, 2018 08:11
Show Gist options
  • Save whisher/62914a169d01c1b5628fcbe6e1f237a3 to your computer and use it in GitHub Desktop.
Save whisher/62914a169d01c1b5628fcbe6e1f237a3 to your computer and use it in GitHub Desktop.
export class PwaService {
promptEvent: ServiceWorkerEvent;
constructor(private swUpdate: SwUpdate, platform: PlatformService) {
if(platform.isBrowser() && environment.production){
console.log('hello browser');
swUpdate.available.subscribe(event => {
console.log('current version is', event.current);
console.log('available version is', event.available);
if (window.confirm('Want to update?')) {
swUpdate.activateUpdate().then(() => {
console.log('Update');
document.location.reload();
});
}
});
swUpdate.activated.subscribe(event => {
console.log('old version was', event.previous);
console.log('new version is', event.current);
});
window.addEventListener('beforeinstallprompt', event => {
this.promptEvent = event;
});
this.install();
interval(6 * 60 * 60).subscribe(() => swUpdate.checkForUpdate());
}
}
install(): void {
if(this.promptEvent){
this.promptEvent.prompt();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment