Skip to content

Instantly share code, notes, and snippets.

@freddi301
Last active January 24, 2024 10:49
Show Gist options
  • Save freddi301/7463693ae2705337d3439b960f07e5df to your computer and use it in GitHub Desktop.
Save freddi301/7463693ae2705337d3439b960f07e5df to your computer and use it in GitHub Desktop.
MSW configuration for create-react-app
import { setupWorker } from 'msw/browser'
// https://mswjs.io/docs/best-practices/typescript
// also ensure there is .env or env.local file
// with PUBLIC_URL=index set to something otherwise chrome wont load the mock service worker
const worker = setupWorker(/* here goes handlers */);
if (process.env.NODE_ENV === "development") {
if (window.location.pathname === process.env.PUBLIC_URL) {
window.location.pathname = process.env.PUBLIC_URL + "/";
}
mockStarted = worker.start({
serviceWorker: {
url: `${process.env.PUBLIC_URL}/mockServiceWorker.js`,
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment