Custom GROQ Request in Sanity Preview/Prepare Functions

5 replies
Last updated: Jul 25, 2020
Hi, is it possible to make a custom groq request in the preview / prepare functions? Considering the following code:
export default {
    name: 'singleImage',
    title: 'Single image',
    type: 'object',
    fields: [
        {name: 'image', type: 'image'}
    ],
    preview: {
        select: {
            image: 'image'
        },
        prepare: ({image}) => {
            console.log(image)
            // todo: get image original filename
            return {
                title: 'Single image',
                media: image
            }
        }
    }
}
I'd like to get the original filename of the image field. Accessing it via the select object will only return its
_ref
and
_type
. I would also need to access references field name and more.
AI Update

Yes, you can access the original filename and other asset metadata in your preview configuration! The key is to use dot notation to dereference the image asset in your select object. Instead of just selecting image, you need to select image.asset and then the specific fields you want from the asset document.

Here's how to modify your code to get the original filename:

export default {
    name: 'singleImage',
    title: 'Single image',
    type: 'object',
    fields: [
        {name: 'image', type: 'image'}
    ],
    preview: {
        select: {
            image: 'image',
            originalFilename: 'image.asset->originalFilename',  // Use -> to dereference
            extension: 'image.asset->extension',
            url: 'image.asset->url'
        },
        prepare: ({image, originalFilename, extension, url}) => {
            console.log('Original filename:', originalFilename);
            console.log('Extension:', extension);
            return {
                title: originalFilename || 'Single image',
                subtitle: extension ? `${extension.toUpperCase()} file` : undefined,
                media: image
            }
        }
    }
}

The -> operator is the dereference operator in Sanity's select syntax. When you use image.asset->originalFilename, you're telling Sanity to:

  1. Get the image field
  2. Follow the reference in image.asset
  3. Retrieve the originalFilename field from the referenced asset document

This works because image assets in Sanity are stored as separate documents in the sanity.imageAsset type, which includes fields like:

  • originalFilename - The original name of the uploaded file
  • extension - File extension (jpg, png, etc.)
  • url - The asset URL
  • metadata - Contains dimensions, palette, etc.

You can also access nested metadata like dimensions:

select: {
    image: 'image',
    originalFilename: 'image.asset->originalFilename',
    width: 'image.asset->metadata.dimensions.width',
    height: 'image.asset->metadata.dimensions.height'
}

Important limitation: As mentioned in the preview configuration documentation, the prepare function cannot make custom GROQ queries. All data fetching must happen in the select object using this dot notation and dereference syntax. The prepare function only transforms the data that was already selected.

Show original thread
5 replies
Hi
user R
!
In
select
you can dot into references to get their values (https://www.sanity.io/docs/previews-list-views#preview-using-fields-from-referenced-documents-dcbbdb0ec3aa )
So something like this should to it

select: {
   'filename': 'image.asset.originalFilename'
}
Ok that's great. Thanks
Just a quick issue I went through: when requesting
asset.originalFilename
and
asset
in
select
obj, the media won't be loaded. Requesting
asset.originalFilename
seems to modify the content of the media prop, what could be done here?
export default {
    title: 'Image',
    name: 'imageAlt',
    type: 'image',
    preview: {
        select: {
            title: 'asset.originalFilename',
            media: 'asset'
        }
    }
}
I think you need to use the
prepare
function as well. See the example in the link above.
The issue is exactly the same with
prepare
. We can also see that the media object returned is different when we request
asset.originalFilename

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?