Issue with groq query not returning desired result on front end
This is a classic issue related to perspectives and how the Sanity API returns draft vs. published documents. The behavior you're seeing - where Vision returns the full image data but your frontend only gets _type and _ref - almost certainly means you're querying different document states in each environment.
The Problem
When you query in Vision (inside Sanity Studio), it defaults to the raw perspective, which returns all documents including drafts. However, as of API version 2025-02-19, the default perspective for client queries changed from raw to published. This is a recent breaking change.
If that one problematic image exists only as a draft (perhaps it was just uploaded or the document containing it hasn't been published yet), your frontend query with the published perspective won't find the asset document. Instead of following the reference with asset->, it just returns the raw reference object with _type and _ref.
The Solution
You need to explicitly set the perspective in your Sanity client configuration. Here are your options:
Option 1: Use previewDrafts perspective (recommended for preview environments)
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2025-02-19',
useCdn: false,
perspective: 'previewDrafts' // This prioritizes drafts over published
})Option 2: Use raw perspective (returns everything)
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2025-02-19',
useCdn: false,
perspective: 'raw' // Returns all documents including drafts
})Quick Debugging Steps
Check if it's a draft issue: Look at the document containing that image in Studio - does it have a "Publish" button visible? If so, the image asset itself might only exist in the draft state.
Verify the asset is published: Go to the Vision plugin and run:
*[_type == "sanity.imageAsset" && _id == "YOUR_IMAGE_ASSET_ID"]If you only see a result with an ID starting with
drafts., that's your issue.Publish the document: The simplest fix might be to just publish the document containing that image, which should publish the image asset reference as well.
For production environments, you typically want the published perspective (the new default), but make sure all your content is actually published. For preview/development environments, use previewDrafts to see draft content.
The perspectives documentation has more details on how these different views work.
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.