How to Implement Live Preview and Visual Editing in NextJS and Sanity

How to Implement Live Preview and Visual Editing in NextJS and Sanity

Learn how to level up your content editing experience by integrating Sanity’s dynamic Visual Editing and Live Preview with Next.js App Router—transforming how your team builds, previews, and launches content in real time!

TL;DR:

The Problem: Tired of clunky, outdated content editing? The Solution: This guide shows you how to integrate Sanity’s Live Preview and Visual Editing with the Next.js App Router. Draft Mode: Instantly preview unpublished drafts to see exactly how they’ll look—no waiting or guessing needed. Visual Editing: Transform your website into an editable canvas—click, drag, and see changes live as you work. Live Preview Iframe Pane: See your changes come to life in real-time, directly alongside your editing workflow—no context switching required! The Result: It’s a total game-changer for smoother workflows, happier editors, and a stress-free content process.

Welcome to the next level of content editing! Traditionally in Sanity, visual editing has been confined to presentation mode, where you preview your content separately from the editing interface. But what if you could see your changes live, right alongside your work? That's where Live Preview comes in. In this guide, we’re exploring how to integrate Sanity’s Visual Editing with the Next.js App Router and how to enable Live Preview—so you can watch your content update in real time.

Whether you’re setting up a brand-new project or upgrading an existing one, this walkthrough will equip you with the tools you need to create an intuitive, real-time editing experience that brings your content to life. Let’s dive in!

1. Getting Started

Before we jump in, make sure you have the following:

  • A Sanity Project: Ensure your Sanity Studio is hosted or embedded (remember to use route groups if embedding!).
  • A Next.js Application: Using the App Router (if you haven’t set one up yet, check out the Next.js documentation).

2. Next.js Application Setup

Install Dependencies

First things first, you’ll need to install the necessary packages to handle data fetching and visual editing:

npm install @sanity/client next-sanity

This will equip your application with the tools required to interact with Sanity’s Content Lake.

Set Environment Variables

Create a .env file at the root of your project to store your Sanity configuration. Here’s an example:

Loading Code...

These variables will allow your application to fetch and preview content smoothly.

Configure the Sanity Client

Set up a Sanity client instance to manage data fetching:

Loading Code...

The stega option here is crucial—it enables automatic overlays when in preview mode.

3. Enabling Draft Mode

Draft mode is a game-changer for content editors. It allows you to view and interact with draft content before it goes live.

Create an API Endpoint to Enable Draft Mode

Set up an endpoint to activate draft mode:

Loading Code...

Create a Server Action to Disable Draft Mode

Sometimes, you’ll need to switch off the draft mode. Here’s how you can do that:

Loading Code...

Build a Disable Draft Mode Component

Let's install a react toast library called sonner :

npm install sonner

Create a component named DraftModeToast.tsx that will render a toast with a button to disable draft mode for content authors:

Loading Code...

Now that you've successfully enabled Draft Mode, your team can:

  • Instantly preview draft content directly in your Next.js application.
  • Collaborate effectively by viewing unpublished content changes in real-time.
  • Quickly toggle between draft and published states for seamless editing and reviewing.

4. Enabling Visual Editing

Visual Editing is a powerful feature from Sanity that enables your editors to see exactly how content changes affect the website in real-time—directly within the editing context. This means no more switching between admin panels and preview windows. Editors can click on content elements to edit them instantly, seeing their updates appear immediately on-screen.

The main benefits of enabling Visual Editing include:

  • Faster content iteration: Instantly preview and refine content without delays.
  • Intuitive workflow: Edit directly on-page rather than in a separate admin interface.
  • Enhanced accuracy: See the true impact of changes immediately, reducing mistakes.

Integrate Visual Editing into Your Root Layout

Enabling Visual Editing with Sanity and Next.js is straightforward. It involves adding the <VisualEditing> component into your application's root layout and conditionally render it when Draft Mode is active.

Here’s how simple it is to set up:

Loading Code...

Yes, it’s really that simple! Once this is configured, your editors will be able to visually edit content in real time.

Now that you’ve enabled Visual Editing, your team can:

  • Edit content directly on the live page, making updates intuitive and quick.
  • Enjoy instant visual feedback on edits, speeding up content production cycles.
  • Easily identify and refine content visually, reducing errors and improving user experience.
draft mode component

5. Rendering Pages in Preview Mode

Preview Mode in Next.js allows you to fetch and display draft or unpublished content directly within your application, enabling content creators to review their updates exactly as they'll appear live—without having to publish first. It's a game-changer for content accuracy and speed.

Learn more about Preview Mode in the Next.js documentation.

Update Your Data Fetching Logic

Here's how you'll adjust your data fetching logic to take advantage of Preview Mode:

Loading Code...

With Preview Mode enabled, your team can instantly preview and verify content updates, making your editing workflow smoother than ever.

Here's a demonstration of how the live preview functions when preview mode is activated. It updates the data instantly whenever you modify any field in the Sanity Studio.

live preview demo

6. Studio Setup for Live Preview

To achieve an exceptional editing experience, you'll want to enable Live Preview right within your Sanity Studio. Live Preview allows your editors to instantly visualize how changes will look in real-time, directly alongside their editing interface, eliminating context switching and streamlining your workflow.

Learn more about setting up Live Preview in Sanity’s official documentation.

Presentation Resolver API

Take your Live Preview to the next level by programmatically creating shortcuts directly from your document forms to the relevant routes using Sanity’s Presentation Resolver API. This powerful feature allows editors to instantly jump between content and its visual representation—boosting productivity and making content editing effortless.

The Presentation tool enables you to effortlessly create shortcuts that link directly from your documents in Sanity Studio to their visual previews. This is achieved by configuring two essential concepts:

  • Main Document Resolvers
  • Document Locations Resolvers

Let's create a resolve.ts file and implement both main document and document locations resolvers.

Loading Code...

Configure the Presentation Tool

Update your Sanity Studio configuration to include the Presentation tool:

Loading Code...

With this setup, when your editors navigate to /posts/example-post, Sanity Studio will automatically identify and display the matching post document—making visual editing seamless!

visual editing

Live Preview: Customize Document Views with an Iframe Pane

Let's install the sanity-plugin-iframe-pane in where you installed your sanity studio.

npm install --save sanity-plugin-iframe-pane

The simplest way to configure views is by customizing the defaultDocumentNode settings in the structureTool() plugin. This allows you to integrate an Iframe Pane directly into your Studio, providing an immediate preview of your content.
Let's create a separate file called defaultDocumentNode.ts

Loading Code...

Now implement the defaultDocumentNode in structureTool() which will override the default node and show an additional tab for Live Preview

Loading Code...

Now editors can see the live changes on the page or post without leaving the editor tab. This will allow them to see live previews in real-time effortlessly.

live preview

7. Optional Extras: Live Content API

For those who crave the ultimate real-time experience, Sanity’s experimental Live Content API delivers real-time updates for both draft and published content. It allows your application to reflect content changes immediately, creating a dynamic, engaging experience for both editors and end-users.

Learn more by checking out the Live Content API documentation or the official Live Demo video on YouTube!

Update Your Sanity Client for Live Content

Loading Code...

Configure defineLive

Loading Code...

Render Live Updates in Your Layout

Loading Code...

Replace client.fetch with sanityFetch

Finally, update your page fetching logic:

Loading Code...

Conclusion

If you've struggled to integrate Sanity’s Live Preview and Visual Editing features into your Next.js App Router project, this guide has shown you how straightforward it can be. You now have a powerful setup that enables your team to visually edit and preview content seamlessly, in real-time, without context-switching or delays.

For example, your marketing team can now confidently create, edit, and immediately preview landing pages or blog posts, ensuring that what they see is exactly what your visitors will experience—instantly.

Ready to enhance your Sanity & Next.js projects even further?
Reach out to us and let's discuss how we can support your content workflows.

Happy coding and live editing!

Eric Nation profile picture

Eric Nation

|

Co-Founder & Head of Engineering

Eric Nation, co-founder and partner at Operation Nation, spearheads the Product and Web Development division. Boasting over a decade of expertise as a professional software engineer, Eric thrives on crafting immersive web applications and sites. Away from the keyboard, you can find him at the gym, surfing, skateboarding, or on a motorcycle adventure somewhere.