gpt4 book ai didi

ServiceWorker updatefound event vs waiting worker(ServiceWorker updatefound事件与等待的worker)

转载 作者:bug小助手 更新时间:2023-10-22 15:10:32 33 4
gpt4 key购买 nike



I'm wondering why ServiceWorkerRegistration has an updatefound event but no event that fires as soon as Service Worker in the installed (not "installing") state.

我想知道为什么ServiceWorkerRegistration有一个updatefind事件,但没有在Service Worker处于已安装(而不是“正在安装”)状态时立即触发的事件。


IMHO updatefound is not very useful on itself since it triggers when there is a new Service Worker file found. In that phase, the service worker may still be downloading cache files and might take a few moments until it is ready to be activated.

IMHO updatefound本身并不是很有用,因为它在发现新的Service Worker文件时触发。在该阶段,服务工作者可能仍在下载缓存文件,并且可能需要一些时间,直到它准备好被激活。


Depending on the application, it might be too early to display a "refresh to update" message in response to "updatefound". Instead, the client IMHO should wait the new Service Worker to reach the installing state:

根据应用程序的不同,现在显示“刷新以更新”消息以响应“updatefound”可能还为时过早。相反,客户端IMHO应该等待新的Service Worker达到安装状态:



function handleUpdateReady() {

// Service Worker ready to be activated (skipWaiting() can be called)
// Show message to the user.

}


navigator.serviceWorker.register('/sw.js').then(registration => {

if (registration.waiting) {

// a new Service Worker is already in the "installed" Phase and waits to be activated.
handleUpdateReady();

} else {

registration.addEventListener('updatefound', () => {

const newSW = registration.installing;

newSW.addEventListener('statechange', ev => {

if (newSW.state === 'installed')
handleUpdateReady();

});

});

}

});


On a slow network this might make a huge difference, because reloading the page while the new Service Worker is still "installing" is useless. On fast networks, one might not notice the difference.

在慢速网络上,这可能会产生巨大的影响,因为在新的Service Worker仍在“安装”时重新加载页面是无用的。在快速网络上,人们可能不会注意到差异。


Either I'm missing something or the above sample shows the correct way to deal with Service Worker / App updates if the app should update as soon as the new version is fully ready.

要么我遗漏了什么,要么上面的示例显示了处理Service Worker/应用程序更新的正确方法,如果应用程序应该在新版本完全准备好后立即更新。


It makes me wonder why the relatively complicated code above is necessary when the spec could just have provided a registration.addEventListener('updateready', ...) or something like that for a relatively common use case. Since there is no such event, I'm wondering if I'm missing a simpler solution.

这让我想知道,当规范本可以为相对常见的用例提供register.addEventListener('updateready',…)或类似的东西时,为什么上面相对复杂的代码是必要的。由于没有这样的事件,我想知道我是否错过了一个更简单的解决方案。


更多回答
优秀答案推荐
更多回答

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com