Sanity Pioneers: Get early access to betas, extra AI credits, and a direct line to the engineering team. Apply now

Sanity query returns undefined in Next.js App Router despite correct setup

7 repliesLast updated: Nov 29, 2025

Hey all, I’m trying to use Sanity with Nextjs app router, but I always get undefined.• I used
https://www.sanity.io/plugins/next-sanity#quick-start as reference• Using the studio for the project, I definitely get matches.
• If I use the incorrect dataset name, I do get an error, so it seems to be connecting fine
How can I debug this?
Thanks

export default async function Page({ params }: { params: { slug: string } }) {
    const posts = await cmsClient.fetch<Banner[]>(`*[_type=="bannerDe"]`)
    try {
        const banner = await cmsClient.getDocument<Banner>("d13c0b40-b3e1-443a-a21f-a6bdbe128e6e");
        console.log(`banner `, banner);
    } catch (e) {
        console.error('error', e);
    }
    console.log(`posts `, posts);
...
}
AI Update

This is almost certainly a perspective issue related to a recent breaking change in Sanity's API! You're likely querying for documents that only exist as drafts, but your client is using the default published perspective which filters them out.

The Problem

As of API version 2025-02-19, the default perspective changed from raw to published. This means:

The Fix

Add the perspective parameter to your client configuration:

import { createClient } from 'next-sanity'

export const cmsClient = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  apiVersion: '2025-02-19',
  useCdn: false, // Disable CDN for draft content
  perspective: 'previewDrafts', // This will show drafts!
})

The available perspectives are:

Debugging Steps

const posts = await cmsClient.fetch(`*[_type=="bannerDe" || _id in path("drafts.**")]`)
export const cmsClient = createClient({
  // ... other config
  perspective: process.env.NODE_ENV === 'development' ? 'previewDrafts' : 'published',
})

This perspective change catches a lot of people, so you're definitely not alone! The fix is usually just adding that one perspective parameter to your client config.

Show original thread
7 replies

Was this answer helpful?

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.

Related contributions