Skip to content

Commit

Permalink
Fix Storybook Service Worker in GitHub Pages (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell authored Jun 14, 2024
1 parent e4f021a commit 5832785
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
15 changes: 12 additions & 3 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import type { Preview } from "@storybook/react";
import { initialize, mswLoader, getWorker } from "msw-storybook-addon";
import "./stories/input.css";

// Initialize MSW
initialize();

const preview: Preview = {
// calling getWorker().start() is a workaround for an issue
// where Storybook doesn't wait for MSW before running:
Expand All @@ -20,4 +17,16 @@ const preview: Preview = {
},
};

let options = {};
if (location.hostname === "apollographql.github.io") {
options = {
serviceWorker: {
url: "/graphql-testing-library/mockServiceWorker.js",
},
};
}

// Initialize MSW
initialize(options);

export default preview;
4 changes: 2 additions & 2 deletions .storybook/stories/Welcome.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ const schemaWithMocks = addMocksToSchema({
},
});

// `createHandler` returns an object with `handler` and `replaceSchema`
// functions: `handler` is a MSW handler that will intercept all GraphQL
// `createHandler` returns an object with a `handler` and `replaceSchema`
// function: `handler` is a MSW handler that will intercept all GraphQL
// operations, and `replaceSchema` allows you to replace the mock schema
// the `handler` use to resolve requests against.
const { handler, replaceSchema } = createHandler(schemaWithMocks, {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const schemaWithMocks = addMocksToSchema({
},
});

// `createHandler` returns an object with `handler` and `replaceSchema`
// functions: `handler` is a MSW handler that will intercept all GraphQL
// `createHandler` returns an object with a `handler` and `replaceSchema`
// function: `handler` is a MSW handler that will intercept all GraphQL
// operations, and `replaceSchema` allows you to replace the mock schema
// the `handler` use to resolve requests against.
const { handler, replaceSchema } = createHandler(schemaWithMocks, {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
"clean-storybook": "rm -rf storybook-static",
"prepdist:changesets": "node scripts/prepareDist.cjs",
"changeset-publish": "pnpm run clean && pnpm run build && pnpm run prepdist:changesets && cd dist && changeset publish",
"changeset-version": "changeset version && pnpm i",
Expand All @@ -46,7 +47,7 @@
"test-storybook": "test-storybook",
"build-and-test-storybook": "pnpm run build-storybook && npx concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"npx http-server storybook-static --port 6006 --silent\" \"npx wait-on tcp:6006 && pnpm run test-storybook\"",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
"build-storybook": "pnpm run clean-storybook && storybook build"
},
"license": "MIT",
"devDependencies": {
Expand Down

0 comments on commit 5832785

Please sign in to comment.