First, note that there can only be one active service worker per area, and your client page can be controlled by no more than one and only one service employee. So, when everything is ready, you probably know that the active worker controls your page .
To find out if an arbitrary sw is active, you can register it and check the queue of service workers during registration and listen to the changes in their state. For instance:
function registerReady(swScript, options) { return navigator.serviceWorker.register(swScript, options) .then(reg => { // If there is an active worker and nothing incoming, we are done. var incomingSw = reg.installing || reg.waiting; if (reg.active && !incomingSw) { return Promise.resolve(); } // If not, wait for the newest service worker to become activated. return new Promise(fulfill => { incomingSw.onstatechange = evt => { if (evt.target.state === 'activated') { incomingSw.onstatechange = null; return fulfill(); } }; }); }) }
Hope this makes sense to you.
Salva source share