diff --git a/src/helpers/page/ServiceWorkerUtilHelper.ts b/src/helpers/page/ServiceWorkerUtilHelper.ts index 21a3ac94b..1839c3f95 100644 --- a/src/helpers/page/ServiceWorkerUtilHelper.ts +++ b/src/helpers/page/ServiceWorkerUtilHelper.ts @@ -13,11 +13,11 @@ export default class ServiceWorkerUtilHelper { } // A ServiceWorkerRegistration will have a ServiceWorker in 1 of 3 states, get which ever is available. - static getAvailableServiceWorker(registration: ServiceWorkerRegistration): ServiceWorker { + static getAvailableServiceWorker(registration: ServiceWorkerRegistration): ServiceWorker | null { const availableWorker = registration.active || registration.installing || registration.waiting; - // This should never throw unless ServiceWorkerRegistration is pointing to a worker that is completely gone. + // This never be null unless ServiceWorkerRegistration is pointing to a worker that is completely gone. if (!availableWorker) { - throw new Error("Could not find an available ServiceWorker instance!"); + Log.warn("Could not find an available ServiceWorker instance!"); } return availableWorker; } diff --git a/src/libraries/WorkerMessenger.ts b/src/libraries/WorkerMessenger.ts index 04eb8e0ea..8db88122d 100644 --- a/src/libraries/WorkerMessenger.ts +++ b/src/libraries/WorkerMessenger.ts @@ -152,8 +152,14 @@ export class WorkerMessenger { return; } + const availableWorker = ServiceWorkerUtilHelper.getAvailableServiceWorker(workerRegistration); + if (!availableWorker) { + Log.error("`[Worker Messenger] [Page -> SW] Could not get ServiceWorker to postMessage!"); + return; + } + // The postMessage payload will still arrive at the SW even if it isn't active yet. - ServiceWorkerUtilHelper.getAvailableServiceWorker(workerRegistration).postMessage({ + availableWorker.postMessage({ command: command, payload: payload }); diff --git a/src/managers/ServiceWorkerManager.ts b/src/managers/ServiceWorkerManager.ts index effd89930..2ed0b7470 100644 --- a/src/managers/ServiceWorkerManager.ts +++ b/src/managers/ServiceWorkerManager.ts @@ -83,6 +83,10 @@ export class ServiceWorkerManager { // Get the file name of the active ServiceWorker private static activeSwFileName(workerRegistration: ServiceWorkerRegistration): string | null { const serviceWorker = ServiceWorkerUtilHelper.getAvailableServiceWorker(workerRegistration); + if (!serviceWorker) { + return null; + } + const workerScriptPath = new URL(serviceWorker.scriptURL).pathname; const swFileName = new Path(workerScriptPath).getFileName();