How to set image size in GROQ query for thumbnails?

3 replies
Last updated: May 24, 2023
Hey friends- Is there a way to set the image size in a GROQ query? Looked all over the dos and only see image sizing with the URL builder after the asset is fetched. I have a thumbnail component and would love to only grab a small version. Thanks in advance for any help!
AI Update

You're absolutely right that GROQ itself doesn't have built-in image resizing – GROQ is purely for querying data. However, you can construct the sized image URL directly within your GROQ query by concatenating URL parameters to the asset URL! This way your thumbnail component receives a ready-to-use sized URL.

Here's how:

*[_type == "post"] {
  title,
  "thumbnailUrl": image.asset->url + "?w=150&h=150&fit=crop&auto=format"
}

This returns a complete URL like https://cdn.sanity.io/images/.../image.jpg?w=150&h=150&fit=crop&auto=format that Sanity's CDN will automatically transform when requested.

Common URL parameters for thumbnails:

  • w=150 - set width in pixels
  • h=150 - set height in pixels
  • fit=crop - cropping mode (also clip, fill, min, max)
  • auto=format - automatically serve WebP/AVIF when browser supports it
  • q=80 - quality (0-100, default is 75)

You can also build more complex URLs:

"smallThumb": image.asset->url + "?w=100&h=100&fit=crop&auto=format&q=80",
"mediumThumb": image.asset->url + "?w=300&h=300&fit=crop&auto=format"

Important caveat: While this gives you convenient sized URLs, the actual image transformation happens on Sanity's CDN when the URL is requested, not during the GROQ query execution. So you're still fetching the full asset reference from the API – the bandwidth savings happen at the image delivery level, not the query level.

Alternative approach: If you're working in JavaScript/TypeScript, the @sanity/image-url package provides a cleaner builder API:

import imageUrlBuilder from '@sanity/image-url'

const builder = imageUrlBuilder(client)
const thumbnailUrl = builder.image(image).width(150).height(150).fit('crop').auto('format').url()

Both work great! String concatenation in GROQ is perfect when you want the URL ready to go in your query response, while the builder gives you more flexibility for dynamic sizing logic in your components.

Show original thread
3 replies
You fetch the reference and not the asset, then you can build the url with the wanted size and load the asset
You're only really fetching the image information from groq not a specific size, the url parameters on the image src specify what size image is fetched from Sanity's cdn. (eg.
?w=100
for 100px wide image)
Granted if you wanted to use a url directly from your query result you could do something like this (would need to change based on your schema), I probably wouldn't do this though:


*[_id == "abc123"] {
  "src": image.asset->url + "?w=100"
}
Thank you both for the quick response! I will transform with my url builder- didn't realize that was achieving what I wanted, thought it was just doing a CSS resize

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?