How-to guide

Really easy
real-time

Sanity features in this guide

Live Content API

Live Content API

Sanity Studio

Learn more

Content Lake

What is the Content Lake?

No more refresh button

If you have an existing Sanity project, you may prefer to read the Live Content API Docs for details on how to set it up in your application.

What's covered in this guide

You'll create a new free Sanity project using the Next.js template, publish new documents with Sanity Studio and explore how the Live Content API works.

Create a new project

From the command line, create a new free Sanity project using the Next.js template.

If you don't already have an account with Sanity, you'll be prompted to create one.

Now inside this directory, start the development server.

You should now have two apps running:

These apps are separately configured in the following directories:

├─ /nextjs-app
└─ /studio

The Sanity Studio is configured with code, edits you make to the configuration will render immediately in the development environment.

Content in Sanity Studio is written to a schemaless, cloud-hosted Dataset in what we call the Content Lake.

The Next.js app fetches content using Sanity Client with the GROQ query language.

Enter the Studio

Open http://localhost:3333 and log in to your Sanity Studio. The first tab that will open is the Presentation tool.

The Presentation tool is used to view your Next.js front end (http://localhost:3000) within an Iframe so that you can interact with your content.

💡 To see how Presentation can be used for interactive live preview of draft content, see the guide in this series on Visual Editing.

Publish a new post

Click "+ Create" from the top menu and select "Post."

The post document type currently has a few required fields. You will need to give this new document a

  • Title
  • Slug
  • Cover Image
    • and alternative text for the image

Once the Publish button is enabled, publish the post.

Open the post in Presentation

At the top of the document, you will see a box saying "Used on 2 pages" which you can open to show the locations of the current document in the front end.

Click the first location and you'll see the front end open side-by-side with the document editor. Make a change to the document and press Publish. You'll see the document update again, without a reload!

Sanity Studio in a browser window

It's really real-time!

Open your Next.js application in a separate private browser window, and keep it open while you publish another change to the blog post. You should see the on-page content update briefly, without having to press reload, rebuild the site or purge the cache.

How this works

Sanity Client

Open nextjs-app/sanity/lib/client.ts

Code showing configuration of Sanity Client

The most common way to interact with Sanity projects in JavaScript applications is with Sanity Client. One has been configured in the Next.js app for you.

Within the default configuration are settings for the Project ID and Dataset which you created at the beginning of this guide.

The other settings here in Sanity Client relate to Visual Editing.

You could use Sanity Client to fetch for content, but it would only be done once, at page load time. To go live we pass this client instance to another helper...

Sanity Live

Open nextjs-app/sanity/lib/live.ts

Code showing the defineLive helper from next-sanity

(The screenshot above has been simplified from the version in the template, to remove the token which is used for Visual Editing)

The defineLive helper from next-sanity allows you to turn a static fetch at load-time into a live-updating data source. Your Next.js app listens to published changes in your Dataset and refreshes content without the need to reload. It also handles caching.

Root layout component

Open nextjs-app/app/layout.tsx

Code highlighting a SanityLive component

The <SanityLive /> component comes from the defineLive helper and sets up the Event Source listener for content changes for the result of any fetches performed with sanityFetch.

The combination of <SanityLive /> and sanityFetch is all you need to make a Next.js application "live by default."

Next steps

Some ideas of what you might do next.

  • Commit this repository to your Git provider and deploy to Vercel to share these live previews with content creators
  • Add or edit Sanity Studio schema types. The template comes with a few already created, try adding a new field to post.ts in the /studio directory and see how the Studio updates imediately.
  • Explore GROQ, queries.ts in the /nextjs-app directory contains all the queries that the Next.js app currently makes. You can test queries in the Studio using the Vision tool.
  • Take the Day One with Sanity Studio Course to get a from-the-ground-up understanding of how to create new Sanity projects.
Go deeper

More resources

What's next?

Visual Editing, the Presentation tool and Sanity Studio and are just a few parts of the Sanity Content Operating System. Get a fresh start on a new project or take a guided course for more.

Explore more guides

Sanity TypeGen

Flexible schemas, Type-safe queries

Visual Editing

Fix a typo without a PR

Live Content API

Really easy real-time