gatsby: "The result of this StaticQuery could not be fetched" when calling useStaticQuery within a custom hook and declaring the query in the usage of the custom hook
Description
Executing gatsby build throws the following 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
when using a custom hook that uses useStaticQuery, and passes the query parameter as a variable, as opposed to the example in the docs, which inlines the query in the useStaticQuery call.
An example of a custom hook which causes this behavior is:
export const useHelloWorld = query => useStaticQuery(query)
.allFile
.nodes[0]
.childYaml
.hello
Steps to reproduce
I have set up a demo repo to demonstrate the issue. In order to reproduce it:
$ git clone git@github.com:MeLlamoPablo/gatsby-static-query-bug-demo.git
$ cd gatsby-static-query-bug-demo
$ yarn
$ git checkout 546024b
$ yarn build # This should work
$ git checkout -
$ rm -rf .cache && yarn build # This should error
The code that causes the bug is the following:
diff --git a/src/components/header.js b/src/components/header.js
index 920b133..22ec421 100644
--- a/src/components/header.js
+++ b/src/components/header.js
@@ -1,9 +1,10 @@
-import { graphql, Link, useStaticQuery } from "gatsby"
+import { graphql, Link } from "gatsby"
import PropTypes from "prop-types"
import React from "react"
+import { useHelloWorld } from "../hooks/useHelloWorld"
const Header = ({ siteTitle }) => {
- const data = useStaticQuery(graphql`
+ const hello = useHelloWorld(graphql`
{
allFile(filter: {relativePath: {regex: "/Header\\/.+\\\\.yml/"}}) {
nodes {
@@ -15,8 +16,6 @@ const Header = ({ siteTitle }) => {
}
`)
- const hello = data.allFile.nodes[0].childYaml.hello
-
return (
<header
style={{
diff --git a/src/hooks/useHelloWorld.js b/src/hooks/useHelloWorld.js
new file mode 100644
index 0000000..9d9f358
--- /dev/null
+++ b/src/hooks/useHelloWorld.js
@@ -0,0 +1,7 @@
+import { useStaticQuery } from "gatsby"
+
+export const useHelloWorld = query => useStaticQuery(query)
+ .allFile
+ .nodes[0]
+ .childYaml
+ .hello
Expected result
The yarn build command should work successfully.
Actual result
The yarn build command fails with the error mentioned above.
Environment
System:
OS: Linux 4.15 Ubuntu 18.04.2 LTS (Bionic Beaver)
CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Shell: 5.4.2 - /usr/bin/zsh
Binaries:
Node: 8.12.0 - /tmp/yarn--1556700432647-0.45796071280625616/node
Yarn: 1.15.2 - /tmp/yarn--1556700432647-0.45796071280625616/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
Languages:
Python: 2.7.15 - /usr/bin/python
Browsers:
Chrome: 74.0.3729.108
Firefox: 66.0.3
npmPackages:
gatsby: ^2.3.34 => 2.3.34
gatsby-image: ^2.0.41 => 2.0.41
gatsby-plugin-manifest: ^2.0.29 => 2.0.29
gatsby-plugin-offline: ^2.0.25 => 2.0.25
gatsby-plugin-react-helmet: ^3.0.12 => 3.0.12
gatsby-plugin-sharp: ^2.0.35 => 2.0.35
gatsby-source-filesystem: ^2.0.33 => 2.0.33
gatsby-transformer-sharp: ^2.1.18 => 2.1.18
gatsby-transformer-yaml: ^2.1.12 => 2.1.12
Thanks for your help!
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 15 (2 by maintainers)
@heytulsiprasad I have the exact same situation.
developis fine, but I get this issue on prod with Vercel on random deployments.Is there anyone who faced this issue randomly out of nowhere? I don’t know why but my site is working fine locally everytime I run
gatsby developbut when pushing to Vercel (I am using vercel for deployments) I am getting this exact issue but only sometimes and randomly. Its like 9/10 times, and I get this done by doing another deployment and voila it works!It’s not like I’m stuck or something, but I feel like I should know why this happens and if there’s any real issue or bug why doesn’t it show up while running locally?
@MeLlamoPablo i’ve picked up on your issue and i have a answer for your:
From my undestanding of Gatsby, this is technically not a bug, it’s reported as such but it’s not. What you’re doing from Gatsby’s point of view is using a variable with a
StaticQuery, that’s not possible, more on that here. There’s some work being done in that department but as it’s alot of moving parts involved some more time will be necessary to make it work seamlessly.To make this work you would have to transform your hook code from:
To:
In your
/src/components/header.jsyou would have to make some adjustments to make it show anything without any kind of errors transforming your component into:With my proposed changes, issuing
gatsby developand opening uphttp://localhost:8000i’m presented with:@heytulsiprasad It could be related to Vercel, but I doubt it. People seem to have had this issue not long ago on Heroku. https://github.com/gatsbyjs/gatsby/issues/21392
To me, it sounds related to
gatsby buildin general.I am getting this error based on where I import the component that uses it.
I have tried it inside of my
layoutsfile as well as inside of the header that sits inside of that.After moving it into
pages/index.jsit works as expected.In my case, I want to be able to use this
staticQueryat a global level since it is for a navigation component that is accessible from every page.Any suggestions?