Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gatsby Version >= 2.0.24 breaks Storybook support #10662

Closed
NathHorrigan opened this issue Dec 26, 2018 · 5 comments · Fixed by #10669
Closed

Gatsby Version >= 2.0.24 breaks Storybook support #10662

NathHorrigan opened this issue Dec 26, 2018 · 5 comments · Fixed by #10669
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby

Comments

@NathHorrigan
Copy link

NathHorrigan commented Dec 26, 2018

Description

When following the gatsby docs for storybook support (https://www.gatsbyjs.org/docs/visual-testing-with-storybook/) on any version >= 2.0.24 you get the following fatal error:

ERROR in ./node_modules/gatsby/cache-dir/commonjs/public-page-renderer-dev.js
Module not found: Error: Can't resolve './pages.json' in '.....PROJECT..../node_modules/gatsby/cache-dir/commonjs'
 @ ./node_modules/gatsby/cache-dir/commonjs/public-page-renderer-dev.js 12:36-59
 @ ./node_modules/gatsby/cache-dir/commonjs/public-page-renderer.js
 @ ./node_modules/gatsby/cache-dir/commonjs/gatsby-browser-entry.js
 @ ./src/components/header/index.js
 @ ./src/components/header/header.stories.js
 @ ./src sync .stories.js$
 @ ./.storybook/config.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/config.js (webpack)-hot-middleware/client.js?reload=true

Steps to reproduce

Expected result

Storybook should compile & serve

Actual result

Fatal Crash: See Above

Environment

System:
    OS: macOS 10.14
    CPU: x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.13.0 - ~/.nvm/versions/node/v10.13.0/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.13.0/bin/npm
  Browsers:
    Safari: 12.0
  npmPackages:
    gatsby: 2.0.24 => 2.0.24
    gatsby-image: ^2.0.20 => 2.0.25
    gatsby-plugin-manifest: ^2.0.9 => 2.0.12
    gatsby-plugin-offline: ^2.0.16 => 2.0.20
    gatsby-plugin-react-helmet: ^3.0.2 => 3.0.5
    gatsby-plugin-sharp: ^2.0.14 => 2.0.16
    gatsby-source-filesystem: ^2.0.8 => 2.0.12
    gatsby-transformer-sharp: ^2.1.8 => 2.1.9
@sidharthachatterjee sidharthachatterjee added status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby labels Dec 26, 2018
@sidharthachatterjee
Copy link
Contributor

Thank you for opening this @NathHorrigan

Please add the following to .storybook/webpack.config.js to fix this

// Prefer Gatsby ES6 entrypoint (module) over commonjs (main) entrypoint
  defaultConfig.resolve.mainFields = ["browser", "module", "main"]

DSchau pushed a commit that referenced this issue Dec 27, 2018
…k config (#10669)

The steps in the current documentation for using Storybook with Gatsby (https://www.gatsbyjs.org/docs/visual-testing-with-storybook/) don't work because importing Link from gatsby in any component breaks. 

This is because several internals get required as a result of requiring anything from the default commonjs build (which would have been tree shaken before #9123) and eventually require a non-existent `pages.json`. 

More details are at #10668

This pull request updates the documentation with a quick fix (setting resolve.mainFields in the storybook webpack config override thereby _preferring_ es6 gatsby over commonjs) 

Fixes #10662
gpetrioli pushed a commit to gpetrioli/gatsby that referenced this issue Jan 22, 2019
…k config (gatsbyjs#10669)

The steps in the current documentation for using Storybook with Gatsby (https://www.gatsbyjs.org/docs/visual-testing-with-storybook/) don't work because importing Link from gatsby in any component breaks. 

This is because several internals get required as a result of requiring anything from the default commonjs build (which would have been tree shaken before gatsbyjs#9123) and eventually require a non-existent `pages.json`. 

More details are at gatsbyjs#10668

This pull request updates the documentation with a quick fix (setting resolve.mainFields in the storybook webpack config override thereby _preferring_ es6 gatsby over commonjs) 

Fixes gatsbyjs#10662
@pardesiaka08
Copy link

Hi, I am facing an issue on integrating storybook with gatsby.As my gatsby site consists of components which uses graphql queries which are giving an error.Is there any solution to create storybook for such components.

footerSocialIcons Component

code_capture

Storybook for FooterSocialIcons Component

import React from "react";
import { storiesOf } from "@storybook/react";
import FooterSocialIcons from './footerSocialIcons';

storiesOf("FooterSocialIcons", module).add(
"with diffent SocialIcons",
() => (
),
{ info: "It should shows social icons with links" }
);

Error while creating the storybook for this component

It appears like Gatsby is misconfigured. Gatsby related graphql calls are supposed to only be evaluated at compile time, and then compiled away,. Unfortunately, something went wrong and the query was left in the compiled code. .Unless your site has a complex or custom babel/Gatsby configuration this is likely a bug in Gatsby.
Error: It appears like Gatsby is misconfigured. Gatsby related graphql calls are supposed to only be evaluated at compile time, and then compiled away,. Unfortunately, something went wrong and the query was left in the compiled code.

.Unless your site has a complex or custom babel/Gatsby configuration this is likely a bug in Gatsby.

Any solutions?

@rstacruz
Copy link
Contributor

rstacruz commented Apr 5, 2019

I don't think this issue should be closed - it still persists in the latest versions of Gatsby (2.3) and Storybook (5.0), with the instructions in the Gatsby docs.

This error happens anytime you try to include a component that uses <StaticQuery> or useStaticQuery:

Error: It appears like Gatsby is misconfigured. Gatsby related graphql calls are supposed to only be evaluated at compile time, and then compiled away,. Unfortunately, something went wrong and the query was left in the compiled code.

I don't think there's an easy solution to this one at the moment.

@JenniferMcIntyre-xyz
Copy link

Anyone have a recent update to this?

@dakebl
Copy link
Contributor

dakebl commented May 19, 2019

I don't think this is strictly an issue with Gatsby, but a mismatch of context when Storybook is rendering components that expect the GraphQL data mesh for data fetching:

storybookjs/frontpage#43 (comment)

Hey @domyen, I looked into it and it makes sense. Gatsby provides static files that are created during the initial compilation of the build process, which is when the GraphQL queries are run. This means that the data collected from the query has already been used to write the file that will be served as part of the production app.

In the Storybook context however, we render the component in isolation and therefore forego the initial compilation that fetched the data for the component.

I did find a post from a few months ago that worked around this issue: gatsbyjs/gatsby#12007 (comment)

The short of it being that adding babel-plugin-remove-graphql-queries (as we have in our webpack config) will remove the GraphQL queries all together but only when the NODE_ENV is set to production. Storybook sets the NODE_ENV to development by default, so we can fix this by updating the npm scripts to set NODE_ENV:

{
    "storybook": "NODE_ENV=production start-storybook -p 6006 -s static",
    "build-storybook": "NODE_ENV=production build-storybook -s static",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants