ServiceWorkerContainer.ready for a specific scriptURL?

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

ServiceWorkerContainer.ready resolves to an active ServiceWorkerRegistration, but if there are multiple service workers in play (e.g. service workers from previous page loads, multiple registrations within the same page) there are multiple service workers it could resolve to. How can I make sure that a particular service worker is handling network events when a chunk of code is executed?

That is, how can I write a function registerReady(scriptURL, options) that can be used as follows:

registerReady("foo.js").then(function (r) {     // "foo.js" is active and r.active === navigator.serviceWorker.controller     fetch("/"); // uses foo.js's "fetch" handler });  registerReady("bar.js").then(function (r) {     // "bar.js" is active and r.active === navigator.serviceWorker.controller     fetch("/"); // use bar.js's "fetch" handler }); 

回答1:

First, notice there can be only one active service worker per scope and your client page can be controlled by at most one and only one service worker. So, when ready resolves, you know for sure the service worker controlling your page is active.

To know if an arbitrary sw is active you can register it and check the queue of service workers in the registration and listen for 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 it makes sense to you.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!