Why would a Query Work in the Vision Plugin from the Dashboard, but not in the Queries.js File?
This is a classic issue related to perspectives and how Sanity handles draft vs. published documents!
The Vision plugin in your Studio dashboard uses the raw perspective by default, which returns all documents (both drafts and published). However, when querying from your site's code, you're likely hitting a different perspective - and as of the recent API version 2025-02-19, the default perspective changed to published, which only returns published documents without the drafts. prefix.
Why this happens
When you create or edit a document in Sanity Studio, it exists as a draft with an ID like drafts.page-123. The published version has the ID page-123. Your Vision query sees both, but your site code might only be seeing published documents.
Solutions
1. Explicitly set the perspective in your client configuration:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
useCdn: false, // Important for draft content
apiVersion: '2025-02-19',
perspective: 'previewDrafts', // or 'raw' to match Vision
})The previewDrafts perspective will prioritize draft documents over published ones, similar to what you see in Vision.
2. Make sure your documents are actually published:
In Studio, check if those pages with diningMenu modules are published (not just saved as drafts). Look for the "Publish" button - if it's available, the changes are still drafts.
3. Filter out drafts in your query (if you only want published):
*[_type == 'page' && !(_id in path("drafts.**")) && modules[]._type == 'diningMenu']{
'slug': slug.current,
title
}4. Check your CDN settings:
If you're using useCdn: true, the CDN serves published content only and can be cached. Set it to false for draft content or when testing.
The perspectives documentation has more details on the different perspectives available and when to use each one. For local development and preview environments, previewDrafts is usually what you want. For production, stick with published.
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.