Closed alessioprestileo closed 4 years ago
Thanks @alessioprestileo for reporting. See more info here https://community.netlify.com/t/gatsby-and-netlify-cms-preview-error-with-rendering-staticquery/5215/2?u=erez
TLDR: You can't use static query in Netlify CMS previews as the CMS runs in browser context while static query runs during build time. The solution is to make sure the relevant shared components have their data injected as props.
Closing, as we can continue the discussion in the community thread.
@erezrokah
The solution is to make sure the relevant shared components have their data injected as props.
Could you please explain this in other words? how can I pass data that I get from a staticQuery
to a widget component as a prop?
Could you please explain this in other words? how can I pass data that I get from a
staticQuery
to a widget component as a prop?
Hi @AbdallahAbis, you can't pass data from a staticQuery
to a widget component.
staticQuery
is evaluated during build time whereas widget components run in the browser.
You should use a page query instead.
@erezrokah cool, but still, how do I pass it the data returned from a query in general?
@erezrokah But this is a template page, not a Netlify custom widget
Correct @AbdallahAbis, I think I missed you point. You can see here what is passed by the CMS to custom widgets. The value passed to the widget is parsed from the markdown file and is unrelated to any query done by Gatsby.
Can you clarify your use case? What are you using a custom widget for?
@erezrokah I have a list of posts on my blog, each post has an author. I have a collection where you can add new authors or view an existing one. when viewing an existing author I have the custom widget listing all the articles a specific author posted.
So I'm using StaticQuery
to get all the markdown posts on my blog from that specific author and map through them and display them.
Have you considered using the relation widget? Each author can have a list of relation widgets, each one referencing a post.
If that doesn't work, you can maybe mimic the way the relation widget uses the query
API:
https://github.com/netlify/netlify-cms/blob/65343c1238af85e9151abb6bbba3770a08091176/packages/netlify-cms-widget-relation/src/RelationControl.js#L140
@erezrokah Mimicking the Relation widget
did the trick!
For anyone facing the issue, I think using the query
property method on widget component
will help.
https://github.com/netlify/netlify-cms/blob/65343c1238af85e9151abb6bbba3770a08091176/packages/netlify-cms-widget-relation/src/RelationControl.js#L140
import { groupBy } from 'lodash'
//...
async componentDidMount () {
const { payload } =
await this.props.query(
this.props.forID,
'articles', // Name of the collection you want to query
['author'], // The field/fields you want to query for on that collection.
'',
this.props.field.get('file')
)
const hits = payload.response.hits || []
const articleGroupByAuthor = groupBy(hits, hit => {
return hit.data.author
})
this.setState({
campaignArticles: articleGroupByAuthor
})
}
Describe the bug Result of StaticQuery could not be fetched. This happens only in the CMS (as shown in the picture) when trying to preview the index page, but not when rendering the same page in the website. The static query causing the error is:
To Reproduce You can clone this repo: https://github.com/alessioprestileo/netlify-cms-issue.git
Expected behavior The static query should work and return the same data as when rendering the index page in the website
Screenshots
Applicable Versions:
netlify-cms-app@2.12.16
git-gateway
Mozilla/5.0 (X11; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0
CMS configuration
Additional context