Sanity query ordering works in Vision but not in JavaScript

17 replies
Last updated: Aug 5, 2022
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
Hey Oliver. Could you please group all your messages so we use a single thread?
user S
please group your question in a single thread so that we can keep track of all relevant information. It makes it easier to help you!
user M
💚
Impeccable timing
user F
!
oh yeah sure sorry
this is vue also
would rly appreciate the help doing this for a record label and want it to work perfectly :)
[9:30 PM] its fetching perfectly in vision though

basically I want it to show up on the site in descending order from release num, so DY-002, DY-001 etc
Can you share the link to the Vision query that's working properly? It should be right above your results pane in the plugin.
so it orders it properly by order(releaseNum desc)
The query in production on your website isn’t implementing an
order()
.
what do you mean sorry? i set this as the query *[_type == "post"] | order(releaseNum desc)
What I meant was that if the screenshot is from your production website (which the list order does appear to match), then your query is this:
*[_type == "post"]{ _id, releaseNum, artistName, releaseName, slug, releaseDate, releaseType, anr, }[0...50]
. You can open the Network tab in your dev tools and filter by
production
, and that’s the GET request.
If you’re also seeing that same order in your local or staging environment, where
order()
is implemented in the query, then maybe some screenshots of that would help.
oh im so stupid ahaha. So do I have to order it in the post.js in schemas and then pass it through to the vue file some how?
Not to worry!
No, you don’t need to worry about it in your schema. You’ll want to add
| order(releaseNum desc)
to your query that you’re doing in Vue using the Sanity Client.
thank you! that worked perfectly :D
Glad to hear it, Oliver! Have a good weekend!

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?