Visual Editing

Visual Editing with React Router/Remix

Get started with Sanity Visual Editing in a new or existing React Router (Remix) application.

Following this guide will enable you to:

  • Edit your content in drafts or releases and see changes reflected in an embedded preview of your react router application in Sanity Studio's Presentation tool.
  • Optional: Render overlays in your application, allowing content editors to jump directly from Sanity content to its source in Sanity Studio.

Prerequisites

  • A Sanity project with a hosted or embedded Studio. Read more about hosting here.
  • A React Router application. This guide uses code from the Displaying content section of the quick start as a starting point. This guide uses React Router 7.9+.

React Router application setup

The following steps should be performed in your React Router application.

Install dependencies

Install the dependencies that will provide your application with data fetching and Visual Editing capabilities. You may already have some of them if you followed the quick start.

Add 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. When working with a locally-running studio, you'll want to set the public URL to http://localhost:3333.

# .env

# Public
PUBLIC_SANITY_PROJECT_ID="YOUR_PROJECT_ID"
PUBLIC_SANITY_DATASET="YOUR_DATASET"
PUBLIC_SANITY_STUDIO_URL="https://YOUR_PROJECT.sanity.studio"
# Private
SANITY_API_READ_TOKEN="YOUR_VIEWER_TOKEN"

Application setup

Configure the Sanity client

Create a Sanity client instance (or edit your existing one) 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.

Add preview mode logic

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

Create a preview helper file, named session.ts in the app/sanity directory, to manage preview sessions and return context about the current preview state. This helper exposes getters and setters to store preview and perspective context as a cookie.

Vite impementations

Create an API endpoint to enable preview mode when viewing your application in Presentation tool. This performs some checks and commits some data into a cookie which enables preview mode for other parts of your app.

Similarly, create an API endpoint to disable draft mode. You may want to adjust how you handle redirects to better-match your UI.

Next, add these routes as new entries in your application’s routes file. Don't forget your other routes.

Next, create a new component with a link to the disable endpoint. We add conditional logic to only render this for content authors when viewing draft content in a non-Presentation context. If they're inside Studio, they can use Presentation's built-in 'Edit' toggle.

Finally, update the root to use our session logic and render the DisablePreviewMode component when preview mode is enabled.

Edit the root.tsx file to include the following:

Set up loaders

Loaders vastly improve the server/client handoff experience compared to using Sanity client on it's own. First create a loader, then update your data fetching to use it.

Create a new loader.server.ts file alongside your other Sanity files.

Next, update your routes to use the loader and the getPreviewData helper we created earlier.

We'll start with a home route and render a list of posts.

Repeat this for your other routes. We'll add one more in this example to render the individual posts. This example also includes some image rendering from the quick start, which you can remove if you aren't using it.

If you're following along, your routes file should have at least these routes.

At this point you can move to configuring your studio.

Studio setup

To setup Presentation tool in your Sanity Studio, import the tool from sanity/presentation, add it to your plugins array, and set previewUrl to the base URL of your application. You should also configure the location resolvers to improve the experience.

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

Now if you visit a document in Studio, it will show a "used in" widget at the top of each document. Use this to open the document in Presentation.

With both your app and Studio set up, you should now be able to test Presentation. Run both the app and studio, then visit Presentation in Studio to test the functionality. Learn more about configuring the presentation tool.

Additional features

Optional: Visual Editing overlays

You can include a React Router-specific visual editing component that enables hover overlays and outlines that make the content editor experience more enjoyable. It adds overlays, click to edit buttons, and communicates with Presentation.

First, install the visual editing package.

Next, in your React Router app, create a new SanityVisualEditing component that will include the DisablePreviewMode component.

Then, update the imports and App function in root.tsx to use this new component instead.

Was this page helpful?