Comments (9)
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.
from msw.
Tested this out and it works perfectly. Thanks!
from msw.
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:
- When should the service worker be unregistered?
I'll try to perform that on the same beforeunload
window event, for that is a reliable way of knowing that window is being closed. My only worry is that there is no guarantee that the service worker unregister promise will resolve prior to the window actually unloading. That may lead into some unexpected scenarios, I suppose. I'd like to test how it behaves.
Need to keep in mind that there may be multiple clients listening to the same mocking Service Worker. The latter must not unregister when one window closes, but, ideally, when all its clients are closed.
from msw.
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.
from msw.
I gave it a quick manual test and unregistering the service worker on beforeunload
seems to wield an expected result.
Timer sounds good, yet I'm afraid it creates a side-effect that is even harder to reason about than sw.unregister()
. I can imagine it being subjected to a race condition (what finishes first: page unload or timer's callback?), and can't say from the top of my head if you reload the page with an action in a timer, would that action still execute after the page loads?
from msw.
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?
from msw.
Released in 0.13.0
.
This release comes with a change to mockServiceWorker.js
, so please make sure to:
$ 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 beforeunload
window event.
Eventually, I added a self-terminating logic to the worker, despite my previous comment. Service Worker is a reliable context to unregister itself, as it's persistent between clients' sessions, and keeps running even if you close the client. It's enough for the client to signal it's being closed, and there is no race condition for the worker to unregister itself, as the handling of unregistration is not bound to the client.
from msw.
This isn't working for me. I'm still seeing the mockserviceworker in Dev Tools, even after restarting my computer.
"msw": "^0.21.2"
Chrome Version 88.0.4324.96 (Official Build) (x86_64)
Service worker is running on two clients locally, one in HTML file and one in React app
from msw.
@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 msw
(0.26.0 at the moment) and re-instantiate its worker script (follow the instructions in the browser's console). You are a couple of versions behind and a lot of things have changed since then.
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.
from msw.
Related Issues (20)
- Add delay before each request HOT 3
- Mock a request that contains both query and path parameters
- TypeError: confirm is not a function HOT 7
- Failing to intercept an Axios request: Node 20.11 + Vitest 1.3.1 + MSW 2.2.1
- v2.2.2 does not intercept request in browser mode (CORS error) but v1.3.1 does HOT 3
- Infering the `boundary` callback arguments HOT 7
- support custom fetch option HOT 7
- support selecting interceptors HOT 1
- Set-Cookie responses containing commas are not handled correctly HOT 1
- HttpResponse.json() throwing TypeError: Right-hand side of 'instanceof' is not an object. HOT 1
- Request with FormData body makes Jest hang forever HOT 6
- TypeError: Right-hand side of 'instanceof' is not an object HOT 4
- Cannot read properties of undefined (reading 'url') HOT 5
- Mocked data getting empty string HOT 8
- "InvalidStateError: The object is in invalid state" when mocking rest api
- Unable to use msw/node for testing solid-js due to `resolve.conditions` set to `browser` HOT 5
- drop CommonJS support HOT 2
- quiet: true should supress RESPONSE LISTENER logs HOT 4
- Narrowing the response body type in `HttpResponse.json` HOT 16
- Error: No known conditions for "./browser" specifier in "msw" package HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msw.