-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow directly embedding sanity studio with <SanityStudio>
component
#541
Comments
Any updates on this? Currently working on this using renderStudio function but getting: I believe it is because vite using a different way to get env vars.
|
I found a way to embed Sanity Studio 😄I followed this tutorial by Sanity, and added some extra code/steps to make it work in Nuxt. This should be a pretty complete tutorial on how to embed Sanity Studio v3 in Nuxt 3: Step 1Add Sanity to your project npm install sanity@latest Step 2Create a new plugin in your import { defineConfig } from 'sanity';
import { deskTool } from 'sanity/desk';
import { schemaTypes } from '../schemas';
export default defineNuxtPlugin((nuxtApp) => {
const config = defineConfig({
plugins: [deskTool()],
name: '<PROJECT-NAME>',
projectId: '<PROJECT-ID>',
basePath: '/admin', // Same as our page
schema: {
types: schemaTypes, // Your Sanity-schemas
},
});
return {
provide: {
sanityConfig: config,
},
};
}); Step 3I assume you I don't have to explain in detail how to define Sanity-schemas, but in my example I created a folder in the root of my project called animal.ts export default {
name: 'animal',
type: 'document',
title: 'Animal',
fields: [
{
name: 'name',
type: 'string',
title: 'Name',
},
],
} index.ts import animal from './animal'
export const schemaTypes = [animal] Step 4In your
Add the following content to the page <template>
<div id="studio" ref="studio"></div>
</template>
<script setup>
import { renderStudio } from 'sanity';
const { $sanityConfig } = useNuxtApp();
const studio = ref();
onMounted(() => {
renderStudio(studio.value, $sanityConfig);
});
</script>
<style>
/* This assumes no margin or padding on #app's parent(s) */
#studio {
height: 100vh;
max-height: 100dvh;
overscroll-behavior: none;
-webkit-font-smoothing: antialiased;
overflow: auto;
}
/* Fix if you're using Tailwind.css */
*:not([data-ui='Popover__arrow']):not([data-ui='Tooltip__arrow']) > svg {
display: inline;
}
</style>
Step 5We also need to add some config to our nuxt.config.ts export default defineNuxtConfig({
routeRules: {
'/admin/**': {
ssr: false,
},
},
vite: {
define: {
// Fix broken process.env for styled-components in Sanity Studio
'process.env': {
REACT_APP_SC_ATTR: 'data-styled',
},
},
},
}); Some explanation
Done! 🎉Sanity Studio should now be running on I have not tested this implementation fully as I just got it working myself, but it seems to be working fine. @danielroe Would it be interesting to add this as a built-in option in the Edit/noteIt seems like you will run in to some issues if you try to customize Sanity Studio with React-components. For instance, if you want to change the logo, you have to create a React-component with JSX. But with my solution it will give the following error:
So maybe the best solution is to keep the Sanity Studio completely outside of the Nuxt rendering. I guess one solution could be to just have an API-route in the Another solution would be to use Sanity CLI and build the studio project to static files, and put them in the Any thoughts on this, @danielroe ? 😇 |
Thank you so much Ola! Great work! |
This is great work! 🙌 I think the objectives are:
I'm up for some different approaches to achieving these objectives, or even exposing multiple options for the end user. |
@OlaAlsaker Do you know how to exclude types coming from the sanity studio? I am getting all these annoying react ts errors. I tried everything i found on google, and one solution working on fullcalendar but not for sanity, |
Not yet, unfortunately! If you need to do custom things with React in Sanity, the best current solution is to have Sanity Studio as it's own project in a custom folder. |
@OlaAlsaker I am thinking in combination with Nuxt. Sanity is enabled with ts and nuxt also. They are in the same repo (seperate folders). |
Hello:) Any updates on this? I am heavily using sanity with Nuxt 3, and to be frank I think this would be such a good feature to implement for various reasons. I would love to help on this, but i dont know where to start. Maybe someone could point me in the right direction? So the main stopper for me is the ability to create custom components in the studio, maybe this could even be done as a Vue component? And that react types flows into Nuxt types to make it useless. Any ideas about a possible approach to this? |
No description provided.
The text was updated successfully, but these errors were encountered: