How Can I Use a Dynamic Name or Function to Change the Sanity Desk List Items?
You can absolutely customize what's displayed in the desk list for your NavigationItem by using the preview configuration with the select property to dereference the route and access its slug directly.
Here's how to do it:
{
name: 'navigationItem',
type: 'document',
preview: {
select: {
routeSlug: 'route.slug.current', // Access the slug from the referenced route
hasChildren: 'children'
},
prepare({routeSlug, hasChildren}) {
return {
title: routeSlug || 'No route selected',
subtitle: hasChildren?.length ? `${hasChildren.length} children` : 'No children'
}
}
}
}The key is using dot notation in the select object to traverse into referenced documents. When you write 'route.slug.current', Sanity automatically dereferences the route reference and pulls out the nested slug.current value.
This works for any depth of reference - you can access fields from referenced documents without needing to make custom queries or add duplicate fields to your schema. As explained in the preview configuration documentation, the select object supports this dereferencing pattern natively.
If you want to get even fancier, you could combine multiple pieces of information:
preview: {
select: {
routeSlug: 'route.slug.current',
routeTitle: 'route.title', // If your route has a title field
childCount: 'children'
},
prepare({routeSlug, routeTitle, childCount}) {
return {
title: routeTitle || routeSlug || 'Untitled',
subtitle: `/${routeSlug}${childCount?.length ? ` • ${childCount.length} children` : ''}`
}
}
}This approach keeps your schema clean without adding redundant fields, and the preview will automatically update when the referenced route's slug changes.
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.