Joint session with Vercel: How to build intelligent storefronts (May 15th)

Best practice for Sanity data fetching in Gatsby: top-level vs component queries

4 repliesLast updated: Nov 30, 2025

Using Sanity + Gatsby + Netlify, is the “best” option to get all the data your site needs at the top of the app and then pass it all down through props?
If you have graphql queries to sanity on each component, would they each get classed as 1 api request each, so potentially hundreds/thousands of api calls per build? When you could just call everything at the top and use 1 call? Or is Gatsby smarter and bulk calls? I guess it cant as the component queries rely on vars/data returned in the top call
AI Update

Great question! The good news is you don't need to worry about making hundreds or thousands of API calls with the Gatsby + Sanity setup. Gatsby is indeed "smarter" than that, and here's how it actually works:

How gatsby-source-sanity Actually Works

The gatsby-source-sanity plugin only makes two API requests during the entire build process, regardless of how many GraphQL queries you have in your components. Here's what happens:

This data is then loaded into Gatsby's local GraphQL data layer (an in-memory datastore). All your page queries, static queries, and component queries are executed against this local data layer at build time—not against Sanity's API.

The Answer to Your Question

You don't need to fetch everything at the top and pass it down through props. In fact, that's not the Gatsby way! Instead:

The only limitation is that useStaticQuery and StaticQuery cannot accept variables—they must be static queries. If you need variables (like getting data based on a page slug), use page queries instead.

Best Practices

For the Gatsby + Sanity + Netlify stack:

The plugin architecture is specifically designed to be efficient, so embrace Gatsby's data layer rather than trying to work around it!

Show original thread
4 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions