GROQ query returns empty array in Next.js but works in browser/Vision
This is a very common issue right now, and it's almost certainly related to a recent breaking change in Sanity's API!
As of API version 2025-02-19 (released February 24, 2025), the default perspective changed from raw to published. This means queries now only return published documents by default, not drafts. The Vision plugin in Studio still uses the old default, which is why your query works there but returns empty arrays in your Next.js app.
The fix: Explicitly set the perspective in your client configuration or queries.
If you're using the newer @sanity/client or fetching data in Next.js:
// In your sanity client config
import { createClient } from '@sanity/client'
export const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2025-02-19', // or whatever version you're using
useCdn: true,
perspective: 'previewDrafts', // Add this to see drafts + published
// or perspective: 'raw' to see everything including versions
})Or if you're using next-sanity:
import { createClient } from 'next-sanity'
export const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
apiVersion: '2025-02-19',
perspective: 'previewDrafts', // Add this
useCdn: false, // Set to false for server-side rendering
})Perspective options:
published(new default) - only published documentspreviewDrafts- drafts take precedence over publishedraw- everything including drafts, published, and versions
Why your browser/Vision queries work: When you query directly in the browser or Vision, you're likely hitting an older API version or the tools are automatically using a different perspective. Your Next.js app is probably using the latest API version which has the new default.
Quick debugging tip: Add perspective=raw or perspective=previewDrafts as a URL parameter to your query to test if this is indeed the issue before changing your code. If you suddenly start seeing results, you've confirmed the problem!
Show original thread9 replies
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.