Skip to content

Always 404 error returns on next.js environment #119

@hiromaily

Description

@hiromaily

Though I set handler that handler doesn't work and 404 error returns.
I made sure logging [MSW] Mocking enabled. on console.

I set various handler, get method, post method, full path url, relative path url, handler on preview.ts, handler on each component, but it doesn't change anything.

when I set onUnhandledRequest event, this event always emits.

initialize({
  onUnhandledRequest: ({ method, url }) => {
    console.log(`MSW:onUnhandledRequest [method] ${method}, [url] ${url}`);
  },
});

Is that because next.js specific issue?

I called getWorker() on my storybook component, but its data currentHandlers: Array(0) expresses no handlers set.

  useEffect(() => {
    console.log(getWorker());
    
    fetch("/get_config")
      .then((response) => {
        return response.json();
      })
      .then((data) => {
        console.log(data);
      });
  }, []);

my preview.ts is

const preview: Preview = {
  parameters: {
    msw: {
      handlers: [
        rest.get("/get_config", (req, res, ctx) => {
          return res(
            ctx.status(200),
            ctx.json({ jsonrpc: "2.0", id: 1, result: JSON.parse("{}") }),
          );
        }),
        rest.get("http://localhost:6006/get_config", (req, res, ctx) => {
          return res(
            ctx.status(200),
            ctx.json({ jsonrpc: "2.0", id: 1, result: JSON.parse("{}") }),
          );
        }),
        rest.post("/get_config", (req, res, ctx) => {
          return res(
            ctx.status(200),
            ctx.json({ jsonrpc: "2.0", id: 1, result: JSON.parse("{}") }),
          );
        }),
        rest.post("http://localhost:6006/get_config", (req, res, ctx) => {
          return res(
            ctx.status(200),
            ctx.json({ jsonrpc: "2.0", id: 1, result: JSON.parse("{}") }),
          );
        }),
      ],
    },
    loaders: [mswLoader],
  },
};

dependency

    "@storybook/addon-essentials": "^7.4.0",
    "@storybook/addon-interactions": "^7.4.0",
    "@storybook/addon-links": "^7.4.0",
    "@storybook/blocks": "^7.4.0",
    "@storybook/nextjs": "^7.4.0",
    "@storybook/react": "^7.4.0",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions