How do I uninstall a Service Worker?

2019-01-04 18:59发布

After deleting /serviceworker.js from my root directory, Chrome still runs the service worker that I removed from my webroot. How do I uninstall the service worker from my website and Chrome so I can log back into my website?

I've tracked the issue down to Service Work's cache mechanism and I just want to remove for now until I have time to debug it. The login script that I'm using redirects to Google's servers for them to login to their Google account. But all I get from the login.php page is an ERR_FAILED message.

5条回答
做个烂人
2楼-- · 2019-01-04 19:14

You can also go to the URL: chrome://serviceworker-internals/ and unregister a serviceworker from there.

查看更多
叼着烟拽天下
3楼-- · 2019-01-04 19:15

You should detecte two API in your devices: getRegistrations and getRegistration. The service-worker not has a unique set of APIs in all platforms. For example, some browsers only have a navigator.serviceWorker.getRegistration, no navigator.serviceWorker.getRegistrations. So you should consider with both.

查看更多
叼着烟拽天下
4楼-- · 2019-01-04 19:17

In Google Chrome, you can go to Developer tools (F12) -> Application -> Service worker and unregister the service workers from the list for that specific domain.

Screenshot

This method is effective in development mode of a site and mostly they run on localhost which is you may need for other project's development.

查看更多
爷、活的狠高调
5楼-- · 2019-01-04 19:21

You can remove service workers programmatically like this:

navigator.serviceWorker.getRegistrations().then(function(registrations) {
 for(let registration of registrations) {
  registration.unregister()
} })

Docs: getRegistrations, unregister

You can also remove service workers under the Application tab in Chrome Devtools.

查看更多
做个烂人
6楼-- · 2019-01-04 19:25

You can do this through Chrome Developer Tool as well as Programatically.

  1. Find all running instance or service worker by typing

    chrome://serviceworker-internals/

    in a new tab and then select the serviceworker you want to unregister.

  2. Open Developer Tools (F12) and Select Application. Then Either

    Select Clear Storage -> Unregister service worker

    or

    Select Service Workers -> Choose Update on Reload

  3. Programatically

if(window.navigator && navigator.serviceWorker) {
  navigator.serviceWorker.getRegistrations()
  .then(function(registrations) {
    for(let registration of registrations) {
      registration.unregister();
    }
  });
}

查看更多
登录 后发表回答