msw in browser doesn't work with Vite
See original GitHub issueDescribe 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:
- Use Vite as bundler
- build using
vite --debug
- see in build debug output
vite:spa-fallback Not rewriting GET /mockServiceWorker.js because the path includes a dot (.) character. +4s
- see error in browser console:
A bad HTTP response code (404) was received when fetching the script.
- 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:
- Created 2 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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.