Visual Editing

Visual Editing with Next.js App Router

Get started with Sanity Visual Editing in a new or existing Next.js application using App Router.

Following this guide will enable you to:

  • Render overlays in your application, allowing content editors to jump directly from Sanity content to its source in Sanity Studio.
  • Edit your content and see changes reflected in an embedded preview of your application in Sanity’s Presentation tool.
  • Optional: Provide live content updates and seamless switching between draft and published content.

Prerequisites

Next.js application setup

The following steps should be performed in your Next.js application.

Install dependencies

Install the dependencies that will provide your application with data fetching and Visual Editing capabilities.

Set environment variables

Create a .env file in your application’s root directory to provide Sanity specific configuration.

You can use Manage to find your project ID and dataset, and to create a token with Viewer permissions which will be used to fetch preview content.

The URL of your Sanity Studio will depend on where it is hosted or embedded.

Application setup

Configure the Sanity client

Create a Sanity client instance to handle fetching data from Content Lake.

Configuring the stega option enables automatic overlays for basic data types when preview mode is enabled. You can read more about how stega works here.

Draft mode

Draft mode allows authorized content editors to view and interact with draft content.

Create an API endpoint to enable draft mode when viewing your application in Presentation tool.

Create a server action which can be used to disable draft mode. Add a delay to ensure a loading state is shown.

Create a new component for disabling draft mode. We will render this for content authors when viewing draft content in a non-Presentation context.

Enable Visual Editing

The <VisualEditing> component handles rendering overlays, enabling click to edit, and refreshing pages in your application when content changes.

Import it into your root layout, and render it conditionally when draft mode is enabled alongside the <DisableDraftMode> component you created above.

Render a page in preview mode

Add configuration to your client.fetch calls when draft mode is enabled in order to fetch up-to-date preview content with stega encoding.

Studio setup

To setup Presentation tool in your Sanity Studio, import the tool from sanity/presentation, add it to your plugins array, and configure previewUrl, optionally passing an origin, path, and endpoints to enable and disable preview mode.

We similarly recommend using environment variables loaded via a .env file to support development and production environments.

Optional Extras

Live Content API

The Live Content API can be used to receive real time updates in your application when viewing both draft content in contexts like Presentation tool, and published content in your user-facing production application.

Implementing Visual Editing using the Live Content API is recommended for the best experience for both users and content editors.

Update Sanity client

First, update your client configuration. The token can be removed from the base client instance as we pass it as configuration in the next step.

Configure defineLive

Configure defineLive to enable automatic revalidation and refreshing of your fetched content.

The Viewer token can be used as both browserToken and serverToken, as the browserToken is only shared with the browser when draft mode is enabled.

Layout and pages

The <SanityLive> component is responsible for making all sanityFetch calls in your application live, so should always be rendered. It will also enable seamless switching between draft and published content when viewing your application in Presentation tool.

Replace your client.fetch calls with the newly exported sanityFetch function.

Explicitly passing the options parameter based on the draft mode status is no longer necessary as sanityFetch handles setting the correct options internally.

Was this page helpful?