Error fetching data from Sanity using Next.js and getServerSideProps
Based on your code, I can see a few potential issues. The most likely culprit is that you're querying for published documents, but your content might only exist as drafts in Sanity Studio.
The Draft vs Published Issue
When you create documents in Sanity Studio, they initially exist as draft documents with IDs prefixed by drafts.. Your query *[_type == "product"] by default only returns published documents (without the drafts. prefix). If you haven't explicitly published your documents, they won't show up in your query results.
To fix this, you have a few options:
Option 1: Publish your documents in Studio
Go to your Sanity Studio, open each product and banner document, and click the "Publish" button. This will create published versions that your query can find.
Option 2: Use the previewDrafts perspective
Modify your client configuration to include drafts:
export const client = sanityClient({
projectId: '********',
dataset: 'production',
apiVersion: '2022-05-08',
useCdn: true,
token: process.env.NEXT_PUBLIC_SANITY_TOKEN,
perspective: 'previewDrafts', // This will show drafts with published fallback
});The previewDrafts perspective prioritizes draft documents over published versions, which is perfect for development.
Option 3: Query for drafts explicitly in GROQ
You can modify your query to exclude draft IDs:
const query = '*[_type == "product" && !(_id in path("drafts.**"))]';Or score drafts higher to prioritize them:
const query = '*[_type == "product"] | score(_id in path("drafts.**")) | order(_score desc)';Other Potential Issues
Token visibility issue
Your token is stored in process.env.NEXT_PUBLIC_SANITY_TOKEN. The NEXT_PUBLIC_ prefix makes it available on the client-side, which is fine for read operations. However, make sure:
- The token exists in your
.env.localfile - You've restarted your Next.js dev server after adding the env variable
- The token has read permissions for your dataset
API Version and CDN
Since you're using useCdn: true with a token, be aware that CDN caching and tokens can sometimes conflict. For development, try setting useCdn: false to rule out caching issues.
Legacy imports
Your schema.js is using the old Studio v2 syntax (part:@sanity/base/schema-creator). If you're following an old tutorial, this might work, but be aware that newer tutorials use Studio v3 syntax.
Quick Debug Steps
- Check if documents exist: Go to
https://yourstudio.sanity.studio/deskand verify your products and banners are there - Check document IDs: Click on a document and look at the URL - if it contains
drafts., they're unpublished - Test in Vision: In your Studio, go to the Vision plugin (usually in the toolbar) and run your query there to see what returns
- Console log the client config: Add
console.log(client.config())to verify your configuration is correct
The most common fix is adding perspective: 'previewDrafts' to your client config or publishing your documents in Studio. Let me know if that resolves it!
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.