Embedding Sanity Studio on a subroute in a React application

15 replies
Last updated: Sep 23, 2021
is there a defined method to embedded sanity studio on a subroute of another website, i.e. react application at
/admin
I’ve been struggling to to this well. and we don’t want to organise separate hosting, at a
admin.*
domain.
AI Update

Yes, there's a well-defined method for embedding Sanity Studio on a subroute like /admin in your React application! Here's how to do it:

The Official Approach

Sanity Studio is distributed as a single npm package that you can embed in any React application. The key steps are:

1. Install the Studio package

npm install sanity@latest

2. Configure the basePath

In your Studio configuration, set the basePath to match your desired route:

// StudioRoute.tsx
import { defineConfig, Studio } from "sanity";

const config = defineConfig({
  projectId: "your_project_id",
  dataset: "your_dataset",
  basePath: "/admin" // This is the key setting!
});

export default function StudioRoute() {
  return <Studio config={config} />
}

3. Set up catch-all routing

This is crucial - you need to ensure all sub-routes under /admin redirect to the page where Studio is mounted. Most modern frameworks support this:

  • Next.js: Use [[...index]].tsx or catch-all routes in App Router
  • React Router: Use a splat route like /admin/*
  • Other frameworks: Check their docs for "catch-all" or "rest" routes

4. Add proper styling

The Studio needs to take full viewport dimensions. Add this CSS to the container:

#app {
  height: 100vh;
  max-height: 100dvh;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  overflow: auto;
}

5. Update CORS settings

Don't forget: Add your domain to your project's CORS origins settings with authenticated requests enabled.

Next.js Specific

If you're using Next.js, the next-sanity library makes this even easier and includes additional features like live preview. It's the recommended approach for Next.js projects.

The basePath configuration is exactly what you need to avoid separate hosting at admin.* - it allows Studio to live happily alongside your main application on the same domain!

Show original thread
15 replies
tried that but after loggin in to sanity with a single signon, it sounds back to the /{basepath}/desk which 404s, but if I manually route back to the base path then i’m logged in.would there be a recomendation to handle routing dirrectly to subroutes within sanity like the above?
What does your
sanity.json
look like? It sounds like something might not be configured correctly. Here's an example of one of the starters that uses this method.
i’m going to split it out and do the hosting separately for simplisity and speed up build times. but… with your example sanity.json you linked too, I learnt about that I can inject the projectId with a environment varable which is great. as will we hope to have the same codebase, but a sanity instance per client. 🙂
is there a way to inject those projectID’s at runtime rather than build time, so that we can have the same built assets?? so that we only need the one build pipeline.
?
is there a way to inject those projectID’s at runtime rather than build time, so that we can have the same built assets?? so that we only need the one build pipeline.
thank you for your help btw, really appreciate it
Very happy to help! And, to be honest, I can't remember whether or not you can inject at runtime. I'll have to do some digging to confirm!
I can see the projectId is baked into the build. 😞
so you know if theres a way I can inject a step into the build script to make this be defined by a query param or something? i.e. custom webpack step or sometihng?
I can see the projectId is baked into the build. 😞
so you know if theres a way I can inject a step into the build script to make this be defined by a query param or something? i.e. custom webpack step or sometihng?
i’m just inspecting if I can write a plugin that would acheive the above. but let me know if you do hear of anyone else who’s achieved.
success!!! i’ve worked it out!!!
success!!! i’ve worked it out!!!
by overriding the plugin i was able to inject my projectID from a query param.means Ishould be able to do multi tenency now
🙂
success!!! i’ve worked it out!!!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?