如何注销和删除旧服务人员? [英] How to unregister and remove old service worker?

查看:106
本文介绍了如何注销和删除旧服务人员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用.register注册了服务程序,并且可以在开发工具的应用程序"选项卡中看到.现在我的问题是如何注销该服务人员.

我已经运行了该脚本

if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
                console.group('====SW registration=======');
                navigator.serviceWorker.register('/sw.js', {scope: './'}).then(function(registration) {
                    console.log('SW registratered successfully');
                    console.log(registration);
                    registration.unregister().then(function(result) {
                        console.log('Unregistered done', result);
                    });
                }).catch(function(error){
                    console.error('sw registration failed', error);
                });
                console.groupEnd();
        });
}

但是我看到这样做,我们实际上是在先注册服务工作者,然后再取消注册.这对我来说似乎不是正确的方法.

或者,我可以从开发工具 Application> Service Workers 中单击服务工作者附近的取消注册链接,也可以单击 Application> Clear Storage em>,然后在新标签页中重新打开URL.

但是当我检查 chrome://serviceworker-internals/

它在列表底部显示旧的和未注册的服务人员的列表(参见图片)

那么为什么我在这里看到冗余服务人员列表?什么时候更新?这是chrome浏览器的默认行为吗?

解决方案

您应指定 为什么 要注销服务工作者.

我想有两种可能性:

  • 您要摆脱/service-worker.js文件并完全删除软件
  • 您想要一个新的/更新的软件并替换以前的软件

第一种情况: 签出此答案:如何卸载Service Worker?

第二种情况: 您不必注销旧的SW,只需更新/service-worker.js中的代码并将其注册在旧SW的顶部即可.这是通常的情况.新的更新SW将接管,过时的将消失. 我建议您非常仔细地阅读这些教程: https://developers.google.com/web/fundamentals /instant-and-offline/service-worker/lifecycle https://developers.google.com/web/fundamentals/getting -started/primers/service-workers

我要特别强调 ,因为很容易弄错与软件相关的东西,并使您的网站完全瘫痪:)

I have register a service woker using .register and can see in Application tab in dev tool. now my questionn is how to unregister this service worker.

I have run this script

if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
                console.group('====SW registration=======');
                navigator.serviceWorker.register('/sw.js', {scope: './'}).then(function(registration) {
                    console.log('SW registratered successfully');
                    console.log(registration);
                    registration.unregister().then(function(result) {
                        console.log('Unregistered done', result);
                    });
                }).catch(function(error){
                    console.error('sw registration failed', error);
                });
                console.groupEnd();
        });
}

But I see that by doing this, we are actually first registering the service worker and then unregister it. This seems not the correct way to me.

Alternatively, I can click on unregister link near to the service worker from dev tools Application > Service Workers and also click on Application >Clear Storage and re-open the URL in the new tab.

but when I check through chrome://serviceworker-internals/

it displays the list of old and unregistered service worker at bottom of list ( see image)

So why I am seeing the redundant service worker list here? when will it update? does this is the default behaviour of chrome browser.

解决方案

You should specify why you want to unregister the Service Worker.

I suppose there are two possibilities:

  • You want to get rid off the /service-worker.js file and remove SW completely
  • You want to have a new/updated SW and replace the previous one

First case: Checkout this answer: How do I uninstall a Service Worker?

Second case: You don't have to unregister the old SW, you just update the code in /service-worker.js and register it on top of the old one. This is the usual scenario. The new, updatet SW will takeover and the obsolete will go away. I suggest you read these tutorials very carefully: https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle https://developers.google.com/web/fundamentals/getting-started/primers/service-workers

I'm stressing carefully since it's very easy to get SW related stuff wrong and completely bork your website :)

这篇关于如何注销和删除旧服务人员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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