gatsby: Uncaught Error: The result of this StaticQuery could not be fetched.
Description
I have deployed site through Vercel and I’m facing following error.
Uncaught 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 h (gatsby-browser-entry.js:77)
at e.default (products.js:15)
at Ki (react-dom.production.min.js:153)
at Fa (react-dom.production.min.js:175)
at vo (react-dom.production.min.js:263)
at cu (react-dom.production.min.js:246)
at ou (react-dom.production.min.js:246)
at Zo (react-dom.production.min.js:239)
at react-dom.production.min.js:123
at KqkS.t.unstable_runWithPriority (scheduler.production.min.js:19)
Error shows when I click on Products page or even Contact page where isn’t static query declared. This error didn’t exist while developing. Here’s products.js file:
import { useStaticQuery, graphql } from 'gatsby'
import Product from '../components/product/product'
const Products = () => {
const {
allShopifyProduct: { edges },
} = useStaticQuery(graphql`
query Products {
allShopifyProduct {
edges {
node {
title
productType
shopifyId
handle
images {
id
localFile {
childImageSharp {
fluid {
srcWebp
tracedSVG
base64
srcSetWebp
}
fixed {
src
}
}
}
}
variants {
sku
price
title
shopifyId
}
}
}
}
}
`)
return (
<Wrapper>
<ProductsGrid>
{edges.map(({ node }) => {
return <Product key={node.shopifyId} product={node} />
})}
</ProductsGrid>
</Wrapper>
)
}
export default Products
I added query Products because I thought it’ll help but it didn’t.
Steps to reproduce
Expected result
website should work in production
Actual result
some problems with static query I guess
System: OS: macOS 10.15.6 CPU: (4) x64 Intel® Core™ i5-7360U CPU @ 2.30GHz Shell: 5.7.1 - /bin/zsh Binaries: Node: 12.14.0 - /usr/local/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.8 - /usr/local/bin/npm Languages: Python: 2.7.16 - /usr/bin/python Browsers: Chrome: 84.0.4147.125 Firefox: 79.0 Safari: 13.1.2 npmPackages: gatsby: ^2.17.4 => 2.24.10 gatsby-cli: ^2.12.62 => 2.12.65 gatsby-image: ^2.2.29 => 2.4.13 gatsby-plugin-google-analytics: ^2.0.13 => 2.3.13 gatsby-plugin-layout: ^1.0.11 => 1.3.10 gatsby-plugin-manifest: ^2.2.23 => 2.4.20 gatsby-plugin-offline: ^3.0.16 => 3.2.20 gatsby-plugin-react-helmet: ^3.1.13 => 3.3.10 gatsby-plugin-root-import: ^2.0.5 => 2.0.5 gatsby-plugin-sharp: ^2.2.32 => 2.6.21 gatsby-plugin-styled-components: ^3.3.10 => 3.3.10 gatsby-plugin-transition-link: ^1.20.2 => 1.20.2 gatsby-plugin-web-font-loader: ^1.0.4 => 1.0.4 gatsby-source-filesystem: ^2.1.33 => 2.3.22 gatsby-source-graphql: ^2.6.2 => 2.6.2 gatsby-source-shopify: ^3.2.24 => 3.2.24 gatsby-transformer-sharp: ^2.3.0 => 2.5.11 npmGlobalPackages: gatsby-cli: 2.12.87
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 9
- Comments: 83 (15 by maintainers)
I may have figured out what the issue is. I haven’t run into this error for my last few builds now after making this change.
When I set up my new Gatsby project, I changed the case of the default components (layout.js and seo.js) to be PascalCase (Layout.js, SEO.js). I know that git by default ignores changes in filename case, so I had to update my git.config to register these changes by running
git config core.ignorecase false.However, it appears that the original lowercase file was never completely removed. It didn’t appear in my repository or local branch, but I noticed that when I staged my changes in VSCODE, both the lowercase and uppercase versions of the files were listed!
To fully remove the old versions of the files, I had to run:
Pushing that change seems to have fixed the Static Query error.
I guess I figured it out. For some reason the problem was in
useStaticQueryhook. I switched toStaticQuerycomponent and problem is solved.As many people have noticed, getting the case wrong on import names can cause this problem if you’re on OSX as Gatsby is case insensitive when setting up static queries but OSX isn’t so if you have
import foo from "./foobut the file is actuallyFoo.js— it’ll seem to work except for the static query failing.An easy way to find where there’s this problem in your code base is by adding this plugin — https://github.com/Urthen/case-sensitive-paths-webpack-plugin — as it enforces strict case sensitivity.
You can add it by:
npm install case-sensitive-paths-webpack-pluginThen in your
gatsby-node.jsfile, modify the webpack config to add the plugin:It’ll then print errors to your terminal that look like the following:
I ran into the same issue earlier and it was a case error in the Importing component, but not the Imported component where the static query is made:
Using the example above, the solution for me was to simply change the filename of the relevant import in
index.jsto match the filename of the imported component:I initially attempted
gatsby clean, removednode_modulesand reinstalled them but that didn’t work for me. Although I doubt that it was the problem to begin with, because after playing around for a while the only way I could cause the error to be thrown was unmatching cases between theimportstatement and the filename of the imported component.Also not sure if it’s worth mentioning, but might as well throw it out there: in
.cache/gatsby-browser-entry.js, it appears thatReact.useContext(StaticQueryContext)returns an empty object when the aforementioned case mismatch exists, which is what lead to the error being thrown in the first place.Note that this is just what happened in my particularly case, but I hope it helps!
Edit: I forgot to mention that I’m using macOS (10.15.7, case-insensitive).
same problem any new to solve it?
any news on that? 😭
I am now encountering this issue in different ways:
StaticQueryI geht this issue: https://github.com/gatsbyjs/gatsby/issues/26038, meaning: Instead of the component,Loading (StaticQuery)is shown.useStaticQuerythe entire app blows up, returningThe 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/issuesThis is the component: https://github.com/FNSKtZH/ae2/blob/master/src/components/Home.js
This happens only in production, so very bad 👿
PLEASE SOLVE THIS ISSUE
It has been around for ages and many have endured it. I somehow have a feeling that it might be related to https://github.com/gatsbyjs/gatsby/issues/19618, which has also hit very hard, myself included.
Sorry for being so explicit but Gatsby being so big, this should have been solved a long time ago.
Result of running
gatsby info --clipboard:The app is served using caddy v2.3.0 on docker: https://github.com/FNSKtZH/ae2/blob/master/backend/caddy/Caddyfile
Also, this appears when checking the caching:
To be honest, I have no idea what it means and if necessary, how to change. Especially as this list makes it seem like some things are set up wrong while the docs (https://www.gatsbyjs.com/docs/caching/#page-data) show for instance for page-data that exactly what I seem to have set is right, although this list makes it seem to be wrong??? The expected value is not what the docs say is needed.
Concerning https://github.com/gatsbyjs/gatsby/issues/26563#issuecomment-704702783
@wardpeet says:
Gatsby is supposed to create a static site. That’s one of the things that make it interesting for many. If you have to get a doctorate in setting headers for files served that kind of destroys the point.
Why don’t you do that? Would that solve our issues?
Ok, so building using
gatsby build --no-uglifygave me an idea of where the error was coming from. Changed that formuseStaticQueryto aStaticQuerycomponent and the error went away.The site looks great now EXCEPT there is a random “Loading (StaticQuery)” inside a div on the page. This has to be related, but at least it’s not breaking anything now.
Again, this only happens on the production build.
Just trying to follow along with the tutorial and I’m stuck on this Unhandled Runtime Error. It does not occur on the Gatsby Cloud hosted version, however, it does occur locally. Tried incognito, doesn’t work. Tried clean, clearing browser cache, reinstalling packages, scouring the code (most of it is copy pasted from the tutorial), nothing works.
It started when I added {mdx.frontmatter__slug}.js in part 6 of the tutorial to generate page templates. Instead I get ‘Error in function useStaticQuery in ./.cache/static-query.js:83’
I’m really stumped, by the tutorial. Wow.
Figured it out. For my issue anyway, but it seems as if other peoples issue may be stemming from the same kind of symptoms.
For me it was
emotion-cache.In a Netlify production build, it would fail the first time a page would load while using the
useStaticQueryhook. If you would refresh, it would not reproduce the error… so only on first load. This got me thinking it might have something to do with caching strategy. I removed my custom implementation of emotion-cache and now everything appears to be just fine.This is a rather unique case, but hopefully it helps others narrow this down.
I too am experiencing this issue and it’s definitely not a cache issue for me. I too do not experience this issue locally.
I’m setting up a staging environment with no cache, just hosting the files on S3, and I am getting an undefined property error. It’s like the query never ran. I empty the S3 bucket manually and redeploy, and the issue is still there.
My current dependencies are:
🤔 I assumed it was caching headers or the platform not purging but it seems like we should add a hash to the file
https://github.com/jsg2021/gatsby-staticquery-error-repro this is my min reproduction… in gatsby 2 it’s fine.
Ah, mixing
gatsby-plugin-mdxandgatsby-plugin-feedtrigger thisThis error seems to happen to so many people, for completely different reasons. Isn’t it possible to send more information along with this error, so we have the smallest chance to find what’s going on?
The query could not be fetched: XYZ did not workwould solve so many issues.We’ve been seeing this error on all the versions of gatsby we’ve tried, we tried a ton of fixes suggested here and in the dozens of other issues with this error, and nothing ever worked. From v2.20 to today’s 3.10 in my history.
In my case, I used
useStaticQueryinhtml.jsfile and it crashed in production build only. Worked just fine in dev mode.Changing
useStaticQuerytoStaticQueryfixes the issue!UPDATE: well, it doesn’t work. There is “Loading (StaticQuery)” inside a div on the page.
Same problem. After replacing
useStaticQuerywith<StaticQuery />it started working for me. Removingnode_modulesandpackage-lock.jsondid not help.@caesarsol yes I do have, did removing it solved for you?
More information on my findings: the cause seems to be the
StaleWhileRevalidatestrategy in fetchingpage-data.jsonfiles: https://github.com/gatsbyjs/gatsby/blob/e4fd0ad8cda56996f42e0a2b9fd6b580823289c9/packages/gatsby-plugin-offline/src/gatsby-node.js#L144-L145This makes every request to
page-data.jsonreturn with an old version of the file, which contains the incorrectstaticQueryHashes. If you compare the requests in the errored page and after a refresh, you’ll see the content of the relativepage-data.jsonwill have differentstaticQueryHashes, being the latest correct.This also explains the apparent random nature of the error: it happens only after a query changes, and the ServiceWorker in the browser still has the previous version cached.
I am unsure on the best solution when one wants to keep the offline cache functionality:
npx check-gatsby-cachingand it’s all green.If someone has some ideas please get in touch!
Facing the same issue.
gatsby version 2.24.53
and I do not have gatsby-plugin-offline installed.
I just had the exact same error - ONLY ON PRODUCTION!
The error did not appear in dev.
I then realized that the only place I used a static query (via the useStaticQuery hook) could just as well accept a hardcoded value. After changing that the next update to production did not error any more. Phew!
I had updated the project just before. So all gatsby parts were up to date, see here: https://github.com/FNSKtZH/ae2/blob/5673e1d282e67e0c52ffff116e27c41d089e912e/package.json