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

fix: cannot read properties of undefined (reading 'url') #2240

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

baegofda
Copy link

fix error cannot read properties of undefined (reading 'url') (ref. #2193)
image
This bug is said to be fixed in the latest version, but it's still an issue for some users, including myself (I'm currently using version 2.3.5).

In the createResponseListener function, sometimes const request = context.requests.get(requestId) is possible undefined. so reference error in request.url in the code below.

if (!response.url) {
  Object.defineProperty(response, 'url', {
    value: request.url,
    enumerable: true,
    writable: false,
  })
}

The same issue does not occur when using the changed version with this code.

@kettanaito
Copy link
Member

Hi, @baegofda. Thanks for opening this!

This is the issue:

sometimes const request = context.requests.get(requestId) is possible undefined

The solution you propose is a workaround. The error doesn't occur because the logic never runs. But it has to.

If you have a moment, would you look if you can spot any consistent reproduction of when context.requests.get(requestId) is undefined? I've addressed it in the past, but obviously there's more I haven't accounted for. Would be nice to track it down and fix.

@baegofda
Copy link
Author

baegofda commented Aug 28, 2024

Hello, @kettanaito. Sure!

This is my Environment

Next.js 14.0.4
react: 18.2.0
msw: 2.3.5
@tanstack/react-query: 5.14.2

This is happening in my local environment where I'm developing Next.js and I'm requesting msw handler via react-query as a CSR.

In my case, the error occurs when I run more than one browser tab and do a page reload.
image

@baegofda
Copy link
Author

It looks like the createResponseListener return is executed before the context.requests.set in the createRequestListener.

image
image

@kettanaito
Copy link
Member

@baegofda, that looks interesting. Thanks for diving in! Do you have a reproduction repo where I an look at this. If I have a repro, we can track it down in no time, I believe.

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