取消注册/移除 Service Worker [英] Unregistering/Removing a Service Worker

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

问题描述

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

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.

如何卸载 Service Worker?

我也用过这篇文章和代码,得到了同样的结果.

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

我该怎么办删除有缺陷的服务工作者,或实施终止开关"?

我还收到一条 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-销毁-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/

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

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