According to the spec,ServiceWorker.waiting
属性返回对已安装并等待激活的服务工作者的引用。
很好。但是,如何确定等待服务的工作人员何时过渡到新状态?
由ServiceWorker.waiting
返回的服务工作者似乎没有任何必要的事件来通知状态更改:
if (registration.waiting) {
registration.waiting.postMessage('skip-waiting');
// This is failing because ready is undefined.
registration.waiting.ready.then(function () {
console.log('new service worker - ready');
}
}
答案 0 :(得分:0)
在撰写本文时,registration.waiting.onstatechange
在Chrome中为空。但是,可以进行以下操作:
if (registration.waiting) {
const waiting = registration.waiting;
waiting.postMessage('skip-waiting');
waiting.addEventListener('statechange', function (e) {
if(waiting.state === 'activated') {
console.log('new service worker - ready');
}
}
}