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._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);

View File

@ -34,10 +34,19 @@ export class ServiceWorkerHandler {
}
registerAndStart(path) {
this._registrationPromise = (async () => {
navigator.serviceWorker.addEventListener("message", this);
navigator.serviceWorker.addEventListener("controllerchange", this);
this._registration = await navigator.serviceWorker.register(path);
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._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) {