🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Best Way to Fetch Global Data with GROQ and Next.js

11 replies
Last updated: Apr 30, 2021
What is the best way to fetch “global” data in with GROQ and Next? Things like content in the header and footer and menu items.
Apr 30, 2021, 1:31 PM
I know Next.js doesn’t have a good solution for this yet
Apr 30, 2021, 1:31 PM
Apr 30, 2021, 1:32 PM
Is fetching it in
_app.js
not good practice? That's what I've been doing to fetch my
siteSettings
singleton (site name, og:image, nav items, etc), which feeds it into each page.
Apr 30, 2021, 3:33 PM
If you mean using
getInitialProps
, then I would say, no. Or perhaps, it depends. See https://nextjs.org/docs/advanced-features/custom-app for some caveats.
Apr 30, 2021, 3:36 PM
If 
getServerSideProps
 or 
getInitialProps
 is present in a page, Next.js will switch to render the page on-demand, per-request (meaning Server-Side Rendering ).
Apr 30, 2021, 3:37 PM
hmm
getInitialProps
seems to work fine with SSG (next build & export) for me
Apr 30, 2021, 3:44 PM
Does it run for each page you're exporting?
Apr 30, 2021, 3:46 PM
good question -- i'll add a debug statement to check that
Apr 30, 2021, 3:46 PM
I'm curious about that, too! If so, you should probably look into the solutions I suggested, in order to limit the amount of API calls, unless the data changes on a page-by-page basis.
Apr 30, 2021, 3:47 PM
😬 this is only a few of the logs lol. yeah, i'm definitely gonna use one of those solutions above, thanks for the heads up!
Apr 30, 2021, 3:52 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hey all - what’s the quickest way to batch delete all documents of a certain type? It looks like the `sanity` cli tool allows...Jan 29, 2021
Deploying Next JS on VercelJan 20, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
DOMParser is not defined at defaultParseHtmlDec 3, 2020
Warning: Data for Page Exceeds the Threshold of 128 kBAug 17, 2022
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
How to get the number of nested/children components in a GROQ query.Jan 19, 2024
Issue with fetching data from an array of references and non-references in a groq query.Dec 14, 2023
Error in Next.js tutorial with event handlers in server components.Feb 28, 2024
GraphQL query resolving references on a _raw field in GatsbyMay 17, 2022

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle