🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Adding custom image preview for a document in Sanity

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?
Oct 8, 2021, 7:21 AM
Follow up: This is working in v2.20.0.
Oct 8, 2021, 10:21 AM
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.
Oct 8, 2021, 3:47 PM
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?
Oct 8, 2021, 4:27 PM
No, you won’t need to install Sanity UI.
Oct 8, 2021, 4:31 PM
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?
Oct 8, 2021, 4:55 PM
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?
Oct 8, 2021, 4:55 PM
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 🙂
Oct 11, 2021, 1:15 PM
Great! Thanks for following up.
Oct 11, 2021, 10:23 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?