Embedding Sanity into an Astro admin page and importing schemas dynamically.
13 replies
Last updated: Feb 2, 2023
N
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
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>
Jan 30, 2023, 9:32 AM
O
Have you specified a
basePathsomewhere in your config? I recognise this error from when I want testing out
basePath.
Jan 30, 2023, 1:16 PM
N
hi
user M
, no I think I havent specify the basePath. Where should I do it? 😇Feb 1, 2023, 9:38 AM
N
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') }, })
Feb 1, 2023, 9:40 AM
O
You don’t happen to have a
sanity.cli.jsfile? If you do, could you send it?
Feb 1, 2023, 11:23 AM
O
Oh, I see. You have the
renderStudioin an file called
admin.astro. So, you need to actually add the
basePathproperty to your Sanity-config with the value
/admin. Example:
"project": { "name": "Movies", "basePath": "/studio" },
Feb 1, 2023, 11:25 AM
O
If you experience any issues regarding the
basePath, make sure you have the latest version of
sanityand
@sanity/cli, since there was a bug that was fixed in version 3.2.4.
Feb 1, 2023, 11:28 AM
N
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?Feb 2, 2023, 12:26 PM
O
You could just import the files somehow. In your Sanity config, under
https://www.sanity.io/docs/configuration#4ce1bbe36d22
schema->
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
schemaTypesfrom another file like this:
import {schemaTypes} from './schema'
https://www.sanity.io/docs/configuration#4ce1bbe36d22
Feb 2, 2023, 12:29 PM
N
user H
hereFeb 2, 2023, 1:45 PM
A
hey
./schemas ?Is there a full route for this?
user M
from where you could get this -> ./schemas ?Is there a full route for this?
import {schemaTypes} from './schemas'
Feb 2, 2023, 1:47 PM
O
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. .
Everything is told in detail here:
https://www.sanity.io/docs/create-a-schema-and-configure-sanity-studio
<root>/schemas), and then in the same folder you create a
index.js / index.tsfile 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
Feb 2, 2023, 10:34 PM
O
Also found this Astro + Sanity starter that maybe will help you out:
https://github.com/littlesticks/astro-sanity-minimal-starter
https://github.com/littlesticks/astro-sanity-minimal-starter
Feb 2, 2023, 10:36 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.