Embedded Sanity Studio in Next.js causing problems? Fixes

By Nayan Kyada · · 7 min read

Part of The Sanity + Next.js Guide

If your embedded Sanity Studio in Next.js is making builds slower, showing your site header on top of the editing UI, or somehow ending up in your public JavaScript, the cause is almost always structural rather than a bug in either tool. The default setup from the getting-started guides works, but it leaves the Studio sharing a layout, a build and a import graph with your marketing site. This post covers the symptoms I see most, what they cost if ignored, and how to separate the two cleanly.

Symptoms of an embedded Studio gone wrong

These are the complaints that usually reach me:

  • The Studio at /studio has your site navbar, cookie banner or chat widget floating over it.
  • Fonts and global CSS bleed in, so Studio buttons and inputs look slightly off.
  • Builds got noticeably longer after the Studio was added, or a schema tweak means redeploying the whole site.
  • Public pages ship more JavaScript than they should, and the bundle analyser shows Sanity Studio packages in a chunk it has no business being in.
  • Visiting /studio returns a redirect loop, a 404, or a login screen from your own auth layer.
  • Google has crawled /studio or it appears in your sitemap.

Each of these has a different cause. Work through them in order.

Cause 1: the Studio inherits your root layout

In the App Router, a route at app/studio/[[...tool]]/page.tsx is wrapped by app/layout.tsx. Whatever you put there, such as header, footer, analytics scripts, Tailwind resets or a font provider, wraps the Studio too. Tailwind's base styles in particular can restyle Studio elements in subtle ways.

The fix is route groups with separate root layouts. Move your public pages into app/(site)/ and the Studio into app/(studio)/, delete the top-level app/layout.tsx, and give each group its own.

// app/(studio)/studio/[[...tool]]/page.tsx
import { NextStudio } from 'next-sanity/studio'
import config from '../../../../sanity.config'
 
export const dynamic = 'force-static'
 
export { metadata, viewport } from 'next-sanity/studio'
 
export default function StudioPage() {
  return <NextStudio config={config} />
}
// app/(studio)/layout.tsx
export default function StudioLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body style={{ margin: 0 }}>{children}</body>
    </html>
  )
}

Note the Studio layout does not import globals.css. That is the point. Your site layout keeps its own html and body with fonts, analytics and styles. Check the relative import path against your own folder depth; the config import is the part people get wrong after moving files.

Cause 2: the Studio config leaks into public bundles

This is the expensive one. sanity.config.ts imports sanity, plugins and every schema type. If any frontend file imports from it, even just to read projectId, the bundler may pull Studio code into a route that real visitors load.

The broken pattern looks harmless:

// lib/sanity.client.ts  (broken)
import { createClient } from 'next-sanity'
import config from '../sanity.config'
 
export const client = createClient({
  projectId: config.projectId,
  dataset: config.dataset,
  apiVersion: '2025-01-01',
  useCdn: true,
})

The fix is a small env module that both sides read from, with nothing Studio-related in it:

// sanity/env.ts
export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!
export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET!
export const apiVersion = '2025-01-01'

Then sanity.config.ts and your client both import from sanity/env.ts, and nothing in the frontend imports sanity.config.ts. Schema type files should be the same: import them only from the config, never from page components. If you need a TypeScript type, use generated types rather than importing a schema definition. I covered the bundle side of this in the Next.js bundle size analysis post; run the analyser before and after and confirm no sanity package appears in public route chunks.

Cause 3: your proxy or auth blocks the Studio

In Next.js 16, the old middleware.ts convention is now proxy.ts. If it has a catch-all matcher for redirects, locale prefixes or auth, it will happily rewrite /studio into /en/studio or bounce it to a login page. Studio also loads assets and makes requests under its base path, so a partial exclusion causes half-working screens.

Exclude the Studio path from the matcher, and make sure basePath in sanity.config.ts is exactly /studio to match the route. Multilingual sites are the most common victims here.

Cause 4: the Studio is coupled to your deploy pipeline

When the Studio lives in the same repo and app, any schema change triggers a full site build. On a large site that can mean editors wait on a long build to get one new field. Studio compilation also adds to build time on its own. If this is your pain, read my write-up on slow Next.js builds on Sanity sites first, because the content fetching is usually the bigger cost.

If the coupling is still a problem, you have two options:

  1. Keep embedding, but split into a monorepo with the Studio as its own app and its own deploy. Editors get a Studio URL on your domain and schema changes ship independently.
  2. Deploy the Studio with Sanity's own hosting using the Sanity CLI and drop it from the Next.js app entirely.

The trade-off: you lose the single-domain convenience, and preview and Presentation setups need their origins configured. For a team with a handful of editors and a small site, I would keep it embedded and just fix causes 1 to 3.

Cause 5: the Studio is visible to search engines

A Studio that is crawlable is not a security hole, since it needs a login, but it is clutter and occasionally shows up in search results. Add a Disallow: /studio line in your robots file, make sure the sitemap generator only lists content from Sanity, and do not link to the Studio from public pages. If a staging copy is also exposed, the staging indexed by Google guide covers the broader fix.

How to verify the fix

  1. Open /studio in a private window. No site header, no analytics requests in the network tab, and Studio styles look native.
  2. Run your bundle analyser and search the public route chunks for Studio packages. There should be none.
  3. Load a public page and check the network tab: no Studio chunks downloaded.
  4. Change a schema field and time how long the deploy takes. Compare to before.
  5. Fetch /robots.txt and confirm the Studio path is disallowed.

What it costs to ignore

A leaked config quietly adds weight to every page visitors load, which shows up in Core Web Vitals. A Studio that looks broken makes editors avoid it, which is a bigger problem than it sounds; see why editors avoid the Studio. And a Studio tangled into your site layout makes every handover harder.

What to ask a developer

  • Does the Studio have its own root layout, separate from the public site?
  • Can you show me the bundle report proving no Studio code reaches public routes?
  • Is the Studio excluded from the proxy matcher and from the sitemap?
  • Can schema changes ship without a full site rebuild, and if not, why not?

This is the kind of cleanup I do as part of a Performance and Sanity audit, which starts from $800 and includes the bundle and Studio structure review.

Frequently asked questions

Does embedding Sanity Studio in Next.js slow down my public pages?

It should not. Studio code lives on its own route and loads only when someone visits it. If public pages get heavier, something in your frontend is importing the Studio config or Studio packages, which is fixable.

Should I embed Sanity Studio or host it separately?

Embedding is convenient for small teams and keeps preview and Studio on one domain. Hosting it separately is better when editors need schema changes without waiting on a full site build, or when the Studio keeps breaking the main app.

Why does my embedded Studio show my site header and fonts?

The Studio route is nested inside your root layout, so it inherits the navigation, global CSS and analytics. Moving the site and Studio into separate route groups, each with its own root layout, stops that.