
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes, this is a common pattern! There are a few solid approaches for handling global data like headers and footers in Next.js with Sanity.
With Next.js App Router, the best practice is to fetch your global data in your root layout (app/layout.tsx) and pass it down to your components. Since layouts are React Server Components by default, you can fetch data directly:
// app/layout.tsx
import { client } from '@/sanity/lib/client'
async function getGlobalData() {
return await client.fetch(`{
"header": *[_type == "siteSettings"][0].header,
"footer": *[_type == "siteSettings"][0].footer
}`)
}
export default async function RootLayout({ children }) {
const globalData = await getGlobalData()
return (
<html>
<body>
<Header data={globalData.header} />
{children}
<Footer data={globalData.footer} />
</body>
</html>
)
}If you need the global data in many deeply nested components, you can create a Context Provider:
// app/providers.tsx
'use client'
import { createContext, useContext } from 'react'
const GlobalDataContext = createContext(null)
export function GlobalDataProvider({ children, data }) {
return (
<GlobalDataContext.Provider value={data}>
{children}
</GlobalDataContext.Provider>
)
}
export const useGlobalData = () => useContext(GlobalDataContext)Then wrap your app:
// app/layout.tsx
export default async function RootLayout({ children }) {
const globalData = await getGlobalData()
return (
<html>
<body>
<GlobalDataProvider data={globalData}>
<Header />
{children}
<Footer />
</GlobalDataProvider>
</body>
</html>
)
}Don't fetch on the client side in components - As discussed in this community thread, fetching data client-side with useEffect will:
Caching - Remember that Next.js has its own caching layer. You can control revalidation with the revalidate export in your layout or use tag-based cache invalidation for more precise control.
Pages Router - If you're still using the Pages Router, you can fetch global data in _app.js using getInitialProps, though this disables automatic static optimization for all pages.
The layout approach is clean, performant, and follows Next.js best practices for server-side data fetching!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store