From 1f6a723cfeffef5200112cdcc14a2c6fd2a364f6 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 20 Oct 2022 15:21:13 +0100 Subject: [PATCH] Silently apply updates instead of asking user to confirm --- frontend/platform/ServiceWorkerHandler.ts | 31 ++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/frontend/platform/ServiceWorkerHandler.ts b/frontend/platform/ServiceWorkerHandler.ts index 27daa523..eb094944 100644 --- a/frontend/platform/ServiceWorkerHandler.ts +++ b/frontend/platform/ServiceWorkerHandler.ts @@ -1,11 +1,40 @@ import { ServiceWorkerHandler as BaseHandler } from "hydrogen-web/src/platform/web/dom/ServiceWorkerHandler.js"; export class ServiceWorkerHandler extends BaseHandler { + private get registration() { + return super.getRegistration(); + } + public registerAndStart(path: string) { super.registerAndStart(path); } public async preventConcurrentSessionAccess(sessionId: string) { - return super._sendAndWaitForReply("closeSession", { sessionId }); + return this._sendAndWaitForReply("closeSession", { sessionId }); + } + + async _proposeUpdate() { + if (document.hidden) { + return; + } + + const version = await this._sendAndWaitForReply("version", null, this.registration.waiting); + + // Prevent any fetch requests from going to the service worker from any client, + // so that it's not kept active when calling skipWaiting on the new one. + await this._sendAndWaitForReply("haltRequests", null); + + console.log(`Updating to version ${version.version} (${version.buildHash})`); + + // Once all requests are blocked, ask the new service worker to skipWaiting. + return this._send("skipWaiting", null, this.registration.waiting); + } + + async _sendAndWaitForReply(type, payload: {}|null, worker = undefined) { + return super._sendAndWaitForReply(type, payload, worker); + } + + async _send(type, payload, worker = undefined) { + return super._send(type, payload, worker); } }