mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

feat: isolate parent and child frames when handling requests #2324

Closed kettanaito closed 1 month ago

kettanaito commented 1 month ago

The problem

If the page calls worker.start() and has a child frame that also calls worker.start(), the requests from the frame will only be resolved against the top-level parent's worker and its handlers.

This has been reported by the Vitest team.

The solution

During the resolveMainFrame client lookup, if the event clientId is in the active client ids of the worker, return it. Don't lookup the clients because the top-level client will always match first, ignoring any underlying otherwise matching nested client ids.

kettanaito commented 1 month ago

Also a good point that we still won't properly support this scenario:

- a. parent with MSW
  - a.1. child with MSW
    - a.1.1. child without MSW (request here!)

Since a.1.1 doesn't call worker.start(), it won't be in the activeClientIds in the worker script at all. That will trigger the regular client lookup in all visible frames, and that will still resolve with the top-level client (a).

I'd say the expectation here is that the worker in a.1 should handle the requests coming from a.1.1.

Unfortunately, there's no hierarchical relationship of the service worker clients. You also cannot know the actual client ID on the client to do any sort of logic on the library's side. See https://github.com/w3c/ServiceWorker/issues/1556.

kettanaito commented 1 month ago

Released: v2.5.0 🎉

This has been released in v2.5.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.