Sanity data returns empty array in Next.js getStaticProps

14 replies
Last updated: Apr 27, 2022
Hi all, I'm a little stuck at the moment setting up a Next.js project. I've added content to a schema I've called Events, and when I publish posts and test them out w/ a Groq query, I can see the data, however when I fetch the data with
getStaticProps()
the object returns an empty array. The project is also set to public.
How should I begin to troubleshoot such an issue?
AI Update

Based on your description, this is a common issue that usually comes down to a few specific causes. Here's how to troubleshoot:

Most Likely Cause: API Version and Perspective

The most common reason for this is related to your Sanity client configuration, specifically the API version and perspective settings. As of the recent API version v2025-02-19, Sanity changed the default perspective from raw to published.

When you test queries in Vision (Sanity Studio's query tool), it likely uses the raw perspective by default, which shows both draft and published documents. However, your client in Next.js might be using the published perspective, which only returns documents that have been published.

Check Your Client Configuration

Look at where you're initializing your Sanity client and verify these settings:

import { createClient } from 'next-sanity'

export const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  apiVersion: '2024-01-01', // Use a recent date
  useCdn: false, // Set to false for getStaticProps
  perspective: 'published', // This is likely your issue
})

Quick Fixes to Try

1. Ensure Documents Are Published

Double-check in Sanity Studio that your Events documents are actually published (not just saved as drafts). Look for the green "Published" indicator in the document.

2. Try the raw Perspective

Temporarily change your client configuration to use perspective: 'raw' to see if that returns your data. This will help confirm if the issue is perspective-related:

export const client = createClient({
  // ... other config
  perspective: 'raw',
})

3. Disable CDN for Development

When using getStaticProps, set useCdn: false in your client configuration. The CDN can cache empty results during development:

useCdn: false, // Important for getStaticProps

4. Check Your Query Syntax

Make sure your GROQ query in getStaticProps matches what you're testing in Vision:

export async function getStaticProps() {
  const events = await client.fetch(`*[_type == "events"]`)
  
  console.log('Events:', events) // Add logging to debug
  
  return {
    props: {
      events
    }
  }
}

Additional Debugging Steps

  1. Add console.log statements in your getStaticProps to see what's actually being returned
  2. Check your project ID and dataset are correct in your environment variables
  3. Verify your schema type name - if your schema is called "Events" but you're querying for "events" (lowercase), that could cause issues
  4. Try fetching in the browser console using the same client configuration to isolate whether it's a Next.js-specific issue

If you're using the next-sanity toolkit, make sure you've properly configured it during setup, as it handles a lot of this configuration automatically.

The perspective issue is by far the most common culprit for this "works in Studio, empty in Next.js" problem, so start there!

Show original thread
14 replies
Here's the schema for
event
, as well as my
schema.js
Hi Racheal! Do you mean the Sanity client? (still a little green to this, sorry lol)
I'm assuming this is what you meant, right?
Exactly! It's possible that your client isn't picking up your projectId, so it would be worth it to double check that. Also, is your dataset private?
One thing that might be causing this is that I had tried to swap the project over to another projectID (the previous one had since been archived). Both datasets (production & "expose-noir") are public though
however the ID in my .env.local matches the projectID from the current project I'm working on
I've also just noticed that when I click "production" from the dropdown here, I suddenly get no output from the groq query. Could this be why?
It may be because you're not specifying an API version in your client.
should I set that to today's date?
I just manually changed the dataset value to "expose-noir" and the content seems to be appearing as intended now
I'm assuming this will be an issue when I move over to production though, how can I ensure that the CMS posts that are created are on the production dataset?
Yep! Use today's date. If you're using different datasets you'll need to migrate your content to production.
Thanks so much for your help! You've been great 🙂
Very happy to help! Let us know if you hit any more stumbling blocks!

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?