question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

msw in browser doesn't work with Vite

See original GitHub issue

Describe the bug

see title - I can’t get msw browser to work at all with Vite as a bundler. Was working before with same setup except with Parcel as bundler.

Environment

  • msw: 0.27.1
  • nodejs: v12.20.1
  • npm: 6.14.10

Please also provide your browser version

chrome macos 89.0.4389.114

To Reproduce

Steps to reproduce the behavior:

  1. Use Vite as bundler
  2. build using vite --debug
  3. see in build debug output vite:spa-fallback Not rewriting GET /mockServiceWorker.js because the path includes a dot (.) character. +4s
  4. see error in browser console: A bad HTTP response code (404) was received when fetching the script.
  5. don’t see [MSW started] in browser console (whatever the usual message is to indicate msw has started successfully)

Guess

this issue (has similar error as in vite build log) https://github.com/vitejs/vite/issues/2245 might have something to do with it? I tried the ‘vite-plugin-rewrite-all’ package mentioned in that issue, and it didn’t work for me.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
midanosicommented, May 13, 2021

I followed the strategy by msutkowski in my repo, and it’s working just fine now, thanks for providing the example that helped.

Also just got an email from someone else leaving a comment in this thread i thought? But can’t see it here… anyway they said that likely the only reason it wasn’t working for me was that I hadn’t put my mockServiceWorker file in /public, which was the case. Sorry for being dumb, and thanks for the help!

2reactions
msutkowskicommented, Apr 27, 2021

Follow up: here’s the way I’d actually recommend doing it: https://github.com/msutkowski/msw-ts-vitejs/pull/1/files.

This leverages the special web worker feature of vite, and skips the production & public folder issue altogether.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser - Getting Started - Mock Service Worker Docs
Browser. Setup. Mock Service Worker operates client-side by registering a Service Worker responsible for requests interception.
Read more >
Using Mocked Service Worker (msw) with @web/test-runner
I am trying to setup msw with @web/test-runner (and playwright). The problem is that I don't know how the mockServiceWorker.js can be picked ......
Read more >
@iodigital/vite-plugin-msw - npm
Build. A true value will output MSW's mockServiceWorker. js file to the Vite build directory, in case if MSW is needed in production....
Read more >
Chapter 3.2.2.2: Using Mock Service Worker with Vitest
The important thing for us is that we can use the MSW package to mock network traffic in both browser and Node. js...
Read more >
Using Mock Service Worker with Vitest and fetch
If Mock Service Worker does not seem to work in combination with Vitest, the solution is to add a fetch polyfill.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found