GROQ query returns empty array in Next.js but works in browser/Vision

9 replies
Last updated: Mar 28, 2021
Hi! I’m having an issue where the groq query works fine directly in the browser, also from the studio in vision, but not from the nextjs application, both serverside/clientside. There it just returns an empty array. CORS are set and all envs are loaded - everything seems to work, but have a hard time debugging. Any tips?
AI Update

This is a very common issue right now, and it's almost certainly related to a recent breaking change in Sanity's API!

As of API version 2025-02-19 (released February 24, 2025), the default perspective changed from raw to published. This means queries now only return published documents by default, not drafts. The Vision plugin in Studio still uses the old default, which is why your query works there but returns empty arrays in your Next.js app.

The fix: Explicitly set the perspective in your client configuration or queries.

If you're using the newer @sanity/client or fetching data in Next.js:

// In your sanity client config
import { createClient } from '@sanity/client'

export const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  apiVersion: '2025-02-19', // or whatever version you're using
  useCdn: true,
  perspective: 'previewDrafts', // Add this to see drafts + published
  // or perspective: 'raw' to see everything including versions
})

Or if you're using next-sanity:

import { createClient } from 'next-sanity'

export const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  apiVersion: '2025-02-19',
  perspective: 'previewDrafts', // Add this
  useCdn: false, // Set to false for server-side rendering
})

Perspective options:

  • published (new default) - only published documents
  • previewDrafts - drafts take precedence over published
  • raw - everything including drafts, published, and versions

Why your browser/Vision queries work: When you query directly in the browser or Vision, you're likely hitting an older API version or the tools are automatically using a different perspective. Your Next.js app is probably using the latest API version which has the new default.

Quick debugging tip: Add perspective=raw or perspective=previewDrafts as a URL parameter to your query to test if this is indeed the issue before changing your code. If you suddenly start seeing results, you've confirmed the problem!

Show original thread
9 replies
Hi User. Are you using asynchronous code to await the query?
Yes, pretty much following the different examples I’ve seen. Also trying different ways of connecting to Sanity. Currently trying with the helpers from next-sanity.
I have a working site using graphql, but since I have som issues with resolving references in Portable Text, I’m trying with groq instead.
You could try logging out props to see if anything (and if so, what) comes through. Or, feel free to paste a snippet of your code here to this thread.
I had a similiar problem using Next.js and getStaticProps, turned out that using template literal strings to include a variable in my groq-request was not the right way to do things. Got it working using the $parameter instead. Could that be it?Look here for code example:
https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1616688438160800
Thanks for the input, but I’m down to the simplest of queries, with no variables/parameters. If I paste the generated url for the query in a new browser, it works fine. So I’m guessing it’s a configuration issue for the fetch client. But it works fine for graphql queries, and the other things I’ve tried is taken from docs and examples.
Would like to paste some code but just not at the computer.
Tried also both at the getStaticProps and in the client on mount.
Just wanted to say I had a look, and when adding the token for sanity, the query works fine. The reason I stumbled is because the query works fine without the token when pasting the query link directly in the browser. Also when no error messages are giving, it makes it difficult to debug. Thanks for chiming in
user A
and
user S
.

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?