Preserving image filenames in Sanity CDN URLs for SEO visibility

6 replies
Last updated: Apr 28, 2021
Hey everyone 👋 long-time lurker, first-time poster. I’ve been building sites and templates with Gatsby/Sanity together for a while now and along the way a fair few clients have asked me how to preserve image filenames in generated CDN URLs for SEO visibility. I’ve got as far as manipulating the
, but I can’t find anything on how Sanity manages filename generation when exporting imagery out to and hosting on the CDN. If anyone has any experience in this realm your help or advice would be much appreciated
Mar 22, 2021, 11:07 AM
user H
👋 The CDN lets you add 
 at the end of an image/file URL, while still working with the usual parameters (and things like webp format support).
This should allow you to add the
(or any other field you’ve prepared for this) at the back of a URL as follows:
Does that help?
Mar 22, 2021, 11:49 AM
Thank you, that’s really good to know! Are you able to confirm if the images including the
are indexed by Google?
For example:

^ If I search for “cats in suspicious clothing”, am I likely to find some variant of this Sanity CDN-hosted image?
Mar 22, 2021, 11:54 AM
Never mind, I wrote a component that inserts the original filenames into fluid URLs. End result is that the full filename is available in the src/srcset &lt;img&gt; attributes, but the images look and work the same as they did. Thanks for the help!
Mar 22, 2021, 1:30 PM
Awesome intel here pals! 🙏
Apr 16, 2021, 5:20 PM
user M
Is there a way to add
when using Gatsby image?
Apr 21, 2021, 4:00 PM
user A
You could enrich your fluid URLs with
), e.g.
  const {
    asset: { fluid, description, originalFilename, title }
  } = image; // Sanity image data

  const fluidKeys = Object.keys(fluid);
  const fluidEnriched = {};

  if (fluidKeys?.[0]) {
    fluidKeys.forEach((fluidKey) =&gt; {
      let value = fluid?.[fluidKey]?.toString();

      if (value.includes(`.jpg`)) {
        value = value.replace(/.jpg/g, `.jpg/${originalFilename}`);
      } else if (value.includes(`.png`)) {
        value = value.replace(/.png/g, `.png/${originalFilename}`);
      } // other filetypes

      fluidEnriched[fluidKey] = value;

import Img from "gatsby-image";

&lt;Img fluid={fluidEnriched} /&gt;
Apr 28, 2021, 9:26 AM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in