Knut Melvær
Knut is a principal developer marketing manager at Sanity.io
Recursive schema for nested navigations
This recipe is for an older version of Sanity Studio (v2), which is deprecated.
Learn how to migrate to the new Studio v3 →// navigationSection.js
export default {
name: 'navigation.section',
type: 'object',
title: 'Section',
fields: [
{
type: 'reference',
name: 'target',
title: 'Target article',
to: [{ type: 'article' }],
// _weak: true // enable if you don't want reference integrity checks
},
{
type: 'string',
name: 'title',
title: 'Title',
},
{
type: 'array',
name: 'links',
title: 'Links',
of: [{ type: 'navigation.link' }],
},
],
}// navigationLink.js
export default {
name: 'navigation.link',
type: 'object',
title: 'Link',
preview: {
select: {
title: 'title',
targetTitle: 'target.title',
},
prepare: ({ title, targetTitle }) => ({
title: title || targetTitle,
}),
},
fields: [
{
type: 'reference',
name: 'target',
title: 'Target article',
to: [{ type: 'article' }],
description: 'No target article turns the item into a subheading.',
// _weak: true // enable if you don't want reference integrity checks
},
{
type: 'string',
name: 'title',
title: 'Title',
description: 'Override title from the target article.',
},
{
type: 'array',
name: 'children',
title: 'Children',
of: [{ type: 'navigation.link' }],
},
],
}// navigation.js
export default {
name: 'navigation',
type: 'document',
title: 'Navigation',
fields: [
{
type: 'string',
name: 'name',
title: 'Name',
},
{
type: 'string',
name: 'title',
title: 'Title',
},
{
type: 'array',
name: 'sections',
title: 'Sections',
of: [{ type: 'navigation.section' }],
},
],
};// getNavigationTree.js
/*
* Only fetches structures 2 levels down.
* You can extend it by repeating the pattern inside this query
*/
import client from './sanityClient'
const navigationTree = client.fetch(`//groq
*[_id == 'mainNavigation'][0] {
...,
sections[]{
...,
target->{title, slug, _id},
links[]{
...,
target->{title, slug, _id},
children[]{
...,
target->{title, slug, _id}
}
}
}
}
`)
export default navigationTreeThis schema for nested navigation allows for a separation of content and presentation. Not only can you connect the same pieces of content to different navigation structures, but you also gain from establishing reference integrity that warns or prevents you from deleting content that's represented in navigation.
Knut is a principal developer marketing manager at Sanity.io
Automatically track when content was first published with a timestamp that sets once and never overwrites, providing reliable publication history for analytics and editorial workflows.
Go to First Published Timestamp FunctionAI-powered automatic tagging for Sanity blog posts that analyzes content to generate 3 relevant tags, maintaining consistency by reusing existing tags from your content library.
Go to Automatically tag blog postsThis can be used for blogs or articles where you want to possibly display next and previous article buttons
Go to Get current post, previous post, and next postHow to automatically generate linked headings with a custom serializer
Go to Anchored Headings for Portable Text