How to get the Image URL for an Image Field inside of the Studio

16 replies
Last updated: May 14, 2020
hey guys! what’s the best way to get the image url for an image field inside of the Studio? The use case is display the document image for SEO preview in the split preview panel.
asset.url
  ?
unfortunately, it’s
undefined
😞
I named the image
image
in my document, and this is what I get:

{
  asset: {
    _ref: 'image-[hash]-[dimension]-png',
    type: 'reference'
  },
  _type: 'image'
}
image.url
is
undefined
and
image.asset.url
is also
undefined
Can you share the document schema?
And when you write
The use case is display the document image for SEO preview in the split preview panel.
, how are you implementing this?
Sure. Here it is.(thanks for helping, btw!)
Here’s the document
Here’s the
SEOPreview
component
Not really sure. what about
seo.image._ref
as weird as that seems?
_ref
is on
seo.image
and it’s not a filename. it looks like this “image-aecfca21db0ccac083c7df9bad5c6b057d29d97a-3072x1716-png”
You can use the
@sanity/image-url
module: https://github.com/sanity-io/image-url
urlFor(seo.image).toString()
thanks
user Z
! follow-up question, how do I get the current dataset and projectId from inside the Studio to initiate the sanity client?
You can get the preconfigured client by importing it through
import client from 'part:@sanity/base/client'
awesome, thanks! that’s exactly what I need
👍

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?