强制重新加载在JavaScript中跳过服务工作者
问题描述:
服务人员可能会干扰刷新按钮(按设计)。在桌面Chrome上,您可以按住shift并单击刷新按钮进行硬重新加载,忽略任何已安装的ServiceWorker。
Service Workers can interfere with the refresh button (by design). On desktop Chrome, you can hold shift and click the refresh button to do a hard reload, ignoring any installed ServiceWorker.
有没有办法强制重新加载忽略SW在JavaScript?
Is there a way to force a reload ignoring the SW in JavaScript?
(我想这样,所以如果SW开始行为不端,我可以给移动用户一个推按钮。电话没有移位键。)
(I want this so I can give mobile users a button to push if the SW starts misbehaving. Phones don't have a shift key.)
答
如果您在刷新之前取消注册
服务工作者,则下一页加载将没有服务工作者加载。
If you unregister
the Service Worker before refreshing, the next page load will load without the Service Worker.
navigator.serviceWorker.getRegistration().then(function(reg) {
if (reg) {
reg.unregister().then(function() { window.location.reload(true); });
} else {
window.location.reload(true);
}
});