Skip to content

Latest commit

 

History

History

with-svelte

Svelte + MSW

Mock Service Worker usage example with Svelte.

Edit in CodeSandbox

General

Both client-side and server-side integrations of MSW are implemented using Hooks.

Client-side

  1. Generate the worker script (mockServiceWorker.js) in the ./static directory using the following command:
npx msw init ./static
  1. Create src/mocks/browser.ts and configure the worker instance.
  2. In src/hooks.client.ts, import and start the worker from src/mocks/browser.ts.

Server-side

The server-side integration allows you to intercept outgoing requests from the load functions of your Svelte pages.

  1. Create src/mocks/node.ts and configure the server instance.
  2. In src/hooks.server.ts, import and start the server from src/mocks/node.ts.