diff --git a/src/platform/web/Platform.js b/src/platform/web/Platform.js index 549d49c3..cd6a3c68 100644 --- a/src/platform/web/Platform.js +++ b/src/platform/web/Platform.js @@ -100,8 +100,10 @@ export class Platform { this.onlineStatus = new OnlineStatus(); this._serviceWorkerHandler = null; if (config.serviceWorker && "serviceWorker" in navigator) { - this._serviceWorkerHandler = new ServiceWorkerHandler(); - this._serviceWorkerHandler.registerAndStart(config.serviceWorker); + const serviceWorkerHandler = new ServiceWorkerHandler(); + if (serviceWorkerHandler.registerAndStart(config.serviceWorker)) { + this._serviceWorkerHandler = serviceWorkerHandler; + } } this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push); this.crypto = new Crypto(cryptoExtras); diff --git a/src/platform/web/dom/ServiceWorkerHandler.js b/src/platform/web/dom/ServiceWorkerHandler.js index dd2c755f..b40a3b35 100644 --- a/src/platform/web/dom/ServiceWorkerHandler.js +++ b/src/platform/web/dom/ServiceWorkerHandler.js @@ -34,10 +34,19 @@ export class ServiceWorkerHandler { } registerAndStart(path) { + navigator.serviceWorker.addEventListener("message", this); + navigator.serviceWorker.addEventListener("controllerchange", this); + let registrationPromise; + try { + // this can throw a SecurityError in Firefox with hardened settings + registrationPromise = navigator.serviceWorker.register(path); + } catch (err) { + navigator.serviceWorker.removeEventListener("message", this); + navigator.serviceWorker.removeEventListener("controllerchange", this); + return false; + } this._registrationPromise = (async () => { - navigator.serviceWorker.addEventListener("message", this); - navigator.serviceWorker.addEventListener("controllerchange", this); - this._registration = await navigator.serviceWorker.register(path); + this._registration = await registrationPromise; await navigator.serviceWorker.ready; this._currentController = navigator.serviceWorker.controller; this._registration.addEventListener("updatefound", this); @@ -48,6 +57,7 @@ export class ServiceWorkerHandler { } console.log("Service Worker registered"); })(); + return true; } _onMessage(event) {