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

MSW + React + Vite: Failed to HMR Update if MSW active #2401

Open
4 tasks done
rizaldirnm opened this issue Dec 24, 2024 · 3 comments
Open
4 tasks done

MSW + React + Vite: Failed to HMR Update if MSW active #2401

rizaldirnm opened this issue Dec 24, 2024 · 3 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

@rizaldirnm
Copy link

rizaldirnm commented Dec 24, 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

https://github.com/rizaldirnm/msw-vite

Reproduction steps

clone repo: https://github.com/rizaldirnm/msw-vite

yarn install

yarn dev

Current behavior

  1. Some how MSW mocking request in asset react.svg
    CleanShot 2024-12-24 at 12 18 19@2x

  2. If we try change something in app, HMR failed to deliver asset because MSW mocked the request
    CleanShot 2024-12-24 at 12 29 55@2x

Expected behavior

MSW should mock only registered handler.

@rizaldirnm rizaldirnm 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 Dec 24, 2024
@shanereid
Copy link

+1

I'm getting the exact same error on a pretty fresh install.

  • I'm on macOS
  • Happens on Chrome, but not Safari
  • { onUnhandledRequest: "bypass" } does not fix the issue.
  • Basically exact error as above, says it hot updates but does not. msw seems to be intercepting hmr requests? (screenshot attached)
  • react 18.3.1
  • vite 5.4.8
  • msw 2.7.0
Screenshot 2025-01-02 at 22 21 52

@charlieforward9
Copy link

May be related, I'm getting:

[MSW] Warning: intercepted a request without a matching request handler:

  • GET /node_modules/.pnpm/[email protected]/node_modules/graphql/language/parser.mjs?v=cffff488

If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks

Looks like most of them are coming from the graphql lib...

@yeo311
Copy link

yeo311 commented Jan 10, 2025

I encountered the same issue and identified the cause.

Within msw, the @bundled-es-modules/cookie package is used to parse request cookies.

This package validates cookie names using a regular expression, and when the validation fails, it throws the error mentioned above.

    var cookieNameRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;

https://github.com/bundled-es-modules/cookie/blob/7591afcfa0587459a5e579d16e7f48b6c732199d/index-esm.js#L35C5-L35C61

In my case, some of the cookies used by the service included square brackets in its name. ex) tr[vid]

Looking at the regular expression, it does not account for square brackets. According to RFC 6265, square brackets are not allowed in cookie names.

Since I couldn't remove the problematic cookie, I patched the regular expression using pnpm patch.

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

4 participants