Easy way to display a Sanity Image asset in Studio preview.

22 replies
Last updated: Nov 12, 2021
Whats the easy way to show a Sanity Image asset -- i keep running into roadblocks.
AI Update

The easiest way to display Sanity images is actually simpler than you might think! Here are two straightforward approaches:

Quick Start: Get the URL directly

The simplest method is to query for the image URL in your GROQ query and use it directly:

*[_type == 'person']{
  name,
  "imageUrl": image.asset->url
}

This gives you a direct URL like https://cdn.sanity.io/images/...jpg that you can use in any <img> tag. You can then add transformation parameters to the URL:

// Auto-format for best browser support
imageUrl + '?auto=format'

// Resize to specific dimensions
imageUrl + '?w=800&h=600'

// Prevent upscaling small images
imageUrl + '?w=800&fit=max'

Better Approach: Use @sanity/image-url

For JavaScript projects, install the @sanity/image-url helper library. It handles crop/hotspot settings automatically and gives you a clean API:

npm install @sanity/image-url

Set up a reusable helper:

// sanityImageUrl.js
import { createImageUrlBuilder } from '@sanity/image-url'
import { client } from './client' // your Sanity client

const builder = createImageUrlBuilder(client)

export function urlFor(source) {
  return builder.image(source)
}

Then use it anywhere:

import { urlFor } from './sanityImageUrl'

function MyComponent({person}) {
  return (
    <img 
      src={urlFor(person.image)
        .width(800)
        .height(600)
        .url()}
      alt={person.name}
    />
  )
}

The beauty of @sanity/image-url is that it automatically respects editor crop and hotspot settings while still returning a normal URL string that works with any image component (including Next.js Image, React Native Image, etc.).

Common Gotchas

  • Always add ?auto=format or use the .auto('format') method to serve optimized formats (WebP, AVIF) based on browser support
  • Use fit=max to prevent small images from being upscaled
  • Pass the whole image object (not just image.asset) to urlFor() so it can access crop/hotspot data

Check out the full documentation on presenting images for more details on responsive images, download links, and performance tips!

Do you mean in terms of showing the asset's data or displaying the image?
More specifically, as a preview in the Studio.
I just want to take a "image" which is a reference (Sanity.imageAsset I believe) and show it.
It seems I have to create a client, then use the
@sanity/image-url
to build the url to show the image?
Acutally, I just realized I select the asset url right in the "select" with nested notation.
In what context do you want show it? Would it be within a document or in a separate preview pane? I guess, to put it more simply, what's the ultimate goal of displaying it?
Just a thumbnail preview on the document list.
...
  preview: {
    select: {
      title: 'title',
      mainImage: 'mainImage.asset.url',
    },
    prepare(selection) {
      return {
        title: selection.title,
        media: <img src={`${selection.mainImage}`} />,
      };
    },
  },
...
That actually works -- but I'm not sure how I can get a smaller scaled and cropped version of "mainImage".
Thats in a document schema
Got it. You should just be able to select the field and display it. Like this:
preview: {
    select: {
      title: 'title',
      media: 'mainImage',
    }
}
So in that case
media
seems to be returning just
{ asset: { _type: "reference", _ref: "..." } }
So in that case
media
seems to be returning just
{ asset: { _type: "reference", _ref: "..." } }
What does your mainImage schema look like?
    {
      title: 'Main Image',
      name: 'mainImage',
      type: 'image',
      options: {
        hotspot: true,
      },
    },
So in that case
media
seems to be returning just
{ asset: { _type: "reference", _ref: "..." } }
That should be setting the icon of the item in the document list to the image in that schema. When you say returning, where is it returning?
Oh...
Wow -- that worked out very nicely haha
I was way over thinking that apparently.
I feel like that was magic I never knew about
It happens! If you're not doing any sort of transformations or calculations, etc., it's surprisingly easy to set up the previews.

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?