取消注册/删除服务工作者 [英] Unregistering/Removing a Service Worker

查看:142
本文介绍了取消注册/删除服务工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不再更新的糟糕服务工作者。我首先在Chrome中发现了这个问题。然后,我将以下代码放在index.html文件和sw.js(服务工作者)文件中。在大多数情况下,它似乎工作正常。 Firefox似乎是唯一没有删除服务工作者的浏览器。我使用下面的文章来创建取消注册脚本。

I have a bad service worker that is no longer updating. I noticed the issue in Chrome first. I then put the following code in the index.html file and in the sw.js (service worker) file. For the most part it seems to be working fine. Firefox seems to be the only browser that is not removing the service worker. I used the article below to create the unregister script.

如何卸载服务工作者?

我还使用了这篇文章和代码并得到了相同的结果。

I have also used this article and code and got the same results.

如何删除有缺陷的服务工作者,或实施kill switch?

我也收到了 getRegistrations()的错误消息,说它未定义。不确定如何解决这个问题。

I am also receiving an error message for getRegistrations() saying it is undefined. Not sure how to fix that either.

非常感谢您对这两个问题的帮助。

Help with both of these issues would be greatly appreciated.

<script>
navigator.serviceWorker.getRegistrations().then(function(registrations) {
 for(let registration of registrations) {
  registration.unregister();
} });</script>


推荐答案

我偶然发现了这个似乎比最好的解决方案。

I stumbled across this answer which seemed a better-than-most solution.

博文: https ://medium.com/@nekrtemplar/self-destroying-serviceworker-73d62921d717

Github: https://github.com/NekR/self-destroying-sw

它用这段代码破坏了自己:

It destroys itself with this code:

self.addEventListener('install', function(e) {
  self.skipWaiting();
});

self.addEventListener('activate', function(e) {
  self.registration.unregister()
    .then(function() {
      return self.clients.matchAll();
    })
    .then(function(clients) {
      clients.forEach(client => client.navigate(client.url))
    });
});

这是对上述代码的更深入解释和进一步改进。
https://love2dev.com/blog/how -to-uninstall-a-service-worker /

Here's an even more in-depth explanation and further improvement on the above code. https://love2dev.com/blog/how-to-uninstall-a-service-worker/

这篇关于取消注册/删除服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆