-
-
Save michaeldoye/d48a45c2f3f634cc215c183929406bd0 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core'; | |
import { SwUpdate } from '@angular/service-worker'; | |
import { interval } from 'rxjs'; | |
import { MatSnackBar } from '@angular/material'; | |
@Injectable() | |
export class WorkerService { | |
constructor(public updates: SwUpdate, public snackBar: MatSnackBar) { | |
// If updates are enabled | |
if (updates.isEnabled) { | |
// poll the service worker to check for updates | |
interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate()); | |
} | |
} | |
// Called from app.components.ts constructor | |
public checkForUpdates() { | |
if (this.updates.isEnabled) { | |
this.updates.available.subscribe(event => { | |
console.log('current version is', event.current); | |
console.log('available version is', event.available); | |
this.promptUser(event); | |
}); | |
this.updates.activated.subscribe(event => { | |
console.log('old version was', event.previous); | |
console.log('new version is', event.current); | |
}); | |
} | |
} | |
// If there is an update, promt the user | |
private promptUser(e): void { | |
if(e.available) { | |
let snackBarRef = this.snackBar.open( | |
'A new version of the dashboard is available', | |
'Update', | |
{horizontalPosition: 'left'} | |
); | |
snackBarRef.onAction().subscribe(() => document.location.reload()); | |
} | |
} | |
} |
Hi, i get this:
StaticInjectorError(Platform: core)[SwUpdate]:
NullInjectorError: No provider for SwUpdate!
when m injecting SwUpdate in my constructor.
did you add the service worker itself to your project?
If not, try out " ng add @angular/pwa --project project-name "
This command will add the service-worker package to your project :)
For further information read the Angular-Getting started with service workers Guide: https://angular.io/guide/service-worker-getting-started
Hopefully I was able to help you :)
is there a way to look for changes in the app content?
In App A I have a vehicle list and each vehicle has a testset. If I add a testset to the vehicle and activate the testset in App B, I want to get informed about a new Testset in Vehicle XY in the App A. But I just get it work, when I change the code, not when the data in the app is changed.
Do you have an idea how i could reach this?
Is there a reason you use document.location.reload() over window.location.reload()?