I've exclusively used the gatsby-source-prismic plugin in so far (actually only twice so far), and switched to the gatsby-source-prismic-graphql for this (small) project.
const data = useStaticQuery(graphql`
query HomeQuery {
prismic {
home(lang: "de-de", uid: "home") {
page_title
body {
... on PRISMIC_HomeBodyFont_section {
type
label
primary {
font_name
font_info
buy_button_background_color
buy_button_text_color
}
}
}
}
}
}
`)
I did a really simple query to check for my function to display a simple slider:
When running my App locally, everything works fine, but as soon as I am deploying it to production (Netlify) I am getting the following console error: TypeError in Production: Cannot read property 'home' of undefined which is referring to the home in const fontSections = data.prismic.home && data.prismic.home.body.map((slice, index) => {
Why am I getting that error in production while it works totally fine locally?
Another thing that's really weird is that my design is displayed, only the simple slider does not work (just mapping over some fields and updating the z-index of a single slide on click).
Environment variables are set on Netlify, while I added them (for now) directly into my gatsby-config.js file so I guess that's not needed?!
I have not added my pages array / objects as the docs says they’re optional and I only have a home page/template as well as a info page/template — so there’s only some static page types inside Prismic and no need to dynamically createPages on build.
Using the Gatsby hook instead of their older StaticQuery Component was exactly what caused the issue. Just out of curiosity — Are you planning to support useStaticQuery in the near future or does this not really work with your source plugin?
Yes we do have plans on building support for useStaticQuery but there’s no ETA for the moment.
I saw that you’re not using a Pages folder to generate a URL for the homepage. Both useStaticQuery and StaticQuery are meant to be used inside components. So maybe that could be the problem? where are you rendering the Homepage?
Apologies for not being clear enough, everything worked fine when I switched to StaticQuery (just saw it in the docs today btw, definitely missed that).
Ok cool, cheers for letting me know, looking forward as I personally tend to prefer the hook over the component query style.
Re the pages: In that case I don’t need to generate pages (such as projects or posts) dynamically as there is only an index page and an about page (for now).
As everything works perfectly in production I thought there’s no need to specify those pages. Should I still add those to my gatsby-config.js?
The home page (as well as the about page) is simply rendered as index.js file from my pages directory.
Yes, in your case you don’t need to create dynamic pages to generate your content if you only have one page. The only thing that you may encounter is that you’ll need another page file if you want to generate a different URL aside from the homepage. But aside from that, you don’t need to use the dynamic page generation of the plugin, it is optional.