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

A registered service worker breaks development #2272

Closed
Timer opened this issue May 19, 2017 · 4 comments
Closed

A registered service worker breaks development #2272

Timer opened this issue May 19, 2017 · 4 comments
Milestone

Comments

@Timer
Copy link
Contributor

Timer commented May 19, 2017

If you happen to run serve -s build and it installs a localhost service worker, all future development is borked (no page reloading).

We need to fix this problem... do we need to add something like this?

if (process.env.NODE_ENV === 'development') {
  navigator.serviceWorker.getRegistrations().then(function(registrations) {
    for (let registration of registrations) {
      registration.unregister()
    }
  })
}

I believe the docs mention this, but it's super annoying.

/cc @jeffposnick

@gaearon
Copy link
Contributor

gaearon commented May 19, 2017

I can’t seem to reproduce this so far.

@gaearon
Copy link
Contributor

gaearon commented May 19, 2017

Ah, @Timer says he uses 5000 as DEV port (which is default PROD port).
I guess it’s not very common that people would use the same port but we should probably avoid that.

@gaearon gaearon added this to the 1.0.2 milestone May 19, 2017
@Timer
Copy link
Contributor Author

Timer commented May 19, 2017

Having export PORT=9000 in .bash_profile probably has the same effect. 😄

Will send a PR.

@jeffposnick
Copy link
Contributor

Unwanted service workers taking control due to sharing ports between environments can definitely be an issue. It's really a good idea not to mix ports across environments if at all possible, but obviously developers might do that.

The problem with writing conditional code that refers to process.env.NODE_ENV and unregistering based on that is that the process.env.NODE_ENV is converted into a real string and inlined into the HTML, which then will get read directly from the cache by the service worker. So in the scenario you describe, the same process.env.NODE_ENV value (i.e. 'production') will be reused.

The best way of dealing with this would be to create a no-op service worker that will be checked in to the development source, and that would be overwritten by the production service worker when a build is done.

This no-op service worker will end up being fetched from the development web server in the scenario you describe, and then the next visit to the development page will behave as if there were no service worker present.

I'm happy to file a PR for this after confirming that there are no unexpected side effects locally.

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

3 participants