
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeIt sounds like you're having trouble rendering images from Sanity in Next.js. The issue is likely that you're trying to use file.asset._ref directly, but this reference needs to be resolved to get the actual image URL.
Here are the most common solutions:
In your GROQ query, use the -> operator to dereference the asset and get its URL:
*[_type == 'yourType']{
name,
"imageUrl": image.asset->url
}This will give you the full CDN URL that you can use directly in an <img> tag.
The @sanity/image-url library is the best way to handle Sanity images as it automatically respects crop and hotspot settings:
Install it:
npm install @sanity/image-urlSet up a helper function:
// lib/sanityImageUrl.ts
import { createImageUrlBuilder } from '@sanity/image-url'
import { client } from './sanity' // your sanity client
const builder = createImageUrlBuilder(client)
export function urlFor(source) {
return builder.image(source)
}Use it in your component:
import { urlFor } from '@/lib/sanityImageUrl'
function MyComponent({ image }) {
return (
<img
src={urlFor(image).width(800).url()}
alt={image.alt || ''}
/>
)
}Make sure you're passing the entire image object to urlFor(), not just image.asset._ref. The builder needs the full object including crop and hotspot data.
file.asset._ref doesn't workThe _ref field contains something like image-abc123-800x600-jpg which is an internal Sanity reference, not a URL. You need to either:
->url@sanity/image-url to build the URLThe @sanity/image-url approach is recommended because it handles all the complexity for you, including transformations, crops, and hotspots.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store