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.
May 14, 2020, 4:10 AM
asset.url
  ?
May 14, 2020, 4:27 AM
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'
}
May 14, 2020, 4:30 AM
image.url
is
undefined
and
image.asset.url
is also
undefined
May 14, 2020, 4:30 AM
Can you share the document schema?
May 14, 2020, 4:33 AM
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?
May 14, 2020, 4:34 AM
Sure. Here it is.(thanks for helping, btw!)
May 14, 2020, 4:36 AM
Here’s the document
May 14, 2020, 4:38 AM
Here’s the
SEOPreview
component
May 14, 2020, 4:40 AM
Not really sure. what about
seo.image._ref
as weird as that seems?
May 14, 2020, 4:49 AM
_ref
is on
seo.image
and it’s not a filename. it looks like this “image-aecfca21db0ccac083c7df9bad5c6b057d29d97a-3072x1716-png”
May 14, 2020, 4:51 AM
You can use the
@sanity/image-url
module: https://github.com/sanity-io/image-url
May 14, 2020, 5:14 AM
urlFor(seo.image).toString()
May 14, 2020, 5:15 AM
thanks
user Z
! follow-up question, how do I get the current dataset and projectId from inside the Studio to initiate the sanity client?
May 14, 2020, 5:15 AM
You can get the preconfigured client by importing it through
import client from 'part:@sanity/base/client'
May 14, 2020, 5:16 AM
awesome, thanks! that’s exactly what I need
May 14, 2020, 5:16 AM
👍
May 14, 2020, 5:16 AM

Sanity.io – build remarkable experiences at scale

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