How to use original filename instead of hash in Sanity image URLs?
For example, I want to have a url like
<https://cdn.sanity.io/images/1234567/production/[here-the-original-filename-instead-of-the-hash]-1766x1065.png?q=75&fit=clip&auto=format><https://cdn.sanity.io/images/1234567/production/754c8d64997b668642bd2500f833722c6293cf8b-1766x1065.png?q=75&fit=clip&auto=format>I only found the
&dlparam in the docs but this doesn’t solve the problem as it’s only for downloading the file and the url would still contains the hash instead of the file name.
My goal is to have a better image URL to follow
Google Images best practices for SEO
• Create good URL structure for your images: Google uses the URL path as well as the file name to help it understand your images. Consider organizing your image content so that URLs are constructed logically.
Yes. Add a "vanity filename" to the URL path after the hashed asset filename. The hash has to stay because it identifies the asset, but you can put any readable name after it, and transformation parameters such as q, fit and auto=format still work. Sanity documents this in Presenting images → Downloads and vanity filenames.
Format:
https://cdn.sanity.io/images/<projectId>/<dataset>/<hashedFileName>/<friendly-name>.<ext>?<params>Your example:
https://cdn.sanity.io/images/1234567/production/754c8d64997b668642bd2500f833722c6293cf8b-1766x1065.png/my-friendly-image-name.png?q=75&fit=clip&auto=formatThe vanity filename must use the asset's original extension (.png here), so don't change the extension.
1. Query a name to use
Each image asset document (sanity.imageAsset) has an originalFilename field, next to url and metadata (Manage assets). originalFilename can be missing, for example if the upload didn't keep it because storeOriginalFilename was disabled. Plan a fallback, such as a slug or alt-text field of your own:
*[_type == "post" && slug.current == $slug][0]{
title,
mainImage{
...,
asset->{ _id, url, originalFilename }
}
}Keep the whole image field (mainImage), not only the asset. @sanity/image-url needs the field to apply crop and hotspot.
2. Add the name to the URL path
urlFor(...).url() returns a URL that already has its query string. Insert the name into the path, not after the parameters:
import { createImageUrlBuilder, type SanityImageSource } from '@sanity/image-url'
import { client } from './client'
const builder = createImageUrlBuilder(client)
export function urlFor(source: SanityImageSource) {
return builder.image(source)
}
export function withVanityName(imageUrl: string, name?: string) {
if (!name) return imageUrl
const url = new URL(imageUrl)
url.pathname = `${url.pathname}/${encodeURIComponent(name)}`
return url.toString()
}
// Usage
const src = withVanityName(
urlFor(post.mainImage).width(1200).quality(75).auto('format').url(),
post.mainImage.asset.originalFilename // or a slugified title + the original extension
)If you use your own name instead of originalFilename, end it with the asset's original extension.
Related: downloads
The dl parameter is a separate feature. It sets the filename for a download (?dl= on its own uses the original upload filename when one was stored) and doesn't change the URL path. For image SEO, use the vanity path above.
Show original thread3 replies
Was this answer helpful?
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.