Use Sanity with Next.js

Embedding Sanity Studio in Next.js

Mount Sanity Studio as a route in your Next.js application using NextStudio. Covers auto and manual installation, App Router catch-all route, metadata, and route separation.

Sanity Studio can run as a route inside your Next.js application, giving you a single deployment for both your frontend and content editing interface. Mounting the Studio at a route like /studio means shared authentication context, one build pipeline, and the ability to dynamically configure the Studio based on environment or user context.

Consider your project size

Prerequisites

  • An existing Next.js 14+ project using App Router. If you're starting from scratch, follow the create-next-app setup first.
  • Node.js 18 or later.
  • A Sanity account and project. If you don't have one, the automatic installation flow creates one for you.
  • next-sanity installed in your project.

Automatic installation (recommended)

The fastest way to embed the Studio:

Run this inside your existing Next.js project. The CLI detects your framework and prompts you to:

  • Create or connect a Sanity project.
  • Choose a Studio route (default: /studio).
  • Scaffold configuration files (sanity.config.ts, sanity.cli.ts, and the route file).

If you already have a Sanity project, the CLI connects to it. If not, it creates one.

Manual installation

Install peer dependencies

sanity and styled-components are peer dependencies required for the Studio. Most package managers (npm v7+, pnpm v8+) install them automatically when you install next-sanity.

If you use yarn v1, run the following command:

Create sanity.config.ts

Create a Studio configuration file at the root of your Next.js project:

A few things to note about this configuration:

  • 'use client' is required. Sanity Studio is a client-side React application.
  • basePath must match the route where you mount the Studio (see next section).
  • schema.types is where you define your content model. Start empty and add types as you build.

Create sanity.cli.ts (optional)

This enables npx sanity commands from within your Next.js project:

With this in place, you can run commands like npx sanity cors add or npx sanity schema extract directly from your project.

Create the Studio route

Create an App Router catch-all route for the Studio:

How this works:

  • [[...tool]] is an optional catch-all segment that captures all Studio sub-routes, including structure, vision, and media.
  • dynamic = 'force-static' tells Next.js to statically render this route. The Studio shell (HTML, CSS, JavaScript) is the same for all users. Only the data inside is dynamic, loaded client-side after the shell renders. Static rendering gives the fastest initial load.
  • metadata and viewport exports from next-sanity/studio set mobile-friendly viewport settings and prevent search engines from indexing the Studio route.
  • NextStudio wraps the Sanity Studio component in a Next.js-friendly layout with loading states and viewport handling.

Customizing metadata

The default metadata exports handle the common case. To customize:

Spread the defaults and override specific fields. The defaults include robots: 'noindex' and mobile viewport settings that you'll want to keep.

Advanced: StudioProvider and StudioLayout

If you need to inject custom navigation, wrap the Studio in additional context providers, or access Studio hooks outside of plugins, you can use StudioProvider and StudioLayout as children of NextStudio:

How this works: NextStudio handles Next.js-specific layout concerns (viewport scaling, loading state). When you pass children, it delegates rendering to them instead of its default StudioProvider/StudioLayout. This lets you inject custom components between the provider and layout while keeping the Next.js wrapper benefits.

Most applications don't need this level of control. Use the standard <NextStudio config={config} /> pattern from the section above unless you need to:

  • Inject custom navigation or toolbar components
  • Access Studio React hooks outside of plugins
  • Wrap the Studio in additional context providers

Pages Router projects

Even if the rest of your application uses Pages Router, embed the Studio on an App Router route. Next.js supports both routers in the same application. The Studio is a client-side React app that works best with App Router's layout system, and the NextStudio component is designed for App Router.

Your file structure would look like:

src/
  app/
    studio/
      [[...tool]]/
        page.tsx        # Studio (App Router)
  pages/
    index.tsx           # Your site (Pages Router)
    posts/
      [slug].tsx

Route separation with SanityLive

If you're using <SanityLive /> and <VisualEditing /> in your root layout, make sure they don't render on the Studio route. These components are for your content pages, not the Studio itself.

Use route groups to separate layouts:

src/app/
  (app)/                    # content routes
    layout.tsx              # includes <SanityLive /> and <VisualEditing />
    page.tsx
    posts/
      [slug]/
        page.tsx
  (studio)/                 # Studio route
    studio/
      [[...tool]]/
        page.tsx            # no SanityLive, no VisualEditing

The (app) and (studio) groups create separate layout trees. The Studio route gets its own layout without the Live Content and Visual Editing components.

Troubleshooting

Studio shows a blank page or hydration error

Make sure sanity.config.ts includes 'use client' at the top. Without this directive, Next.js tries to render the Studio configuration on the server, which fails because the Studio is a client-side application.

Studio loads but the URL doesn't match

The basePath in sanity.config.ts must match the actual route where the Studio is mounted. If your Studio route is /admin instead of /studio, update basePath to '/admin'.

Peer dependency errors during installation

If your package manager reports missing peer dependencies for sanity or styled-components, install them explicitly. This is common with yarn v1, which does not auto-install peer dependencies. Run npx install-peerdeps next-sanity or install each package manually.

Next steps

Was this page helpful?