Custom Image Preview Not Showing in Sanity Document List

8 replies
Last updated: Oct 11, 2021
Hello! I'm trying to add a custom image as preview for my document by following this guide: https://www.sanity.io/docs/previews-list-views
My schema looks like:

// language.jsx

import React from "react";

const schema = {
  name: "language",
  type: "document",
  title: "Language",
  fields: [
    {
      name: "title",
      type: "string",
      title: "Title",
      description: "For example English (UK), Norwegian"
    },
    {
      name: "localeTitle",
      type: "string",
      title: "Locale title",
      description: "For example Norsk, Svenska"
    },
    {
      name: "localeId",
      type: "string",
      title: "Locale ID",
      description: "For example en-US"
    }
  ],
  preview: {
    select: {
      title: "title",
      locale: "localeId"
    },
    prepare({ title, locale }) {
      return {
        title: title,
        subtitle: locale,
        media: <img src="<https://purecatamphetamine.github.io/country-flag-icons/3x2/US.svg>" alt={`${title}`} />
      };
    }
  }
};

export default schema;
This only prints title and subtitle. No error messages in the console and I can see no traces of any media/image when inspecting.
I just updated to v2.21.0. Could it be a bug in this version?
AI Update

Your code looks correct! The official Sanity documentation on List Previews shows that you can indeed return JSX directly in the media property from the prepare() function, which is exactly what you're doing.

However, there are a few things that might be preventing your image from showing:

1. Try adding explicit dimensions to your image

The preview thumbnails have specific size constraints. Try adding width/height styles:

preview: {
  select: {
    title: "title",
    locale: "localeId"
  },
  prepare({ title, locale }) {
    return {
      title: title,
      subtitle: locale,
      media: (
        <img 
          src="https://purecatamphetamine.github.io/country-flag-icons/3x2/US.svg" 
          alt={title}
          style={{ width: '100%', height: '100%', objectFit: 'cover' }}
        />
      )
    };
  }
}

2. Check your Studio version compatibility

Since you mentioned you're on v2.21.0, make sure your React version is compatible. Custom preview media with JSX should work, but there might be version-specific quirks.

3. Try wrapping in a container

Sometimes the preview system needs a proper container:

media: (
  <div style={{ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
    <img 
      src="https://purecatamphetamine.github.io/country-flag-icons/3x2/US.svg" 
      alt={title}
      style={{ maxWidth: '100%', maxHeight: '100%', objectFit: 'contain' }}
    />
  </div>
)

4. Verify the image URL is accessible

Make sure the external SVG URL isn't being blocked by CORS or CSP policies. You can check your browser's console for any network errors.

5. Consider using an emoji as a simpler alternative

While you work out the image issue, you could use an emoji flag as shown in the Sanity documentation example:

media: <span style={{ fontSize: '1.5rem' }}>🇺🇸</span>

This isn't a bug in v2.21.0 — the preview system should support your approach. The issue is likely related to styling/sizing or how the external image is being loaded. Try the styling suggestions above and check your browser console for any errors that might give more clues.

Show original thread
8 replies
Follow up: This is working in v2.20.0.
Hi Jørgen. I tried this on 2.21.0 and 2.21.2 and it worked for me in both cases. Is this your exact code or was anything added/removed? I know the React import is important for
media
to use JSX.
Hi
user A
! Thanks for testing. Yes, this is my exact code. I saw that the 2.21 added a lot changes and migrated the studio to use
@sanity/ui
. Could it be that I need
@sanity/ui
npm packages installed for this to work?
No, you won’t need to install Sanity UI.
If you inspect the thumbnail, is there any indication that the studio tried to insert the image?
Can you see any clues on the network tab?
If you inspect the thumbnail, is there any indication that the studio tried to insert the image?
Can you see any clues on the network tab?
Hi
user A
! Thank you for helping out. Not quite sure what happened here, but when I first added my media code and confirmed it worked on v2.20 and then upgraded to v2.21.2 everything worked as expected 🙂
Great! Thanks for following up.

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?