Show document image as thumbnail in list preview using media

7 replies
Last updated: Sep 8, 2020
Hello! I have one document and in the list items, i want to show the image of that document like a thumbnail, i am trying the "preview" with "media" but im getting the default folder icon. I am missing something?
AI Update

Based on your question, it sounds like you're correctly using the preview configuration but the image isn't showing up. Let me help you troubleshoot this common issue.

The most likely problem is that you're referencing the image field directly, but you need to reference the asset within the image field. Here's the fix:

Instead of this:

preview: {
  select: {
    title: 'title',
    media: 'image'  // This might not work
  }
}

Try this:

preview: {
  select: {
    title: 'title',
    media: 'image.asset'  // Reference the asset specifically
  }
}

For image fields in Sanity, the actual image data is stored in a nested asset property, so you need to access it using dot notation as explained in the preview configuration documentation.

If you're working with an array of images and want to show the first one as the thumbnail, you can use array indexing:

preview: {
  select: {
    title: 'title',
    media: 'images.0.asset'  // Gets the first image's asset from an array
  }
}

You can also add a fallback icon in case the image isn't available using the prepare function:

import { ImageIcon } from "@sanity/icons";

preview: {
  select: {
    title: 'title',
    media: 'image.asset'
  },
  prepare({title, media}) {
    return {
      title: title,
      media: media ?? ImageIcon  // Fallback to an icon if no image
    }
  }
}

The folder icon you're seeing is the default fallback when Sanity can't find a valid image reference. Using image.asset instead of just image should resolve this and display your actual thumbnail in the document list. You can see more examples of this pattern in the guide on creating richer array item previews.

Show original thread
7 replies
Hey! Could you share the code for the image field and the preview? 🙂
Yes of course, i hope it helps, im missing something 🙂
export default {
    
name: 'publisher',
    
title: 'Publisher',
    
type: 'document',
    
fields: [
        
{
            
name: 'name',
            
title: 'Name',
            
type: 'string'
        
},
        
{
            
title: 'Logo',
            
name: 'logo',
            
type: 'file'
        
}
    
],
    
preview: {
        
select: {
            
title: 'name',
            
media: 'logo.asset.url'
        
}
    
}

};
Also, seems like the image preview on the Logo field itself is not working correctly either, which might cause the issue above even if the preview was set up correctly. Could it be an issue with the file itself perhaps? Have you tried uploading a different image file to compare?
I see -
logo
is of the
file
type rather than of the
image
type. If you change it to
type: 'image'
that should fix it!
Wow it worked! sorry so dumb question
No such thing! Glad it’s working! 🙂
For anyone comming here, as
user M
say, change type to image and in the preview media: 'logo' or the name of the image you want to show as an image

fields: [
        
{
            
name: 'name',
            
title: 'Name',
            
type: 'string'
        
},
        
{
            
title: 'Logo',
            
name: 'logo',
            
type: 'image' // type image
        
}
    
],
    
preview: {
        
select: {
            
title: 'name',
            
media: 'logo' // the name of the image you want to show
        
}
    
}

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?