Archived
1
0
This commit is contained in:
Stefano Dissegna 2021-08-25 09:08:24 +02:00
parent 3e55e5e1df
commit d26858b6ad

View File

@ -242,7 +242,28 @@ const workerReady = new Promise(async (resolve, reject) => {
} }
}; };
navigator.serviceWorker.addEventListener('message', versionHandler); navigator.serviceWorker.addEventListener('message', versionHandler);
assertIsDefined(registration.active).postMessage({ channel: 'version' });
const postVersionMessage = () => {
assertIsDefined(navigator.serviceWorker.controller).postMessage({ channel: 'version' });
};
// At this point, either the service worker is ready and
// became our controller, or we need to wait for it.
// Note that navigator.serviceWorker.controller could be a
// controller from a previously loaded service worker.
const currentController = navigator.serviceWorker.controller;
if (currentController && currentController.scriptURL.endsWith(swPath)) {
// service worker already loaded & ready to receive messages
postVersionMessage();
} else {
// either there's no controlling service worker, or it's an old one:
// wait for it to change before posting the message
const onControllerChange = () => {
navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange);
postVersionMessage();
};
navigator.serviceWorker.addEventListener('controllerchange', onControllerChange);
}
}, },
error => { error => {
reject(new Error(`Could not register service workers: ${error}.`)); reject(new Error(`Could not register service workers: ${error}.`));