gatsby: [Storybook] useStaticQuery fetching fails when used inside stories
Description
Fetching data via useStaticQuery within a story causes Storybook to break.
Steps to reproduce
I setup a reproduction repo, based on gatsby-default starter, following the description how to use Storybook with Gatsby.
Expected result
The Story in the repo is supposed to show the queried title “Gatsby Starter Default”.
Actual result
Error message:
The result of this StaticQuery could not be fetched.
This is likely a bug in Gatsby and if refreshing the page does not fix it, please open an issue in https://github.com/gatsbyjs/gatsby/issues
Error: The result of this StaticQuery could not be fetched.
This is likely a bug in Gatsby and if refreshing the page does not fix it, please open an issue in https://github.com/gatsbyjs/gatsby/issues
at useStaticQuery (http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:28626:11)
at Reproduction (http://localhost:54134/main.846d47b2b5df26ed1d0b.bundle.js:93:75)
at http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:2952:21
at http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:4255:16
at withSubscriptionTracking (http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:4283:16)
at http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:2952:21
at http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:4254:14
at http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:2980:20
at storyFn (http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:5367:30)
at oh (http://localhost:54134/vendors~main.846d47b2b5df26ed1d0b.bundle.js:41118:146)
Environment
System: OS: macOS 10.15.5 CPU: (16) x64 Intel® Core™ i9-9880H CPU @ 2.30GHz Shell: 5.7.1 - /bin/zsh Binaries: Node: 13.12.0 - /usr/local/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.4 - /usr/local/bin/npm Languages: Python: 2.7.16 - /usr/bin/python Browsers: Chrome: 84.0.4147.89 Firefox: 78.0.2 Safari: 13.1.1 npmPackages: gatsby: ^2.23.12 => 2.24.13 gatsby-image: ^2.4.9 => 2.4.14 gatsby-plugin-manifest: ^2.4.14 => 2.4.21 gatsby-plugin-offline: ^3.2.13 => 3.2.21 gatsby-plugin-react-helmet: ^3.3.6 => 3.3.10 gatsby-plugin-sharp: ^2.6.14 => 2.6.23 gatsby-source-filesystem: ^2.3.14 => 2.3.23 gatsby-transformer-sharp: ^2.5.7 => 2.5.11
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 27 (15 by maintainers)
Commits related to this issue
- fix: update storybook scripts to handle staticQueries REF: https://github.com/gatsbyjs/gatsby/issues/26099#issuecomment-691288286 — committed to IsaiahByDayah/personal-site by IsaiahByDayah 4 years ago
- Fix: storybook static queries (#8) * fix: add storybook build files to gitignore * fix: update storybook scripts to handle staticQueries REF: https://github.com/gatsbyjs/gatsby/issues/26099#iss... — committed to IsaiahByDayah/personal-site by IsaiahByDayah 4 years ago
- fix gatsby config for local storybook found https://github.com/gatsbyjs/gatsby/issues/26099#issuecomment-691288286 https://github.com/IsaiahByDayah/personal-site/commit/202dc7681148200db71cf465838d8... — committed to jscraftcamp/website by lowsky 3 years ago
- fix: Storybook didn't handle useStaticQuery, InstallTabs was not rendering taken from: https://github.com/gatsbyjs/gatsby/issues/26099#issuecomment-785821013 — committed to g-traub/nodejs.dev by g-traub 2 years ago
- fix: Storybook didn't handle useStaticQuery, InstallTabs was not rendering taken from: https://github.com/gatsbyjs/gatsby/issues/26099#issuecomment-785821013 — committed to nodejs/nodejs.dev by g-traub 2 years ago
- fix: Storybook didn't handle useStaticQuery, InstallTabs was not rendering taken from: https://github.com/gatsbyjs/gatsby/issues/26099#issuecomment-785821013 — committed to kevindavies8/nodejs.dev-for-full-stack-developer by kevindavies8 2 years ago
My workaround for gatsby 2.31.0 and storybook 6.1.15. I changed the options of babel-plugin-remove-graphql-queries in
.storybook/main.js.@blainekasten @sidharthachatterjee
I have the same problem with the latest version of Gatsby. I did some research and I think the problem comes from:
1.
babel-plugin-remove-graphql-queriesstill defaults tostatic/d:Considering this line: https://github.com/gatsbyjs/gatsby/blob/289c7f8c154a3d764621c39be585cee0da9c9f1c/packages/babel-plugin-remove-graphql-queries/src/index.js#L199
Recent versions of
gatsby(after “2.24.6”) host the static query data inpage-data/sq/dhowever the latest version ofbabel-plugin-remove-graphql-queriesstill defaults tostatic/d. I suppose this default is used by the Storybook’s babel-loader.2.
babel-plugin-remove-graphql-queriesdoesn’t check againstNODE_ENV=productionanymoreThis pull request edits
packages/babel-plugin-remove-graphql-queries/src/index.jswith the following diff:Now as most Storybook + Gatsby workspaces are advised by Gatsby docs to run Storybook with
NODE_ENV=productionandbabel-plugin-remove-graphql-queriesafter the mentioned PR doesn’t check againstNODE_ENV=productionthe queries aren’t processed anymore.Here is what I did to confirm that these two changes are the cause of the problem:
NODE_ENV=testasbabel-plugin-remove-graphql-queriesstill checks against it and processes the queries.NODE_ENV=test start-storybook -s publicpage-data/sq/dtostatic/d.Viola. The components containing the queries are loaded with the needed queries.
Cool, works for me atm. Definetly just at temporary workaround, so for now i’ve added a script to my package.json to copy the files and run it before storybook, which looks something like this:
Edit: I used a locked version (2.9.14) of
babel-plugin-remove-graphql-queriesso storybook still runs withNODE_ENV=productionThanks @DevSide, that’s working for me.
For everyone else, here’s a full example of what’s working for me on the latest version of Gatsby as of this comment (
2.32.4). This is derived from the official Gatsby doc about integrating Storybook and @DevSide’s comment.First, make a Gatsby addon to keep your
.storybook/main.jsfile clean:(sidenote: this really should just be an addon available on NPM - https://github.com/gatsbyjs/gatsby/discussions/28304)
If you aren’t using TypeScript, feel free to remove that portion.
Then, in your
.storybook/main.jsfile, add your addon:Lastly, install
babel-preset-react-appif you’re using the TypeScript settings in the example above.@smetzdev I don’t think
NODE_ENVhas any effect on hosting, since your files are compiled and then you host those files. Unless the library you are using for compilation treats different environments in a different manner. For examplegatsbytreats different envs differently to optimize the compilation for development and production, and it throws when you passNODE_ENV=test.I don’t think
storybooktreats them any different. It automatically setsNODE_ENV=developmenthowever I have been passingNODE_ENV=production, and recentlyNODE_ENV=testto get the queries processed bybabel-plugin-remove-graphql-queries. I can’t say I have noticed any change whatsoever.Update: As
reactitself handlesNODE_ENVdifferently to decide whether to export the development bundle or the minified one, it’s probably best not to depend onNODE_ENVto process queries. It would be best to introduce a new environment variable forbabel-plugin-remove-graphql-queries.FWIW, @mohsenkhanpour and @smetzdev 's fixes of
NODE_ENV=testandcp -r ./public/page-data/sq/d ./public/staticabove seem to be working for me. It feels a little “hacky” but for now, it’s getting the job done. Thanks, guys!I’ll close this issue as it’s not about Gatsby but the correct configuration for Storybook.
We’ve since then updated our Storybook guide (thanks to various contributors): https://www.gatsbyjs.com/docs/how-to/testing/visual-testing-with-storybook/
It also includes a section on a new Storybook Addon for Gatsby: https://storybook.js.org/addons/storybook-addon-gatsby
If you have further questions for configuring Storybook with Gatsby please go to our GitHub Discussions (https://github.com/gatsbyjs/gatsby/discussions/categories/help) or to the addon’s page. Thanks!
Yeah, if i change query, then i need to run
gatsby developto rebuildpublic/staticfolder and then restart Storybook server. But this is not a big problem, because i am using Storybook for pure UI prototyping, and queries changes are relatively rare. I just want to get my components working in Storybook without splitting them into pure components / containers.Thanks, @smetzdev! This is really working as temp fix.
Solution in short for other Storybook users:
Static query results loading in Gatsby runtime was added in
gatsby@2.24.7andbabel-plugin-remove-graphql-queries@2.9.15, so we need to lock previous versions:Important note:
resolutionsareyarnfeature, so it doen’t work withnpm. But you can just add babel plugin to dependencies list - it’s working too.But packages install through
npmbreaks my Storybook in another way (mdx default styling broke, don’t ask…), so i changed my package manager anyway.@marcysutton ok. I removed my change (add link to issue comment) —-
The comment is here: https://github.com/gatsbyjs/gatsby/pull/26077#issuecomment-665605068
As a sidenote to the actual issue, @muescha please do not edit other people’s comments. If you have new context to add, please add it in a new comment rather than modifying another person’s words, out of respect to them. Thank you!
Solution with
config.mode === 'development' ? 'develop-html' : 'build-html'doesn’t work for me anymore with build-storybook (it worked on previous versions). Since start-storybook is working great, can I use'develop-html'stage for the production build? It works, but any side effects in this solution? I suppose this is not worse than previous solution with NODE_ENV=“test” 🤔Gatsby 3.3.0 Storybook 6.2.8 – with @storybook/builder-webpack5 Webpack 5.28.0 – locked, because the newer version causes another troubles 😦
thank you very much for the answers above.
i was able to run and see images via gql using this topic and recipe https://github.com/PaulieScanlon/gatsby-recipe-storybook-js
start-storybook mode
"storybook": "NODE_ENV=production start-storybook -s public -p 6006 --debug-webpack",build-storybook /serve mode after build and running npx http-server i see no images , just Loading (static query)
"storybook-build": "NODE_ENV=production build-storybook -s public --debug-webpack","storybook-serve": "npx http-server ./storybook-static"in both cases babel plugin goes onto build mode
Destination folder storybook-static
static folder has folders with images and page-date/sq/d/ has gql cache json files as expected
my package.json https://pastebin.com/TRj35qmN
CC @smetzdev @jonniebigodes @mohsenkhanpour