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

Failed to fetch after first interception #1976

Closed
4 tasks done
weblancaster opened this issue Jan 18, 2024 · 7 comments
Closed
4 tasks done

Failed to fetch after first interception #1976

weblancaster opened this issue Jan 18, 2024 · 7 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser

Comments

@weblancaster
Copy link

weblancaster commented Jan 18, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

N/A

Reproduction steps

(this is a migration from older version to latest)

start Next server
go to a page that makes an HTTP call that is listed to be intercepted
page refreshes and MSW fails to fetch after the first request is successful

Current behavior

when next/react triggers a new load cycle the calls failed to fetch

Screenshot 2024-01-17 at 12 18 14 PM

Expected behavior

start Next server
go to a page that makes an HTTP call that is listed to be intercepted
page refreshes, msw intercepts consequent intercepted http calls

@weblancaster weblancaster added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Jan 18, 2024
@Fallsleep
Copy link

Fallsleep commented Jan 19, 2024

in my case, most time every request is ok when I login by form in the page, there's no request before I typing username/password and click to send the first request after msw mocked.

If I refresh the page after login, there's a request for getting user info when vue router navigation, I'll got the same error, the request is send soon after msw mocked. the following login out request is successed.

I try to downgrade to 2.0.14, erveything is ok.

btw, if set onUnhandledRequest to bypass, it only thrown axios error, change to warn, it may thrown msw error sometime.

@dylano
Copy link

dylano commented Jan 19, 2024

I'm seeing the same issue with with msw@2.1.2 on a React+Vite app. MSW does not appear to be initialized for the requests which are fired off as part of the initial page load.

This sort of looks like the symptoms reported in #1653 but the deferred render approach is not effective here as it was in 2.0.14.

Edit: The bug was mine -- I'm running fine with 2.1.2 now. The snippet in the deferred render comment linked above is missing an await on the worker.start() and that carried over to my code. Perhaps 2.0.14 is just slightly quicker to start and didn't affect my app in the same way despite not waiting on the start call.

@weblancaster
Copy link
Author

In my case, I do see [MSW] Mocking enabled. and the first server request is mocked but none of the consequent calls.

I have been trying to replicate this issue:

I was not able to replicate the failed to fetch error but my local code the issue still persists

@tunganh-phamba
Copy link

same problem for me as well in version 2.1.2

@weblancaster
Copy link
Author

In my case, I do see [MSW] Mocking enabled. and the first server request is mocked but none of the consequent calls.

I have been trying to replicate this issue:

I was not able to replicate the failed to fetch error but my local code the issue still persists

@kettanaito I was able to replicate in the codesandbox links above, wherein the first load MSW intercepts the first call but after I refresh the browser, there is no more interception.

@weblancaster
Copy link
Author

weblancaster commented Jan 22, 2024

For future reference found this post from @kettanaito on X

In summary, this is the state of Next.js + MSW right now:

  • Server-side mocking is possible but your app won't pick up updates to the handlers unless you restart it;
  • Client-side mocking is not possible.

https://twitter.com/kettanaito/status/1749496339556094316

@kettanaito
Copy link
Member

kettanaito commented Jan 25, 2024

I'm going to close this. Next.js doesn't work well with MSW at the moment. Until I have a fully functional example as confirmation of that, Next.js is not supported.

You can follow the progress of that example here:
mswjs/examples#101

The Next.js team has also reached out to me to help address the remaining issues on Next.js side, so I hope to post some updates soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser
Projects
None yet
Development

No branches or pull requests

5 participants