rsnext/examples/with-msw
2021-08-17 09:18:08 +02:00
..
mocks Update to latest babel versions (#28174) 2021-08-17 09:18:08 +02: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 Example: Treat "mockServiceWorker" as a generated artifact in "with-msw" (#25515) 2021-05-27 23:18:15 +00:00
package.json Clean up examples package.json (#27121) 2021-07-12 19:58:03 +00:00
README.md Include utm_source on example links to vercel.com (#21305) 2021-01-19 07:28:54 +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 or Yarn 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

Deploy it to the cloud with Vercel (Documentation).