Nesting document types and using Sanity with Next.js discussed in Slack thread

21 replies
Last updated: Jan 12, 2023
Hello, Is it possible to nest multiple document types inside one another in the Studio? Like to make a data structure as a tree.
You can’t exactly nest a document inside of a document, but you can effectively do the same thing by using references! This will allow you to create a flexible hierarchy that will be easily query-able. And as Brian pointed out, you can use a custom desk structure to visually display this in your Studio.
Thank you so much, I will try this out shortly.
user M
Hello Miss, Can you tell me, if I want to use Sanity with Next.js, I should install Next.js first then install Sanity in the Next.js's
src
folder?
I typically create a next project then install sanity into a dir titled
/admin
but you can use
/studio
theres other ways to do it but I found this to work very easy
user G
I see, then I understood it right, I wasn't sure of it. THank you for confirming
/root 
   /pages
   /components
   /public
   /admin *this is where I install sanity
^ how my files are usually set up
Great, understood. Thank you
user G
I finally set up the folders in a structure, but I get a
useContext
error when running
next dev
command. I checked for possible problems, like version mismatch. Versions are OK. In the demo next.js pages folder, there are no componenets which explicitly use hooks, so I guess that's not the problem either. Lastly, maybe the problem is that there are two
react
and
react-dom
libraries one for next.js and one for the sanity studio. But these two shouldn't interfere wiht one another. So, what do you suggest? How could I solve this?
I put together this repo a while back for my self to use as a template https://github.com/briannelson95/sanity-next-client-example
Take a look at the
package.json
scripts and in the
next.config.js
is where the routing get handled to use the studio on domain .com/admin
This is for v2 btw but so far with v3 I haven’t seen any major problems around set up
Here is the blog I followed to set this up
https://www.sanity.io/guides/sanity-nextjs-tailwindcss
Sorry if that doesn’t help, I haven’t ran into the useContext error before
Oh, all right, no worries. I am going to look into this in the morning then. Thank you for your time.
No problem, I basically treat the 2 directories as separate project, each has its on package.json and on build, it directs the server to /admin then run
sanity start
and then runs build commands for next
Yep, that is clear to me too, maybe i should delete all npm packages from my laptop and reinstall which are necessary
So I won't have dplicates or something
Yea might be worth a try or at least form the project
Basically, I installed nextjs with
npm create nextjs@latest
then inside the admin folder I installed sanity separately. It's fine, both work seamlessly, but Nextjs is bugging me. Anyway, thank you for replying. Have a good one. 🙂
Hello, finally I managed to do what I wanted. It's super cool. Thanks for your support.

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?