Joint session with Vercel: How to build intelligent storefronts (May 15th)

Sanity v2 Preview Not Working with Next.js in Separate Repositories

6 repliesLast updated: Dec 2, 2025

Hello,
I am using v2 in sanity studio. I am not able to see the preview content in local too. I have sanity studio and next js(frontend) app in two different repositories. I am following docs -


https://www.sanity.io/guides/nextjs-live-preview

https://www.sanity.io/docs/preview-content-on-site Video is attached. Thanks in advance.
AI Update

I can see you're trying to set up preview functionality with Sanity Studio and Next.js in separate repositories, following the nextjs-live-preview guide and the preview-content-on-site docs.

First, let me clarify something important: both guides you're following are for Sanity Studio v3 with the Presentation tool. When you mentioned "using v2 in sanity studio," I want to make sure we're on the same page about your actual Studio version.

Check Your Studio Version

Run this command in your Studio repository to confirm which version you have:

npm list sanity

If you see sanity@2.x.x, you have Studio v2. If you see sanity@3.x.x or higher, you have Studio v3.

Studio v2 does not have the Presentation tool that those guides describe. The Visual Editing workflow with real-time preview, click-to-edit overlays, and the embedded iframe preview requires Studio v3.

If You Have Studio v3

Since you can't see preview content locally, here are the most common issues with separate repository setups:

1. CORS Configuration

With Studio and Next.js on different ports/domains, you need to configure CORS:

2. Draft Mode Not Activating

The Presentation tool needs to activate draft mode in your Next.js app. Check:

In your Studio's sanity.config.ts:

import { presentationTool } from 'sanity/presentation'

export default defineConfig({
  // ... other config
  plugins: [
    presentationTool({
      previewUrl: {
        draftMode: {
          enable: '/api/draft', // This should match your Next.js API route
        },
      },
    }),
  ],
})

In your Next.js app, you need an API route (e.g., /pages/api/draft.ts for Pages Router):

import { validatePreviewUrl } from '@sanity/preview-url-secret'
import type { NextApiRequest, NextApiResponse } from 'next'
import { createClient } from 'next-sanity'

const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
  apiVersion: '2024-01-01',
  useCdn: false,
  token: process.env.SANITY_API_READ_TOKEN!,
})

export default async function handle(req: NextApiRequest, res: NextApiResponse) {
  const { isValid, redirectTo = '/' } = await validatePreviewUrl(client, req.url!)
  
  if (!isValid) {
    return res.status(401).send('Invalid secret')
  }
  
  res.setDraftMode({ enable: true })
  res.writeHead(307, { Location: redirectTo })
  res.end()
}

3. Missing Token

You need a viewer token for stega encoding to work:

4. Client Configuration

Your Sanity client needs to be configured for preview with the previewDrafts perspective and stega encoding:

import { createClient } from '@sanity/client'

export function getClient(previewToken?: string) {
  return createClient({
    projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
    dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
    apiVersion: '2024-01-01',
    useCdn: !previewToken,
    perspective: previewToken ? 'previewDrafts' : 'published',
    stega: {
      enabled: previewToken ? true : false,
      studioUrl: 'http://localhost:3333', // Your Studio URL
    },
    token: previewToken,
  })
}

5. Live Query Setup

You need to use useLiveQuery from next-sanity/preview to see real-time updates:

import { useLiveQuery } from 'next-sanity/preview'

export default function PostPreview({ post, params }) {
  const [data] = useLiveQuery(post, POST_QUERY, params)
  return <Post post={data} />
}

If You Have Studio v2

If you confirmed you have Studio v2, you'll need to upgrade to Studio v3 to use those guides. Follow the Studio v3 migration guide. Studio v2 is in maintenance mode and the modern Visual Editing features simply don't exist in v2.

Debugging Steps

Without seeing the video you mentioned, here's how to diagnose the issue:

The most common issue with separate repositories is the draft mode not activating due to CORS, missing tokens, or incorrect API route configuration.

If you can share specific error messages from your console or clarify your Studio version, I can provide more targeted help!

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