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

@@ -3,7 +3,10 @@
"version": "0.0.0", "version": "0.0.0",
"scripts": { "scripts": {
"start": "node ./server.js", "start": "node ./server.js",
"serve": "ng serve" "build": "ng build",
"serve": "ng serve",
"serve:prod": "ng serve --configuration=production",
"build:prod": "ng build --configuration=production"
}, },
"proxy": { "proxy": {
"/callback": { "/callback": {
@@ -62,4 +65,4 @@
"ts-interface-builder": "^0.3.3", "ts-interface-builder": "^0.3.3",
"typescript": "~4.9.5" "typescript": "~4.9.5"
} }
} }

View File

@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { AuthService } from './shared/auth/auth.service'; import { AuthService } from './shared/auth/auth.service';
import {UpdateService} from "./shared/service-worker/update.service";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@@ -7,10 +8,12 @@ import { AuthService } from './shared/auth/auth.service';
styleUrls: ['./app.component.css'] styleUrls: ['./app.component.css']
}) })
export class AppComponent implements OnInit { export class AppComponent implements OnInit {
title = 'frontend-hideyoshi.com'; title = 'frontend-hideyoshi.com';
constructor(private authService: AuthService) {} constructor(private authService: AuthService, private serviceWorker: UpdateService) {
this.serviceWorker.checkForUpdates();
}
ngOnInit(): void { ngOnInit(): void {
this.authService.autoLogin(); this.authService.autoLogin();

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());
}
}