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 self when not activated? #96
Comments
This is a good point. I've been stumbling upon this myself, with the service worker at place where it shouldn't have been. At the moment it does deactivate the mocking upon window unload, but I see there is no logic to unregister the worker: I find it a good idea to unregister the worker, for it's bound to a host (I believe it's not even bound to the port), yet I have one concerns:
I'll try to perform that on the same
|
I think that's a good approach. Another thing that might work is when the service worker starts up, it starts a timer and if it's not activated after a certain amount of time, then it unregisters itself? I can see why this may need to be opt-in though. |
I gave it a quick manual test and unregistering the service worker on Timer sounds good, yet I'm afraid it creates a side-effect that is even harder to reason about than |
Maybe on start up it can post a message to the client and if it doesn't get a response then it can unregister itself? |
That's a possibility. Still I'd like to delegate registration responsibility to the client-side library, and not the service worker. This way there is a single point of control, treating the service worker as a ready-only artifact, if that makes sense. |
Released in $ npm install msw@latest
$ npx msw init <PUBLIC_DIR> Service Worker instance should now unregister itself when the last controlled client is closed. That is achieved by keeping a map of controlled clients internally in the worker's scope, and removing a client by ID once it fires a
|
Tested this out and it works perfectly. Thanks! |
This isn't working for me. I'm still seeing the mockserviceworker in Dev Tools, even after restarting my computer. "msw": "^0.21.2" Service worker is running on two clients locally, one in HTML file and one in React app |
@adp-source, could you please elaborate more on how you see the worker? What page do you check the worker on? What state the worker is in? A screenshot of it in the "Application" tab could be useful. Before you do, please update to the latest version of If the worker is active, that means there is a client that's still controlled by it. When the last controlled client is closed, the worker unregisters itself. We have this behavior tested, but I never deny a chance for error. |
I have MSW working for a project on localhost:3000, then I close that project and open a new one which does not have MSW and that starts up on localhost:3000 (the default for create-react-app based apps) and MSW hangs around, logs in the console, and generally confuses people.
Can we have MSW's service worker unregister itself when it's not activated when the app starts up? I'd be fine if this is opt-in.
The text was updated successfully, but these errors were encountered: