Real-time live preview not updating in dev environment, CORS error resolved.

8 replies
Last updated: Sep 22, 2022
Hey chat. Just a quick question is real time live preview support to work in dev environments/on localhost? I've been following along with https://www.sanity.io/guides/nextjs-live-preview and finding it doesn't realtime update. Haven't set up deployment yet so haven't tested.
AI Update

Yes, real-time live preview absolutely works in dev environments and on localhost! Based on the guide you're following, here's what you need to know:

It Should Work Locally

The Visual Editing setup with Presentation tool is specifically designed to work in your local development environment. When you visit http://localhost:3000/studio/presentation, it should:

  • Show both draft and published documents
  • Display clickable overlays on content fields
  • Update in real-time as you type in the Studio

Common Issues & Solutions

If it's not updating in real-time, here are the most likely culprits:

1. Missing useLiveQuery Hook

The guide shows you need to wrap your components with the useLiveQuery hook from next-sanity/preview. Make sure you have preview wrapper components like:

// PostsPreview.tsx
import { useLiveQuery } from "next-sanity/preview";

export default function PostsPreview({ posts = [] }) {
  const [data] = useLiveQuery(posts, POSTS_QUERY);
  return <Posts posts={data} />;
}

2. Draft Mode Not Activated

The Presentation tool needs to successfully activate draft mode by visiting your /api/draft route. Check that:

  • Your SANITY_API_READ_TOKEN is set in .env.local
  • The draft API route exists at ./src/pages/api/draft.ts
  • You've configured the Presentation tool with the draft mode enable route in your sanity.config.ts

3. Missing VisualEditing Component

Make sure you have the <VisualEditing /> component from @sanity/visual-editing/next-pages-router in your _app.tsx:

import { VisualEditing } from "@sanity/visual-editing/next-pages-router";

// Should be rendered when draftMode is true
<VisualEditing />

This component contains the useLiveMode hook that actually listens for content changes from the Presentation tool.

4. Stega Encoding Not Enabled

Verify your client configuration in ./sanity/lib/client.ts has stega enabled for preview:

stega: {
  enabled: previewToken ? true : false,
  studioUrl: '/studio',
}

5. PreviewProvider Not Wrapping Your App

Ensure your _app.tsx conditionally wraps your app with the PreviewProvider when draft mode is active. This provider includes the LiveQueryProvider that enables real-time updates.

Deployment Not Required

You do not need to deploy anything for this to work locally. The whole point of this setup is to give content editors a live preview experience during local development and in production.

Debugging Steps

If you're still having issues after checking these:

  1. Restart your dev server after adding environment variables
  2. Check the browser console for errors
  3. Verify the Presentation tool successfully navigates to /api/draft (check Network tab)
  4. Make sure you're editing content inside the Presentation tool's iframe, not in a separate Studio tab

The real-time updates should work perfectly on localhost once all the pieces are connected! The guide you're following is specifically designed for local development first, so there's no reason it shouldn't work before deployment.

It should, yes. Do you have any error or something?
user F
Thanks for the reply User. I'm not getting an error, simply that it isn't updating as expected. If I make an edit to content, I need to reload the preview in order to see the update. My assumption was that 'live' meant it was realtime. Example video attached.
Oh I actually don’t know if that‘s supposed to work within the studio. What if you open the preview in a different tab instead? Same result?
It should update the preview without refreshing that tab. What does your preview code look like? And just to double check, there are no errors or warnings in the console when this happens?
user M
Preview code is based on the tutorial referenced (https://www.sanity.io/guides/nextjs-live-preview ). My repo can be found here .
Thanks for pointing to the error console, I don't know why I didn't think to check. I am getting a CORS error when the preview loads. I haven't had time to think or get into it but I am slightly confused that there is a CORS error given the preview loads.

[Error] Origin <http://localhost:3000> is not allowed by Access-Control-Allow-Origin. Status code: 403
[Error] Fetch API cannot load <https://9zti0fg7.api.sanity.io/v1/users/me> due to access control checks.
[Error] Failed to load resource: Origin <http://localhost:3000> is not allowed by Access-Control-Allow-Origin. Status code: 403 (me, line 0)
You're right! It is strange that it initially loads but then throws the CORS error. Have you added
<http://localhost:3000/>
to your CORS origins in manage?
Thanks
user M
, you're a star. It was a CORS issue which I must have over looked in the guide, it's clearly there. Thanks again.
There's so much going on in that guide it's really easy to miss something! Glad it's working now!

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?