Trouble shooting empty array response from groq queries in app development
This is a very common issue! The most likely culprit is the API perspective your client is using. Vision and your app may be querying different perspectives by default, which explains why Vision shows data but your app returns empty arrays.
The Root Cause: Perspective Differences
Sanity Vision typically uses the raw perspective by default, which returns all documents including drafts. However, as of API version 2025-02-19, the default perspective for API clients changed to published, which only returns published documents (not drafts).
If you're working with draft documents in Vision but your app client is using the published perspective, you'll get an empty array because those drafts aren't "published" yet.
How to Fix It
When initializing your Sanity client in your app, explicitly set the perspective to match what you need:
import { createClient } from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
useCdn: false, // Use false for development to see latest changes
apiVersion: '2025-02-19',
perspective: 'previewDrafts' // or 'raw' to match Vision's behavior
})For development/preview environments, use perspective: 'previewDrafts' which will prioritize draft documents over published ones. For production, use perspective: 'published' to only show published content.
Other Troubleshooting Steps
Check if documents are published: In Sanity Studio, make sure you've clicked "Publish" on your documents. Draft documents have IDs prefixed with
drafts.- if all your content is drafts and you're querying with thepublishedperspective, you'll get nothing back.Disable CDN in development: Set
useCdn: falsein your client config during development. The CDN can cache results and won't reflect immediate changes.Verify your dataset: Make sure your client is pointing to the same dataset you're using in Vision (usually
productionordevelopment).Check API version: If you're using an older API version, the default perspective might be different. Explicitly setting it avoids confusion.
Console log the full client query: Add debugging to see exactly what's being sent:
const result = await client.fetch(query, params)
console.log('Query:', query)
console.log('Params:', params)
console.log('Result:', result)The perspective mismatch is by far the most common reason for this specific issue - Vision showing data but your app returning empty arrays with no errors. Setting perspective: 'previewDrafts' should get you back on track!
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.