anticipate firefox throwing a SecurityError from sw.register()

and just proceed as if they were not supported
This commit is contained in:
Bruno Windels 2021-04-01 17:41:47 +02:00
parent 1ac36cbd23
commit 91949a0006
2 changed files with 17 additions and 5 deletions

View file

@ -100,8 +100,10 @@ export class Platform {
this.onlineStatus = new OnlineStatus(); this.onlineStatus = new OnlineStatus();
this._serviceWorkerHandler = null; this._serviceWorkerHandler = null;
if (config.serviceWorker && "serviceWorker" in navigator) { if (config.serviceWorker && "serviceWorker" in navigator) {
this._serviceWorkerHandler = new ServiceWorkerHandler(); const serviceWorkerHandler = new ServiceWorkerHandler();
this._serviceWorkerHandler.registerAndStart(config.serviceWorker); if (serviceWorkerHandler.registerAndStart(config.serviceWorker)) {
this._serviceWorkerHandler = serviceWorkerHandler;
}
} }
this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push); this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push);
this.crypto = new Crypto(cryptoExtras); this.crypto = new Crypto(cryptoExtras);

View file

@ -34,10 +34,19 @@ export class ServiceWorkerHandler {
} }
registerAndStart(path) { 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 () => { this._registrationPromise = (async () => {
navigator.serviceWorker.addEventListener("message", this); this._registration = await registrationPromise;
navigator.serviceWorker.addEventListener("controllerchange", this);
this._registration = await navigator.serviceWorker.register(path);
await navigator.serviceWorker.ready; await navigator.serviceWorker.ready;
this._currentController = navigator.serviceWorker.controller; this._currentController = navigator.serviceWorker.controller;
this._registration.addEventListener("updatefound", this); this._registration.addEventListener("updatefound", this);
@ -48,6 +57,7 @@ export class ServiceWorkerHandler {
} }
console.log("Service Worker registered"); console.log("Service Worker registered");
})(); })();
return true;
} }
_onMessage(event) { _onMessage(event) {