How to embed Sanity Studio in an Astro admin page?

13 replies
Last updated: Feb 2, 2023
Hi! I am trying toembed my santiy into an admin page in Astro. Any ideas how can be done? I have tried this one, but it is not working. Something that maybe I am missing?
Thanks in advance


page/admin.astro
-----
<div id="app">

</div>
<script type="module">
  import { defineConfig, renderStudio } from "<https://esm.sh/sanity?alias=lodash:lodash-es>"
  import { deskTool } from "<https://esm.sh/sanity/desk?alias=lodash:lodash-es>"
        
  const config = defineConfig({
    plugins: [deskTool()],
    name: "Project name",
    projectId: "XXXXX",
    dataset: "production",
    schema: {
      types: [
        {
          type: "document",
          name: "page",
          title: "Page",
          fields: [
            {
              type: "string",
              name: "title",
              title: "Title"
            }
          ]
        }
      ]
    }
  });

  // This assumes that there is a <div id="app"></div> node in the HTML structure where this code is executed.
  renderStudio(document.getElementById("app"), config);
</script>
AI Update

I can see you're trying to embed Sanity Studio directly into an Astro page using renderStudio, but there are a few important things you should know about this approach.

The Main Issue: renderStudio via CDN is Deprecated

The renderStudio function you're using from esm.sh is part of an older, deprecated approach to embedding Studio. While it technically exists in the Sanity codebase, it's not officially supported or documented for modern implementations, especially not for framework integrations like Astro. Loading Sanity Studio via CDN imports like this creates several problems:

  1. Dependency conflicts - The lodash aliasing and module resolution don't work reliably
  2. Build incompatibility - Astro's build process doesn't handle this type of dynamic Studio initialization
  3. Routing issues - Studio expects to control routing, which conflicts with Astro's router
  4. No official support - This pattern isn't documented or maintained

According to the Astro quickstart documentation, the official and recommended pattern is to deploy your Studio separately from your Astro application. Here's how:

Step 1: Create Your Studio

If you haven't already, create a Sanity Studio project:

npm create sanity@latest -- --dataset production --template clean --typescript --output-path studio

Step 2: Configure Your Studio

Your sanity.config.ts (in the Studio directory) should look like:

import { defineConfig } from 'sanity'
import { deskTool } from 'sanity/desk'

export default defineConfig({
  name: 'project-name',
  projectId: 'your-project-id',
  dataset: 'production',
  plugins: [deskTool()],
  schema: {
    types: [
      // your schema types here
    ]
  }
})

Step 3: Deploy Your Studio

Deploy the Studio to get a hosted URL:

cd studio
npm run deploy

This gives you a yourproject.sanity.studio URL where your editors can manage content. This is covered in the deployment documentation.

Step 4: Connect Your Astro Site

In your Astro project, install the Sanity integration to query content (not embed Studio):

npx astro add @sanity/astro -y

Configure it in astro.config.mjs:

import sanity from "@sanity/astro";

export default defineConfig({
  integrations: [
    sanity({
      projectId: "your-project-id",
      dataset: "production",
      useCdn: false,
    }),
  ],
});

Now your Astro site can query content from Sanity, while editors use the separate Studio URL.

Alternative: Self-Host the Studio

If you don't want to use sanity.studio hosting, you can deploy your Studio folder to any hosting provider (Vercel, Netlify, etc.) as a separate application. The Studio is just a React app that can be built and deployed independently.

Why Not Embed in Astro?

While the quickstart documentation mentions that Studio can be embedded as a React component at a route, this approach has significant limitations in Astro:

  • Astro's partial hydration model doesn't work well with Studio's routing
  • You'll encounter 404 errors and navigation issues (as noted in community discussions)
  • It's not the documented or supported pattern for Astro specifically

The separate deployment approach is simpler, more reliable, and what most production Astro + Sanity sites use. Your Astro site focuses on displaying content, while Studio remains a dedicated content management interface.

Show original thread
13 replies
Have you specified a
basePath
somewhere in your config? I recognise this error from when I want testing out
basePath
.
☝️ probably something along those lines. Can you share your Studio config?
hi
user M
, no I think I havent specify the basePath. Where should I do it? 😇
Hi
user M
this is my sanity.config.js:
import {defineConfig} from 'sanity'
import {deskTool} from 'sanity/desk'
import {visionTool} from '@sanity/vision'
import {schemaTypes} from './schemas'
import {structure} from './sidebar-structure'
import {media} from 'sanity-plugin-media'
import {BrandLogo} from './plugins/BrandLogo'

export default defineConfig({
  name: 'default',
  title: 'AARP',

  projectId: 'XXXXXX',
  dataset: 'production',

  plugins: [
    deskTool({
      structure,
    }),
    media(),
    visionTool(),
  ],

  schema: {
    types: schemaTypes,
  },

  studio: {
    components: {
      logo: BrandLogo
    }
  } ,

  tools: (prev) => {
    // :point_down: Uses environment variables set by Vite in development mode
    if (import.meta.env.DEV) {
      return prev
    }
    return prev.filter((tool) => tool.name !== 'vision')
  },

})
You don’t happen to have a
sanity.cli.js
file? If you do, could you send it?
Oh, I see. You have the
renderStudio
in an file called
admin.astro
. So, you need to actually add the
basePath
property to your Sanity-config with the value
/admin
. Example:

  "project": {
    "name": "Movies",
    "basePath": "/studio"
  },
If you experience any issues regarding the
basePath
, make sure you have the latest version of
sanity
and
@sanity/cli
, since there was a bug that was fixed in version 3.2.4.
hi
user M
, it works with the basePath, but is there a way to have automatically get all sanity schemas without copying the copde again in the admin.astro page? I mean, i have a repositoty for the backend, and other for the frontend. I will need to get all backend information on the frontend admin.astro without having to copy all the code. Do you know what I mean?
You could just import the files somehow. In your Sanity config, under
schema
-&gt;
types
, is where you should put your schemas. But this does not have to be a hardcoded array, this can be files imported from anywhere. Look at the documentation here, and you will see that they import the
schemaTypes
from another file like this:

import {schemaTypes}  from './schema'

https://www.sanity.io/docs/configuration#4ce1bbe36d22
user H
here
hey
user M
from where you could get this -&gt;
./schemas ?Is there a full route for this?


import {schemaTypes} from './schemas'

I really recommend you reading the documentation, as all the questions you ask are answered there 😄 But essentially you create your schemas in a folder (i.e.
<root>/schemas
), and then in the same folder you create a
index.js / index.ts
file that exports the schema-types. And then you can just do
import {schemaTypes} from '<rootDir>/schemas'
.
Everything is told in detail here:

https://www.sanity.io/docs/create-a-schema-and-configure-sanity-studio
Also found this Astro + Sanity starter that maybe will help you out:
https://github.com/littlesticks/astro-sanity-minimal-starter

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?