Draft content showing on your live Next.js site: Sanity fix
By Nayan Kyada · · 6 min read
Part of The Sanity + Next.js Guide
Draft content showing on a live Next.js site is one of the more alarming things an owner can discover. A half-written page, an old price, or an embargoed announcement appears on production while the editor swears they never clicked Publish. On Sanity sites this is almost always a configuration problem in the front end, not a Studio bug, and it is fixable in a day or two.
Symptoms that point to leaking drafts
The pattern varies, but these are the ones I see most often on inherited Sanity + Next.js projects:
- A page appears at its URL before anyone published it, usually with placeholder copy.
- Listing pages (blog index, team, case studies) show items that are not live, or show the same item twice.
- Edits appear on the live site the moment an editor types, with no Publish click.
- The site shows draft content for some visitors and not others, depending on which server instance or cache entry they hit.
- Search engines have indexed a URL for a page the team believes was never released.
The duplicate-item symptom is the giveaway. In Sanity, an edited published document has a second copy with an ID starting with drafts.. If your query sees both, you get two cards.
What it costs to ignore
The obvious risk is embarrassment: unreleased pricing, legal copy that has not been approved, or a staff member's profile before they have started. The quieter risk is SEO. Draft pages that get crawled become indexed URLs, and when they are later unpublished they turn into 404s or soft 404s. I covered the cleanup side in Next.js soft 404 errors on a Sanity site.
There is also a trust cost internally. Once editors learn that saving can put content live, they stop using drafts at all, or they write in a separate Google Doc. That defeats the point of having a CMS.
Cause 1: the client perspective is not set
Sanity's API has the concept of a perspective. The published perspective returns only published documents. The raw perspective returns everything, drafts included. On older API versions the default behaviour can be raw, which is exactly what you do not want on a public site. Many inherited projects pin an old apiVersion from the day the project was created and never touched it.
The fix is to be explicit instead of relying on defaults:
// src/sanity/lib/client.ts
import { createClient } from 'next-sanity'
export const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
apiVersion: '2025-02-19',
useCdn: true,
perspective: 'published',
})This client has no token and no way to see drafts. That is the point. Production pages should only ever use this one.
Cause 2: GROQ queries that assume only published documents exist
Even with a correct perspective, I often find queries written for a raw client, or a second client created in a utility file that nobody remembers. A defensive filter is cheap insurance on listing queries:
*[_type == "post" && defined(slug.current) && !(_id in path("drafts.**"))]
| order(publishedAt desc) {
_id,
title,
"slug": slug.current
}I would not rely on this filter alone, because the perspective is the real control. But it makes the query safe if someone later swaps clients, and it makes the intent obvious to the next developer.
Cause 3: a token on the public client
If the production client has a read token, it can be configured to see drafts, and a lot of tutorials do exactly that. A token should exist only in a separate server-side client used when Draft Mode is on. If the token ever reaches browser code, that is a bigger problem, covered in Sanity API token exposed in a Next.js site.
The clean pattern is one fetch helper that switches on Draft Mode:
// src/sanity/lib/fetch.ts
import { draftMode } from 'next/headers'
import { client } from './client'
export async function sanityFetch<T>(query: string, params: Record<string, unknown> = {}) {
const { isEnabled } = await draftMode()
if (isEnabled) {
return client
.withConfig({
useCdn: false,
token: process.env.SANITY_API_READ_TOKEN,
perspective: 'drafts',
})
.fetch<T>(query, params, { cache: 'no-store' })
}
return client.fetch<T>(query, params, { next: { tags: ['sanity'] } })
}The token is only read on the server, only when the visitor holds a Draft Mode cookie, and that branch opts out of caching.
Cause 4: a cached preview response served to everyone
This one is the hardest to spot because it is intermittent. If a draft fetch ends up in the Next.js data cache or at the CDN, the next anonymous visitor receives it. It happens when preview logic is bolted on with a query-string flag or a custom header rather than real Draft Mode, or when the draft branch forgets to disable caching. The fix is the helper above: never cache the draft branch, and never decide preview by a URL parameter that a CDN might treat as the same key.
If editors also report the opposite problem, publishing without seeing changes, read Sanity editors publishing changes that don't show on the live site, since the two share a caching root.
How to verify the fix
- In the Studio, edit a published page and do not publish. Load the live URL in a private window. It must show the old copy.
- Create a brand-new document and leave it as a draft. Its URL must return a 404 and it must be missing from listings and the sitemap.
- Turn on Draft Mode through your preview route. The draft should now appear, and a second private window should still not see it.
- Search the codebase for every
createClientcall and every use of a token. There should be one public client and one token-bearing path. - Check Google Search Console for URLs that should never have been public, and request removal if needed.
What to ask a developer
- Which perspective does each client in the codebase use, and why?
- Where does the read token live, and can it reach the browser?
- How is Draft Mode implemented, and is the draft branch uncached?
- Do listing queries, sitemap generation, and static params all go through the same published-only client?
- Can they show you the drafts test above passing on staging before it ships?
A vague answer to the first question usually means the project has more than one leak. For a deeper walk-through of preview itself, see Sanity Draft Mode preview with the App Router and Edge Config.
Getting it fixed
This is work I do. A draft-leak review normally fits inside my Performance & Sanity audit, which starts from $800 and covers clients, tokens, caching, and indexing in one pass, with the fixes scoped afterwards.
Frequently asked questions
01Why is unpublished Sanity content showing on my live website?
Usually the production client is fetching with a perspective that includes drafts, or a GROQ query has no filter excluding draft documents. A preview fetch that got cached and served to everyone is the other common cause.
02Does Sanity return draft documents by default?
It depends on the API version and perspective. Older API versions default to the raw perspective, which returns drafts and published documents together. Setting the perspective to published explicitly removes the ambiguity.
03Is it safe to use a read token on a public Next.js site?
Only on the server, and only for draft preview behind Draft Mode. A token used in a public fetch can pull in drafts, and a token shipped to the browser can be extracted by anyone.