Sanity query ordering works in Vision but not in JavaScript

17 replies
Last updated: Jan 27, 2026
So I am still pretty new to sanity and I was trying to fetch query content from the release number descending, when I fetch it in the vision section it seems to do it fine but when I try fetch it in js it doesnt seem to show up like that on the site?
AI Update

It sounds like you're running into a common issue with perspectives and how they handle draft vs. published content! The Vision tool and your JavaScript code might be using different perspectives, which would explain why you're seeing different results.

Here's what's likely happening: As of API version 2025-02-19, the default perspective changed to published, which only returns published documents. However, Vision might be using a different perspective (like previewDrafts or raw) that includes draft content.

When you're ordering by a release number field in descending order, if some of your documents are still in draft state (with IDs prefixed by drafts.), they won't show up in your JavaScript query if you're using the published perspective.

To fix this, explicitly set the perspective in your JavaScript client:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  useCdn: true,
  apiVersion: '2025-02-19',
  perspective: 'previewDrafts' // This will prioritize drafts over published
})

The perspectives you can use are:

  • published - Only published documents (current default)
  • previewDrafts - Shows drafts when they exist, falls back to published
  • raw - Returns everything (both drafts AND published, which might give you duplicates)

For your use case with ordering by release number, previewDrafts is probably what you want, as it'll show you the latest version of each document whether it's published or not.

Also double-check that:

  1. Your documents are actually published (not just saved as drafts)
  2. Your GROQ query is identical in both Vision and your JS code
  3. You're using the same API version in both places

If you're still seeing issues, try adding a filter to explicitly exclude draft IDs if you only want published content: !(_id in path("drafts.**")) in your GROQ query.

Show original thread
17 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.

Was this answer helpful?