Preview Mode shows published version instead of draft changes in Next.js

2 replies
Last updated: Sep 3, 2020
Hoping for some help with Preview Mode and Next.js — I have it working, and with draft documents showing up, etc— but when I go to preview draft changes from an already published doc nothing happens and I just get the last published version. Any ideas?
AI Update

Based on your description, it sounds like your preview mode is working for unpublished drafts but not showing draft changes to already published documents. This is a common issue that usually comes down to how your queries are fetching data and which perspective you're using.

Here's what's likely happening: When you have a published document with draft changes, Sanity stores two versions - the published document (e.g., post-123) and the draft version (e.g., drafts.post-123). If your queries aren't properly prioritizing the draft version, you'll only see the published content.

The fix: Make sure you're using the previewDrafts perspective in your draft mode queries. This perspective specifically prioritizes draft documents over published ones when both exist.

In your data fetching code (where you're using @sanity/client or similar), you should have something like:

const client = createClient({
  // ... your config
  useCdn: false, // Important: disable CDN for draft mode
  perspective: 'previewDrafts', // This is the key setting
  token: process.env.SANITY_API_TOKEN, // Required for draft access
})

The perspective: 'previewDrafts' setting tells Sanity to return draft versions when they exist, falling back to published versions when no draft is present.

Common gotchas to check:

  1. Make sure your read token has the proper permissions to access draft documents. Without the right token, the API will fall back to published content even with the correct perspective.

  2. Verify that when draft mode is enabled in Next.js, you're actually switching to use this authenticated client. You might have separate client configurations for production vs. preview that need to be conditional based on draftMode().isEnabled.

  3. Ensure useCdn: false is set when fetching draft content - the CDN only serves published content.

The Sanity documentation on perspectives and Draft Mode with Next.js cover this in detail, particularly around how the previewDrafts perspective works with draft and published documents. The key is that without the right perspective setting, your queries will return both the draft AND published versions (or just published), rather than prioritizing the draft as you want for preview mode.

Show original thread
2 replies
user Y
— i know I've bugged you about this before, but just wondering if you had any thoughts here.
In other words: If doc is unpublished and a draft, preview works — but if doc is published and we change X from Foo → Bar … can't see that. Wonder if you know why that would be? Is this something I need to change in my queries?
NEVERMIND — FIGURED IT OUT! HALLELUJAH

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?