Skip to content

Instantly share code, notes, and snippets.

@LuckeeDev
Created May 5, 2021 14:23
Show Gist options
  • Save LuckeeDev/044999223e8c68e2a5da96bda34d7044 to your computer and use it in GitHub Desktop.
Save LuckeeDev/044999223e8c68e2a5da96bda34d7044 to your computer and use it in GitHub Desktop.
Angular - Check for PWA updates
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