diff --git a/package.json b/package.json index bdc7aae..69a95be 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,10 @@ "version": "0.0.0", "scripts": { "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": { "/callback": { @@ -62,4 +65,4 @@ "ts-interface-builder": "^0.3.3", "typescript": "~4.9.5" } -} \ No newline at end of file +} diff --git a/src/app/app.component.ts b/src/app/app.component.ts index df2dcbb..8b7ba6d 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { AuthService } from './shared/auth/auth.service'; +import {UpdateService} from "./shared/service-worker/update.service"; @Component({ selector: 'app-root', @@ -7,10 +8,12 @@ import { AuthService } from './shared/auth/auth.service'; styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { - + title = 'frontend-hideyoshi.com'; - constructor(private authService: AuthService) {} + constructor(private authService: AuthService, private serviceWorker: UpdateService) { + this.serviceWorker.checkForUpdates(); + } ngOnInit(): void { this.authService.autoLogin(); diff --git a/src/app/shared/service-worker/update.service.spec.ts b/src/app/shared/service-worker/update.service.spec.ts new file mode 100644 index 0000000..b51fcc8 --- /dev/null +++ b/src/app/shared/service-worker/update.service.spec.ts @@ -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(); + }); +}); diff --git a/src/app/shared/service-worker/update.service.ts b/src/app/shared/service-worker/update.service.ts new file mode 100644 index 0000000..9cb21ef --- /dev/null +++ b/src/app/shared/service-worker/update.service.ts @@ -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()); + } + +}