
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeHey there! đź‘‹ Great question about creating tree structures in Sanity! This is a common need when building hierarchical content like navigation menus, category systems, or page trees. Let me point you to some helpful resources and explain the key concepts.
There are actually two different aspects to tree structures in Sanity that people often look for:
The Structure Builder is Sanity's API for customizing how content is organized and displayed in your Studio interface. It lets you create nested, hierarchical navigation in your Studio sidebar - think of it like creating a custom folder structure for your editors.
Here's a basic example:
// sanity.config.ts
import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
export default defineConfig({
// ...other config
plugins: [
structureTool({
structure: (S) =>
S.list()
.title('Content')
.items([
S.listItem()
.title('Pages')
.child(
S.list()
.title('Page Types')
.items([
S.documentTypeListItem('landingPage').title('Landing Pages'),
S.documentTypeListItem('blogPost').title('Blog Posts'),
])
),
S.documentTypeListItem('category').title('Categories'),
])
})
]
})Key resources:
For actual content tree structures (like nested categories or page hierarchies), you'll want to create schemas with parent-child references:
// schemas/category.ts
export default {
name: 'category',
type: 'document',
fields: [
{
name: 'title',
type: 'string'
},
{
name: 'parent',
type: 'reference',
to: [{type: 'category'}],
description: 'Leave empty for top-level categories'
}
]
}Then query it with GROQ to build your tree structure in Next.js:
// Get all categories with their parent references
const categories = await client.fetch(`
*[_type == "category"] {
_id,
title,
"parent": parent->_id
}
`)
// Build tree structure in your Next.js component
function buildTree(items, parentId = null) {
return items
.filter(item => item.parent === parentId)
.map(item => ({
...item,
children: buildTree(items, item._id)
}))
}For integrating tree structures with Next.js, you'll want to:
Unfortunately, there isn't a single comprehensive "Sanity + Next.js tree structure tutorial" in the official docs, but combining the Structure Builder guide with standard parent-child reference patterns will get you there.
Hope this helps you get started! If you need more specific guidance on your use case (navigation menu, product categories, page hierarchy, etc.), feel free to share more details and the community can provide more targeted examples.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store