Unregistering/Removing a Service Worker

前端 未结 2 1521
轻奢々
轻奢々 2020-12-19 20:07

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 w

2条回答
  •  悲哀的现实
    2020-12-19 20:54

    Below sample code will check for service worker registered in your browser and fetch it.

    registration.active.scriptURL will provide you exact url of all service workers.

    registration.unregister(); will remove that service worker.

    LINK: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/unregister

    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.getRegistrations()
            .then(function(registrations) {
                for(let registration of registrations) {
                   if(registration.active.scriptURL == 'http://localhost/my-push/myworker.js'){ registration.unregister(); }
                }
            });
    }
    

    If you want to update service worker code than use https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/update

提交回复
热议问题