NextJS Server-Side Rendering Returns Empty Data from Sanity Client
Glad you found the solution! π This is actually a really common gotcha when first working with Next.js and Sanity together, especially with the App Router.
You've hit on an important distinction in how Next.js handles data fetching. When you call an async function that fetches data from Sanity using client.fetch inside a component, it runs in a different context than when you call it at the page level.
The Issue
In the Next.js App Router, pages (the page.tsx files) are React Server Components by default. When you fetch data at the page level, Next.js properly handles the async operation during the server-side render, waits for the data, and then renders your component with that data.
When you tried to fetch inside a component that was being rendered by that page, the timing and execution context likely caused issues - the component was probably trying to render before the data was available, or the fetch wasn't being properly awaited in the server component context.
The Solution (What You Did)
Moving your data fetching to the page level and passing it as props is exactly the right pattern:
// app/your-page/page.tsx
import { client } from '@/sanity/lib/client';
export default async function Page() {
const data = await client.fetch(`*[_type == "yourType"]`);
return <YourComponent data={data} />;
}This ensures the data is fetched during server-side rendering and your component receives it as a prop, fully hydrated and ready to render.
Additional Tips
If you need to control caching behavior for your Sanity content, you can add these exports to your page:
export const revalidate = 60; // Revalidate every 60 seconds
// or
export const dynamic = 'force-dynamic'; // Always fetch fresh dataAnd if you're working with draft/preview content, check out Draft Mode for Visual Editing support!
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.