Troubleshooting NextJS live preview feature with API route errors and missing response
Happy Friday! š
I'd be happy to help with the Next.js live preview feature! That guide you linked is indeed excellent for setting up Visual Editing with the Pages Router.
What specific part of the preview setup are you having trouble with? Here are some common areas where people need help:
Common issues with Next.js draft mode and live preview:
Draft mode not activating - Make sure your
/api/draftroute is properly validating the preview URL secret and setting the draft mode cookie correctly withres.setDraftMode({ enable: true }).Preview not updating in real-time - You need both:
- The
useLiveQueryhook fromnext-sanity/previewwrapping your queries on the client side - The
VisualEditingcomponent from@sanity/visual-editing/next-pages-routeradded to your_app.tsx - The
LiveQueryProviderwrapping your preview components
- The
Stega encoding not appearing (no clickable overlays) - Ensure your Sanity client is configured with:
perspective: 'previewDrafts'when in draft modestega: { enabled: true, studioUrl: '/studio' }- A valid viewer token passed to the client
CORS errors - Don't forget to add your local development URL (like
http://localhost:3000) to your project's CORS origins in sanity.io/manage.Presentation tool not loading your preview - Check that the
presentationToolplugin in yoursanity.config.tshas the correctpreviewUrl.draftMode.enablepath pointing to your draft API route (typically/api/draft).
The key architecture is:
- Server-side: Fetch with the correct perspective and stega encoding using a dynamically configured client
- Client-side: Use
useLiveQueryto subscribe to content updates - Studio: Presentation tool activates draft mode via your API route and communicates changes in real-time
The guide walks through creating separate "preview" components (like PostsPreview.tsx) that use useLiveQuery to wrap your regular components, which is a clean pattern for managing the preview state.
If you can share what specific error message or unexpected behavior you're seeing, I can give you more targeted help! Common symptoms include:
- "No posts found" in Presentation
- Changes not appearing as you type
- 401/403 errors in the console
- Overlays not appearing on content
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.