Error when creating a client in Next.js with Sanity.io integration

10 replies
Last updated: Jul 6, 2023
Hello, yesterday i started to work on a new website and i want to fetch my data from my sanity the problem is when i try to create a client it gives me this error :
Error: It looks like you're using options meant for '@sanity/preview-kit/client', such as 'encodeSourceMapAtPath', 'encodeSourceMap', 'studioUrl' and 'logger'. Make sure you're using the right import.
i am on the nextjs 13 version and sanity v3 here my code

import { createClient } from 'next-sanity'

import { apiVersion, dataset, projectId, useCdn } from '../env'

export const client = createClient({
  apiVersion,
  dataset,
  projectId,
  useCdn,
  token: process.env.NEXT_PUBLIC_SANITY_EDITOR,
})
the problem is line 5, can someone help me ? thanks in advance
AI Update

This error message is a bit misleading! The error mentions options from @sanity/preview-kit/client, but based on your code, you're not actually using any of those options. This error typically occurs due to a version mismatch or caching issue with your dependencies.

Here are the steps to fix this:

1. Check your dependencies

First, make sure you have the correct versions installed. For Next.js 13 with Sanity v3, you should be using:

npm install next-sanity@latest @sanity/client@latest

Or if you're using yarn:

yarn add next-sanity@latest @sanity/client@latest

2. Clear your cache

After updating, clear your Next.js cache and node_modules:

rm -rf .next node_modules package-lock.json
npm install

3. Verify your client code

Your client configuration looks correct for basic usage. However, a couple of notes:

  • Token exposure: You're using NEXT_PUBLIC_SANITY_EDITOR which exposes the token to the browser. If this is a write token, you should not use the NEXT_PUBLIC_ prefix as this makes it publicly accessible. Only use public tokens for client-side code, and keep write tokens server-side only.

  • Better approach: Create separate clients for server and client usage:

// lib/sanity.client.ts
import { createClient } from 'next-sanity'
import { apiVersion, dataset, projectId, useCdn } from '../env'

// Public client (for client-side)
export const client = createClient({
  apiVersion,
  dataset,
  projectId,
  useCdn,
})

// Server client with token (for server-side only)
export const serverClient = createClient({
  apiVersion,
  dataset,
  projectId,
  useCdn: false, // Don't use CDN for writes
  token: process.env.SANITY_API_TOKEN, // No NEXT_PUBLIC_ prefix!
})

4. If you need preview/draft mode

If you're trying to set up preview mode (which might be why the error mentions preview-kit), you should use the Live Content API for modern Next.js applications, or if you specifically need the older preview-kit approach, you'd import from a different package:

import { createClient } from '@sanity/preview-kit/client'

But for your basic setup, stick with next-sanity's createClient.

After following these steps, restart your dev server and the error should be resolved. If you're still seeing issues, double-check that you don't have any conflicting imports or multiple versions of Sanity packages installed by running npm ls next-sanity and npm ls @sanity/client.

I found the error i got the wrong version of next-sanity do i upgraded it and it works now
You don't really need to use this line:
import { apiVersion, dataset, projectId, useCdn } from '../env'
just like
token: process.env.NEXT_PUBLIC_SANITY_EDITOR,
you can do the same for the others too
Hey, i also have got the same error, could you help me with this issue? how did you fix it?
go into your package json and update your next-sanity version to the latest then do npm i and it should fix the problem
it says
"next-sanity": "^4.3.3",
in my package.json
this is strange as it was working a while back and now i see this issue πŸ˜•
works now, strange
user D
what version of
next-sanity
are you now seeing that is working for you?
it's the same as the one i posted herem
"next-sanity": "^4.3.3"
Hiya, was there a fix to this issue? I'm also running
"next-sanity": "^4.3.3"
and seeing the same error. TY!

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?