-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Removed unregistering of service workers on reload #12834
Removed unregistering of service workers on reload #12834
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per @KyleAMathews's comment:
Hmm yeah — we definitely shouldn't unregister all service workers, just ones from gatsby-plugin-offline. Or perhaps make it configurable.
Let's see if we can make this only remove gatsby-plugin-offline instances?
This currently is a little challenging, since it does't seem like we have a great way to identify the Gatsby-registered service worker.
So - I see two options here:
- Provide a way to uniquely identify a Gatsby instance of a service worker (it looks like
prefix
is a supported option for Workbox--perhaps that's usable?) also cc @davidbailey00, any idea here? - Presume that
sw.js
refers to the Gatsby service worker, therefore we can do something like:
if (supportsServiceWorkers(location, navigator)) {
navigator.serviceWorker.getRegistrations().then(registrations => {
for (let registration of registrations) {
if (registration.active.scriptURL.endsWith('sw.js')) {
registration.unregister()
}
}
})
}
I think in this case it does make sense to remove all service workers. Firstly, SWs are port-specific, so for example if you're testing a production build on port 9000, the worker won't be removed when testing the development version on port 8000. Therefore it shouldn't interfere with SWs from other HTTP servers as long as they're on different ports. Secondly, I think if anything, it's more important to remove non-Gatsby service workers, rather than vice versa. Otherwise there would be random things going on outside of our control if another SW is installed from a different non-Gatsby site on the same port. |
Isn't the main purpose of this PR to not remove non-Gatsby service workers? I tried developing firebase cloud messaging on a gatsby site and every time I reload the page in development mode, it de-registers the service worker. |
Ah, I understand the reasoning now. In this case I think it does make sense to remove this code and possibly display a warning instead - we can't really tell which SWs a user wants to keep or not so it's best not to guess, but at least if we display a warning in the console then it'll save people time when they're confused about why a site isn't updating on refresh. @DSchau thoughts on this? |
This seems like a reasonable use case--to me. I don't think we should remove "user code" that we're not in control of.
Warning/prompt is OK but some outstanding questions are helpful:
That being said--the solution should not be to entirely prevent removal of any service workers here (as in this PR)--we should target the Gatsby service worker for removal and leave others alone. |
Correct. Because most of the services that ask users to create service workers with a specific name also allows them to configure the use of an existing service worker with different name. Eg. Firebase cloud messaging needs a SW with name firebase-messaging-sw.js but it can also be configured with name sw.js. So we'll definitely need other way for unique identification. |
I've created a PR at #13716 in favour of this one, which warns about SWs detected but doesn't remove them. In development, we shouldn't have any Gatsby SWs since gatsby-plugin-offline only creates a SW in production mode, so I think detecting the "creator" of a SW might be unnecessary. As a result I'll be closing this PR, but thanks anyway your contribution! Feel free to open an issue or another PR in the future if you feel anything else is wrong and we'll be sure to have a look :) |
Description
Fixed issue #9770