How to create tree structure content in Sanity for Next.js?

10 replies
Last updated: Nov 29, 2025
đź‘‹ Hello, team! I want to learn to create Sanity Tree structure content for my nextjs app, but I cannot find any useful resources to learn it. If possible, please help me find useful resources to learn it
AI Update

Hey 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.

Understanding Tree Structures in Sanity

There are actually two different aspects to tree structures in Sanity that people often look for:

1. Structure Builder - Organizing Your Studio Interface

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:

2. Hierarchical Content Schema - Parent-Child Relationships

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)
    }))
}

Next.js Integration

For integrating tree structures with Next.js, you'll want to:

  1. Design your schema with parent references (as shown above)
  2. Query the data using GROQ in your Next.js pages/components
  3. Build the tree structure client-side or server-side
  4. Render recursively using React components

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.

Additional Tips

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.

Show original thread
10 replies

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?