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

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

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Oct 22, 2024

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.

@@ -155,6 +160,10 @@ async function handleRequest(event, requestId) {
async function resolveMainClient(event) {
const client = await self.clients.get(event.clientId)

if (activeClientIds.has(event.clientId)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix.

@kettanaito kettanaito changed the title fix: support parent and child frames with msw fix: isolate parent and child frames when handling requests Oct 22, 2024
@@ -41,6 +43,11 @@ export function printStartMessage(args: PrintStartMessageArgs = {}) {
console.log('Worker scope:', args.workerScope)
}

if (args.client) {
// eslint-disable-next-line no-console
console.log('Client ID: %s (%s)', args.client.id, args.client.frameType)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also improving the activation message to include the client ID that prompted it, as well as the client frame type (top-level/nested). This helps during debugging.

@kettanaito kettanaito changed the title fix: isolate parent and child frames when handling requests feat: isolate parent and child frames when handling requests Oct 22, 2024
@kettanaito kettanaito marked this pull request as ready for review October 22, 2024 13:57
@kettanaito
Copy link
Member Author

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 w3c/ServiceWorker#1556.

Copy link

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick fix!

@kettanaito kettanaito merged commit a1a81ba into main Oct 22, 2024
12 checks passed
@kettanaito kettanaito deleted the fix/nested-frames-with-msw branch October 22, 2024 14:04
@kettanaito
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants