Skip to content

Instantly share code, notes, and snippets.

@whisher
Created July 29, 2018 12:54
Show Gist options
  • Save whisher/bd8a0e01ac197f15638ed2899e172da2 to your computer and use it in GitHub Desktop.
Save whisher/bd8a0e01ac197f15638ed2899e172da2 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
import { PlatformService } from '../platform/platform.service';
type ServiceWorkerEvent = any;
@Injectable({
providedIn: 'root'
})
export class PwaService {
//promptEvent: BeforeInstallPromptEvent as EventTarget;
promptEvent: ServiceWorkerEvent;
constructor(private swUpdate: SwUpdate, platform: PlatformService) {
if(platform.isBrowser()){
swUpdate.available.subscribe(event => {
if (window.confirm('Want to update?')) {
window.location.reload();
}
});
window.addEventListener('beforeinstallprompt', event => {
this.promptEvent = event;
});
}
}
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