If you inherited a Next.js site and the Sanity content is stale, leaking drafts, or oddly expensive to fetch, look at the Sanity client configuration before you touch anything else. Three settings, apiVersion, useCdn and perspective, cause a surprising share of the problems I see on inherited projects. They sit in one small file that nobody has opened since the first commit.
Symptoms of a misconfigured Sanity client
These are the complaints that usually trace back to the client file:
- Editors publish, and the live site shows the old version for hours, or shows it on one page and not another.
- Draft or unpublished titles appear in listings, search, or the sitemap.
- Duplicate entries appear in a list: one for the draft, one for the published document.
- The Sanity API request count is high, even though traffic is modest.
- Preview works for one editor and not another.
- Queries behave differently on your laptop than on production.
None of these are exotic. They are the result of defaults nobody chose on purpose.
The broken pattern I find most often
Here is a client file in the shape I run into on inherited codebases:
// src/lib/sanity.ts (inherited, problematic) import {createClient} from 'next-sanity' export const client = createClient({ projectId: 'abc123xy', dataset: 'production', apiVersion: 'v1', useCdn: false, token: process.env.NEXT_PUBLIC_SANITY_TOKEN, }) Every line has a problem. The project ID and dataset are hardcoded, which makes staging impossible. The API version is v1, an ancient behaviour set. The CDN is off for every request, including public pages. And the token is exposed to the browser through a NEXT_PUBLIC variable, which is a security problem on its own (I cover that in the post on a Sanity API token exposed in a Next.js site).
Cause 1: an old or missing apiVersion
The apiVersion pins the behaviour of the query API to a date. Old values like v1 give you old behaviour, and some defaults differ between versions. In particular, older API versions default to a perspective that returns raw documents, which means drafts and published versions both appear in query results. That is where the duplicate list entries come from.
The fix is a fixed date string, chosen deliberately, set once and bumped on purpose. Do not use a value that tracks today's date, because then your queries can change behaviour between deploys without any code change. After you bump it, run your main queries and compare results before shipping.
Cause 2: useCdn set wrongly in both directions
The API CDN serves cached responses and is meant for published content. Two opposite mistakes happen:
- useCdn is false everywhere. Every page build and every cache miss hits the live API. This raises latency and request counts, and it is a common reason a Sanity bill climbs. See Sanity API bill spiking on Next.js sites for the full picture.
- useCdn is true in draft or write-then-read code. The CDN can serve a slightly older response, so a preview or a server action that reads right after a write looks stale.
The rule I use: published, public reads use the CDN. Anything that needs fresh data or a token for drafts does not.
Cause 3: the wrong perspective
The perspective decides which version of a document a query sees. Published means only published documents. A drafts perspective layers draft edits over published content. Raw returns everything, drafts and published as separate documents.
If public pages run on raw or on a drafts perspective, unpublished work leaks onto the live site, or you get each document twice. If preview mode runs on published, editors see nothing new and assume preview is broken, which is the situation in Sanity preview not working.
Cause 4: one client doing two jobs
The deeper issue is that a single exported client is trying to serve public pages, draft preview, and sometimes server-side writes. Those need different settings. The cleanest fix is two clients with explicit roles.
The fix: two clients with fixed settings
// src/sanity/lib/client.ts import {createClient} from 'next-sanity' const base = { projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!, dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!, apiVersion: '2025-11-01', } // Public pages: published content only, via the CDN. export const client = createClient({ ...base, useCdn: true, perspective: 'published', }) // Draft mode only: fresh reads, drafts layered over published. // Import this from server code only. The token must never reach the browser. export const previewClient = createClient({ ...base, useCdn: false, perspective: 'drafts', token: process.env.SANITY_API_READ_TOKEN, }) Then choose the client where you fetch, based on whether draft mode is on:
// src/sanity/lib/fetch.ts import {draftMode} from 'next/headers' import {client, previewClient} from './client' export async function sanityFetch<T>( query: string, params: Record<string, unknown> = {}, tags: string[] = [], ): Promise<T> { const {isEnabled} = await draftMode() if (isEnabled) { return previewClient.fetch<T>(query, params, {cache: 'no-store'}) } return client.fetch<T>(query, params, {next: {tags}}) } The token stays in a server-only variable without the NEXT_PUBLIC prefix. Public fetches carry cache tags so a webhook can invalidate them precisely, as described in Sanity webhooks and Next.js ISR revalidation.
What it costs to ignore this
The cost is not dramatic on any one day. It is a slow leak: unpublished content indexed by Google, editors who stop trusting the publish button, higher API and hosting usage, and a security hole if the token is public. Each of those is cheap to fix now and expensive to explain later.
How to verify the fix
- Search the codebase for createClient. You should find one file, not five.
- Search for NEXT_PUBLIC variables containing a token. There should be none.
- Create a draft-only document and confirm it does not appear on any public page, listing, or sitemap while draft mode is off.
- Turn draft mode on and confirm the draft appears.
- Publish an edit and confirm the live page updates through your revalidation path.
- Compare request counts in the Sanity project usage dashboard over the following week.
What to ask a developer you are about to hire
- Which apiVersion is the project on, and why that date?
- Which clients exist, and which perspective does each use?
- Where does the token live, and can any of it reach the browser?
- How do public fetches get invalidated after a publish?
If the answers are vague, the client file is probably not the only thing that needs attention.
Need this checked on your site
Reviewing the Sanity client, token handling, caching and perspective setup is a standard part of the performance and Sanity audit I do, which starts from $800. If you want it looked at, or fixed as part of a wider cleanup, see the details on the services page.
