rsnext/examples/with-msw
Balázs Orbán 1d7ce56a70
chore(examples): lock msw version in with-msw example (#40777)
Closes #40775

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-09-22 00:06:29 +01:00
..
mocks examples/with-msw-typescript (#39196) 2022-07-31 15:54:30 +00:00
pages examples/with-msw-typescript (#39196) 2022-07-31 15:54:30 +00: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 examples/with-msw-typescript (#39196) 2022-07-31 15:54:30 +00:00
package.json chore(examples): lock msw version in with-msw example (#40777) 2022-09-22 00:06:29 +01:00
README.md examples/with-msw-typescript (#39196) 2022-07-31 15:54:30 +00:00
tsconfig.json examples/with-msw-typescript (#39196) 2022-07-31 15:54:30 +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.

The service worker file will automatically be generated in public/mockServiceWorker.js after installing node_modules. If the file is not generated, you can explicitly generate it with the following command:

npx msw init public/

More information on this setup step can be found in the MSW documentation here.

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
yarn create next-app --example with-msw with-msw-app
pnpm create next-app --example with-msw with-msw-app

Deploy it to the cloud with Vercel (Documentation).