How to use original filename instead of hash in Sanity image URLs?

3 replies
Last updated: Jul 7, 2022
Hi, how can I request an image from Sanity with its original filename instead of a hash?
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>
instead of
<https://cdn.sanity.io/images/1234567/production/754c8d64997b668642bd2500f833722c6293cf8b-1766x1065.png?q=75&fit=clip&auto=format>

I only found the
&dl
param 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.
AI Update

Great news! Sanity supports what's called "vanity filenames" for exactly this SEO use case. You can append a friendly filename to your image URL after the hash, and it will work perfectly with all the usual transformation parameters.

The URL format is:

https://cdn.sanity.io/images/<projectId>/<dataset>/<hashedFileName>/<originalFilename>?<params>

So your URL would become:

https://cdn.sanity.io/images/1234567/production/754c8d64997b668642bd2500f833722c6293cf8b-1766x1065.png/my-friendly-image-name.png?q=75&fit=clip&auto=format

The hash still needs to be there (it's how Sanity identifies the asset), but by adding /your-seo-friendly-name.png after it, you get the SEO benefits Google is looking for. The CDN will serve the image correctly, and all your transformation parameters (like q=75, fit=clip, auto=format) continue working as expected.

Implementation: The exact approach depends on how you're building your image URLs, but typically you'd:

  1. Query the original filename from your image asset (it's stored in the asset document)
  2. Append it to the end of your image URL before the query parameters
  3. Or use any other field you've designated for SEO-friendly names

This works for both the /images/ and /files/ CDN endpoints, and you can use modern image formats like WebP when you include auto=format - the vanity filename won't interfere with format conversion.

Unfortunately, this feature isn't prominently documented yet (which is why you couldn't find it), but it's fully supported and widely used for exactly the Google Images SEO best practices you're trying to follow.

Show original thread
3 replies
Hi
user T
, we currently support ‘vanity filenames’, which should be more user and SEO friendly than using the hash alone.
This lets you add e.g.
/someFilename.png
after the hashed file name at the end of an image/file URL, while the usual parameters continue working (e.g. webp format support in case of images).
General format:

<https://cdn.sanity.io/images/><projectId>/<dataset>/<hashedFileName>/<originalFilename>?<params>
Example:

<https://cdn.sanity.io/files/5fe89r5x/production/a123456789bb123cd1e1fgh123h1i123456j1234k1.jpg/my-friendly-image-name.jpg>
The exact implementation will depend on how you currently build the image URLs, but could be a matter of querying and appending the original file name or any other field you’ve designated for these more friendly file names.
Thanks!! That should do the job. Wasn’t able to find this in the docs.
With pleasure! Sometimes things are a little buried, that’s why we are here 😉. But I think there might be a need for a guide or doc article about SEO practices … until then good building! 💪

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?