Adds ServiceWorker Service

Adds ServiceWorker Service for Updating PWA
This commit is contained in:
2023-08-28 03:48:05 -03:00
parent f6afe859c3
commit da3382b74e
4 changed files with 52 additions and 4 deletions

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { UpdateService } from './update.service';
describe('UpdateService', () => {
let service: UpdateService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(UpdateService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import {SwUpdate} from "@angular/service-worker";
import {interval} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class UpdateService {
constructor(private swUpdate: SwUpdate) {
if (swUpdate.isEnabled) {
interval(6 * 60 * 60).subscribe(() => swUpdate.checkForUpdate()
.then(() => console.log('checking for updates')));
}
}
public checkForUpdates(): void {
this.swUpdate.available.subscribe(event => this.promptUser());
}
private promptUser(): void {
console.log('updating to new version');
this.swUpdate.activateUpdate().then(() => document.location.reload());
}
}