Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

By the way how we can unregister the serviceworker #16

Open
webdawe opened this issue Sep 4, 2017 · 4 comments
Open

By the way how we can unregister the serviceworker #16

webdawe opened this issue Sep 4, 2017 · 4 comments

Comments

@webdawe
Copy link

webdawe commented Sep 4, 2017

Hi,
for instance while we disable extension through the config settings there should be a way we can un register the service worker.
What I tried is amend register.phtml with the following

image

But it will register and then unregister. is this the correct way of un-register existing serviceworker?

@tgerulaitis
Copy link
Member

I don't think that's correct. Like you mentioned, this will register the service worker and then unregister it, because you're calling the register() method to retrieve the registration.

I think a better solution would be to use the getRegistration() method to retrieve the registration, e.g.:

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.getRegistration().then(function (registration) {
        registration.unregister();
    });
}

Two notes:

  1. I've not tested this snippet of code, so it comes with no warranty.
  2. This should return the service worker registration that's relevant for the current scope, but I'm not sure if it will return the most relevant one or if it's even possible to have multiple service workers apply to the same scope.

@webdawe
Copy link
Author

webdawe commented Sep 5, 2017

Thank you so much @tgerulaitis
I have tested locally and it seems to be working and we have only one service worker running so no need to worry about it much at the moment.

Anil

@tgerulaitis
Copy link
Member

Glad to have helped :)

I'll keep this issue open as I think this would be a good enhancement to build into the extension whenever there's time.

@webdawe
Copy link
Author

webdawe commented Sep 6, 2017

thanks @tgerulaitis

I have added an if condition to avoid showing error registration undefined
(function () { if ('serviceWorker' in navigator) { navigator.serviceWorker.getRegistration().then(function (registration) { if (registration){ registration.unregister(); } }); } })();

Thanks
Anil

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants