Discrepancy between post counts in Sanity Vision and local environment using the same query and perspective.

12 replies
Last updated: Oct 27, 2023
Hi 👋 I'm new to Sanity.I set up a groq query like so
*[_type == "post"] { ... }
When I set my Perspective in Sanity Vision to
previewDrafts
it returns 5 results (2 published, 3 drafts).With this

sanityClient({
  // ...
  useCdn: false,
  perspective: isDev ? 'previewDrafts' : 'published'
}) 
and then
console.log(sanityClient.config().perspective)
immediately before I make my groq query, it expectedly logs
previewDrafts
in dev mode....but I only get 2 results...

Why is there a discrepancy between Sanity Vision and my local environment Post counts when they both seem to be using the
previewDrafts
perspective with the same exact query? 🤔
AI Update

This is a classic issue! The previewDrafts perspective requires authentication with a token to access draft documents. When you use Sanity Vision in the Studio, you're already authenticated (logged in), so it works seamlessly. But when you're querying from your local environment, you need to explicitly provide a token.

Here's what's happening: Without a token, your client can only see published documents, even though you've set perspective: 'previewDrafts'. The perspective setting tells Sanity what you want to see, but the token provides the permission to actually see it.

The Solution

You need to add a read token to your client configuration. Here's how:

1. Create an API token in your project

Go to manage.sanity.io, select your project, navigate to the API tab, and create a token with Viewer permissions. You can also run:

pnpm dlx sanity manage

2. Add the token to your environment variables

In your .env.local file:

SANITY_API_READ_TOKEN="your-token-here"

3. Update your client configuration

sanityClient({
  // ...
  useCdn: false,
  perspective: isDev ? 'previewDrafts' : 'published',
  token: isDev ? process.env.SANITY_API_READ_TOKEN : undefined
})

The key point from the Sanity documentation on token handling is clear:

Authentication will also be required to use the previewDrafts perspective, a method of performing a GROQ query that returns the latest draft version of a document instead of an already-published document.

Important Security Note: Never expose this token in client-side code! Only use it in server-side contexts (API routes, server components, etc.). The token gives read access to all documents in your dataset, including drafts.

Once you add the token, your local environment should return all 5 results (2 published + 3 drafts) just like Vision does. The previewDrafts perspective will return draft versions when they exist, falling back to published versions for documents that don't have drafts.

Have you checked that
isDev
is true?
Yeah, for sure.I'm also console.logging
sanityClient.config().perspective
immediately before the groq query gets executed and its
previewDrafts
. 🤷
What API version are you using? Are you passing in a token?
This is in SvelteKit if anyone is familiar with this issue.
I've tried disabling everything that could potentially be caching or server-rendering, so I get the freshest possible data.
I'm using
v2023-10-26
in both Vision and in the Sanity client's config.
I don't think I'm passing a token? I'm not sure what that is. I've got the dataset and other env vars getting loaded in (the Sanity client works to fetch everything else, just the
perspective
in the Sanity client doesn't seem to do anything).
You cannot access drafts in an unauthenticated request.
Ohhh that's probably it.
I'll look into it. Thank you so much
user M
!
You’re welcome! Hope that fixes it!
That was it! Thank you so much again! I was stuck! ❤️
Awesome! Happy coding!

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?