From cab5fe98d1eee60301de885860f633c2a8a4ef56 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 15 Dec 2022 14:55:00 +0000 Subject: [PATCH 1/2] Make registerAndStart function async --- src/platform/web/dom/ServiceWorkerHandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platform/web/dom/ServiceWorkerHandler.js b/src/platform/web/dom/ServiceWorkerHandler.js index 0bb8c7256b..2bb3895056 100644 --- a/src/platform/web/dom/ServiceWorkerHandler.js +++ b/src/platform/web/dom/ServiceWorkerHandler.js @@ -33,8 +33,8 @@ export class ServiceWorkerHandler { this._navigation = navigation; } - registerAndStart(path) { - this._registrationPromise = (async () => { + async registerAndStart(path) { + return this._registrationPromise = (async () => { navigator.serviceWorker.addEventListener("message", this); navigator.serviceWorker.addEventListener("controllerchange", this); this._registration = await navigator.serviceWorker.register(path); From 46f7a9b2a9b03f41a58d475ef214e4bee15c6f5c Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 15 Dec 2022 14:59:43 +0000 Subject: [PATCH 2/2] Start the service worker before doing anything else --- src/platform/web/Platform.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/platform/web/Platform.js b/src/platform/web/Platform.js index 0d95e5852d..e6722e586e 100644 --- a/src/platform/web/Platform.js +++ b/src/platform/web/Platform.js @@ -144,7 +144,6 @@ export class Platform { this._serviceWorkerHandler = null; if (assetPaths.serviceWorker && "serviceWorker" in navigator) { this._serviceWorkerHandler = new ServiceWorkerHandler(); - this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker); } this.notificationService = undefined; // Only try to use crypto when olm is provided @@ -172,6 +171,12 @@ export class Platform { async init() { try { + if (this._serviceWorkerHandler instanceof ServiceWorkerHandler) { + // Start the service worker before doing anything else, + // to make sure all requests are intercepted by the service worker. + await this._serviceWorkerHandler.registerAndStart(this._assetPaths.serviceWorker) + } + await this.logger.run("Platform init", async (log) => { if (!this._config) { if (!this._configURL) {