Unlock seamless workflows and faster delivery with our latest releases – get the details

Debugging issue with using Sanity with Nextjs app router

7 replies
Last updated: Mar 4, 2024
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);
...
}
Mar 4, 2024, 6:15 AM
How is your client configured?
Mar 4, 2024, 6:54 PM
Like this
import {createClient} from 'next-sanity'

const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2023-05-03'

console.log(`${projectId} ${dataset}`);
export const cmsClient = createClient({
    projectId,
    dataset,
    apiVersion, // TODO <https://www.sanity.io/docs/api-versioning>
    useCdn: false, // TODO if you're using ISR or only static generation at build time then you can set this to `false` to guarantee no stale content
});
Mar 4, 2024, 9:00 PM
Is your dataset private?
Mar 4, 2024, 9:30 PM
Yes
Mar 4, 2024, 9:44 PM
Working now, thanks!
Mar 4, 2024, 9:50 PM
excellent!
Mar 4, 2024, 10:53 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?