User encounters empty result due to Incognito mode in Sanity query
I can see your issue! You're querying for drafts using the v1 API with credentials, but there are a few important things to understand about how draft access works in Sanity.
The Problem: API Endpoint and Perspective
When you use withCredentials: true with the v1 API, you're sending the sanitySession cookie, which is correct for authenticated requests. However, there are several factors that could cause your query to return empty results even though it works in Vision.
Key Issues to Check:
1. Are you using the CDN endpoint?
When querying drafts, you must use the non-CDN endpoint (api.sanity.io), not the CDN endpoint (apicdn.sanity.io). The CDN is optimized for published content and doesn't serve draft documents. Make sure useCdn: false in your client configuration.
2. Missing perspective parameter
The v1 API doesn't automatically know you want to query drafts. You should explicitly set perspective=previewDrafts in your API request. This tells Sanity to prioritize draft documents over published ones.
3. API version default perspective change
If you're using a recent API version (specifically 2025-02-19 or later), be aware that the default perspective changed from raw to published. This means without explicitly setting the perspective, you'll only get published documents, which could explain your empty result.
The Solution
Here's how to fix your configuration:
// Make sure you're using the non-CDN client
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
useCdn: false, // Critical: must be false for drafts
apiVersion: '2024-01-01', // or your preferred version
token: 'your-token', // Recommended for programmatic access
perspective: 'previewDrafts', // This prioritizes drafts
withCredentials: true // If using cookie-based auth
})If you're making direct HTTP requests, ensure you're adding the perspective as a query parameter:
https://[PROJECT_ID].api.sanity.io/v1/data/query/[DATASET]?query=...&perspective=previewDrafts
Why Vision Works But Your App Doesn't
Vision (the query tool in Sanity Studio) automatically uses the correct API endpoint with proper authentication context and perspective settings. It's configured to access drafts by default when you're logged in, which is why your query works there but not in your app.
Understanding Perspectives
According to the perspectives documentation, there are several perspectives available:
raw- Returns all documents including both drafts and publishedpublished- Returns only published documents (now the default in newer API versions)previewDrafts- Prioritizes draft documents over published versions when both exist (ideal for your use case)
Since your query explicitly filters for _id in path("drafts.**"), you're only looking for draft documents. Without the proper perspective set, the API might not return these documents even with valid credentials.
Verify Your Setup
Double-check these items:
- β
useCdn: falsein your client configuration - β
perspective: 'previewDrafts'is set - β The user has appropriate permissions (Contributor role should work)
- β The draft document actually exists with the slug you're querying
- β
You're using the
api.sanity.ioendpoint, notapicdn.sanity.io
The combination of using the CDN endpoint or missing the perspective parameter is the most common reason for this issue. Setting perspective=previewDrafts with useCdn: false should resolve your empty result problem.
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.