Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister a service worker when another tab is controlled by it can "resurrect" it (Chrome) #1687

Open
signorpipo opened this issue Jul 19, 2023 · 2 comments

Comments

@signorpipo
Copy link

Repro steps:

  • open 2 tabs controlled by the same service worker
  • with the inspector opened, unregister the service worker (or do that in the console if u prefer)
  • the service worker is now flagged as deleted
  • if u just reload 1 of the tab, the reloaded tab will not be controlled, but the service worker is not flagged as deleted anymore
    • note that it's not like a new service worker is installed, the "id" displayed in the inspector is the same
    • this is why I say it's resurrected

This of course makes it hard to properly unregister a service worker unless u reload all the clients.
This is what I'm doing, but I wonder if this bug can still happen if a client reload with a bit of delay, leading to the old service worker resurrection.

@antonkorotkov
Copy link

Repro steps:

  • open 2 tabs controlled by the same service worker

  • with the inspector opened, unregister the service worker (or do that in the console if u prefer)

  • the service worker is now flagged as deleted

  • if u just reload 1 of the tab, the reloaded tab will not be controlled, but the service worker is not flagged as deleted anymore

    • note that it's not like a new service worker is installed, the "id" displayed in the inspector is the same
    • this is why I say it's resurrected

This of course makes it hard to properly unregister a service worker unless u reload all the clients. This is what I'm doing, but I wonder if this bug can still happen if a client reload with a bit of delay, leading to the old service worker resurrection.

Hi! If that helps you I've found a way to refresh the SW in all open tabs after it is deregistered.
You send a message command to the service-worker to self-destroy, and the service-worker in turn reloads all controlled clients.

// service-worker code
self.addEventListener('message', ({ data }) => {
	if (data === 'unregister') {
		self.registration.unregister()
			.then(() => self.clients.matchAll())
			.then(clients => clients.forEach(client => client.navigate(client.url)));
	}
});
// client code
navigator.serviceWorker.ready.then((registration) => {
     registration.active.postMessage('unregister');
});

@signorpipo
Copy link
Author

Hey, it’s been a while, but what I mention at the end is that maybe this can still resurrect the very same sw which is reloading all clients, even if probably unlikely

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

No branches or pull requests

2 participants