How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Preview Mode works locally but fails with Error 500 in production

6 repliesLast updated: Dec 1, 2025

Hi,I’m getting
Not authenticated - preview not available eval @ next-sanity.esm.js?c596:159
when in Preview Mode (Next JS SSG).

But actually, the live preview DOES work (Studio Pane and external window) – but only on localhost. As soon as I try in production I only ever get an Error 500…

Also, when I try to (unsuccessfully) access the Preview in production and the relevant Cookie(s) is/are set, it in turn leads to a 500 that persists, even when NOT in preview mode.
When I delete these Cookies, “Normal Mode” functions perfectly again…

Anyone experienced sth. similar or has any ideas why it would work just fine locally but not in production?

It’s a public dataset, I’ve checked all the relevant CORS settings and the environment variables are the same (if I didn’t miss anything). I tried hosting the frontend on Netlify and on Vercel – makes no difference.
AI Update

This is a frustrating issue that often comes down to environment-specific differences between local and production. Based on your symptoms (works locally, 500 errors in production, cookie-related persistence), here are the most likely causes:

1. Token Authentication Issues

The "Not authenticated - preview not available" error typically means your preview token isn't being properly authenticated in production. Even though your dataset is public, Draft Mode requires an authenticated token to fetch draft content using the previewDrafts perspective.

Check:

The persistent 500 error when Draft Mode cookies are set suggests the cookies are causing issues with your server-side rendering. Next.js Draft Mode uses cookies (__prerender_bypass or __next-draft-mode depending on version) to track preview state.

Common issues:

Solution: Add an exit Draft Mode route so users can clear problematic cookies:

// app/api/disable-draft/route.ts
import { draftMode } from 'next/headers'

export async function GET() {
  draftMode().disable()
  return new Response('Draft mode disabled')
}

Visit https://your-site.com/api/disable-draft to clear the cookies manually when things break.

If your Studio and Next.js site are on different domains, browsers may block third-party cookies, breaking Draft Mode authentication.

Check:

4. API Route Configuration and Errors

Your Draft Mode enable/disable API routes might be encountering errors in production that don't occur locally.

Verify:

Example Draft Mode enable route:

// app/api/draft/route.ts
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url)
  const secret = searchParams.get('secret')
  const slug = searchParams.get('slug') || '/'

  // Validate secret
  if (secret !== process.env.SANITY_REVALIDATE_SECRET) {
    return new Response('Invalid token', { status: 401 })
  }

  draftMode().enable()
  redirect(slug)
}

5. Environment Variable Mismatch

Double-check these environment variables are set identically in both local and production:

SANITY_API_READ_TOKEN=<your-token-with-read-permissions>
NEXT_PUBLIC_SANITY_PROJECT_ID=<your-project-id>
NEXT_PUBLIC_SANITY_DATASET=<your-dataset>
SANITY_REVALIDATE_SECRET=<your-secret-for-draft-mode-activation>

The SANITY_API_READ_TOKEN especially must be available server-side in production. On Vercel/Netlify, ensure you've set these in the dashboard and redeployed after adding them (adding env vars doesn't auto-redeploy).

Critical Debugging Steps

Most Likely Culprit

In my experience, the most common cause is the SANITY_API_READ_TOKEN not being properly set in production environment variables. Since it works locally, you likely have it in your .env.local file, but it might be:

The second most common issue is the Draft Mode API route itself returning a 500 because it can't access environment variables or is encountering an error when trying to validate the secret. Check your production logs first - they'll give you the exact error message and point you directly to the problem.

Show original thread
6 replies

Was this answer helpful?

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.

Related contributions