How to show thumbnail/title from deeply nested objects in a schema
Looking at your schema, you're dealing with nested arrays and union types, which makes preview selection tricky. The good news is that Sanity's select in preview configuration supports accessing nested properties, and you can handle the union type logic in the prepare function.
Your intuition is correct - you'll need to select the fields you need using dot notation and then determine the type in prepare. Here's the recommended approach:
// productionContent.js
export default {
name: 'productionContent',
title: 'Content',
type: 'object',
fields: [{
name: 'content',
title: 'Content',
type: 'content',
}],
preview: {
select: {
// Select the first item's type to determine photo vs video
contentType: 'content.0._type',
// Select photo fields
photoImage: 'content.0.image',
photoFilename: 'content.0.image.asset.originalFilename',
// Select video fields
videoTitle: 'content.0.title',
videoImage: 'content.0.image',
},
prepare(selection) {
const { contentType, photoImage, photoFilename, videoTitle, videoImage } = selection;
if (contentType === 'photo') {
return {
title: photoFilename || 'Untitled Photo',
subtitle: 'Photo',
media: photoImage,
}
}
if (contentType === 'video') {
return {
title: videoTitle || 'Untitled Video',
subtitle: 'Video',
media: videoImage,
}
}
return {
title: 'No content',
}
}
}
}The key points:
Use
content.0._typeto determine which union type you're dealing with. Every Sanity object has a_typefield that identifies its schema type.Select all possible fields you might need from both photo and video types. The ones that don't exist for the actual type will just be
undefined.Branch in
preparebased on the_typeto return the appropriate title, subtitle, and media.
This is indeed the standard pattern for handling union types in preview configurations. While it feels verbose, it's the most reliable way to handle polymorphic content in Sanity previews.
Alternative consideration: If your content array typically only has one item (which seems to be the case from your schema structure), you might want to reconsider if you need the array wrapper at all. If productionContent.content is always a single item, you could make it a direct union type instead of an array, which would simplify your preview configuration to just content._type, content.image, etc. without the array indexing.
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.