rsnext/examples/with-msw
2022-07-26 20:08:40 -05:00
..
mocks fix: update with-msw example to function properly for all use cases (#38050) 2022-07-06 14:07:08 -05:00
pages Update with-msw example (#20335) 2020-12-28 14:22:10 -05:00
public Example: Treat "mockServiceWorker" as a generated artifact in "with-msw" (#25515) 2021-05-27 23:18:15 +00:00
.env.development Update with-msw example (#20335) 2020-12-28 14:22:10 -05:00
.env.production Update with-msw example (#20335) 2020-12-28 14:22:10 -05:00
.gitignore Update default gitignore templates (#39051) 2022-07-26 20:08:40 -05:00
package.json Clean up examples package.json (#27121) 2021-07-12 19:58:03 +00:00
README.md Update pnpm create next-app for latest pnpm 6 and 7 (#37254) 2022-05-27 21:21:40 +00:00

Mock Service Worker Example

Mock Service Worker is an API mocking library for browser and Node. It provides seamless mocking by interception of actual requests on the network level using Service Worker API. This makes your application unaware of any mocking being at place.

In this example we integrate Mock Service Worker with Next by following the next steps:

  1. Define a set of request handlers shared between client and server.
  2. Setup a Service Worker instance that would intercept all runtime client-side requests via setupWorker function.
  3. Setup a "server" instance to intercept any server/build time requests (e.g. the one happening in getServerSideProps) via setupServer function.

Mocking is enabled using the NEXT_PUBLIC_API_MOCKING environment variable. By default, mocking is enabled for both development and production. This allows you to have working preview deployments before implementing an actual API. To disable MSW for a specific environment, change the environment variable value in the file corresponding to the environment from enabled to disabled.

Deploy your own

Deploy the example using Vercel:

Deploy with Vercel

How to use

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example with-msw with-msw-app
# or
yarn create next-app --example with-msw with-msw-app
# or
pnpm create next-app --example with-msw with-msw-app

Deploy it to the cloud with Vercel (Documentation).