How to Load an Image Using the Original File Name

5 replies
Last updated: Oct 28, 2021
I am pretty sure that I've seen this somewhere, but I can't find it. Is there a way to load an image using its original file name (through the URL)?
Hey Christopher! To clarify, are you talking about using the original filename when using the imageUrl builder?
Not quite. I need to feed it into GatsbyImage, so I have:

        const gatsbyImageData = imageUrlsAndFilesNames[domNode.attribs.src].gatsbyImageData
        return (
          <GatsbyImage image={gatsbyImageData} className={domNode.attribs.class} alt={domNode.attribs.alt} />
        )
I had to hit the graphQL for all images (3500+) build an array, then filter through the results just to pull out one, but there must be an easier way.
Ah, I see what you mean. I'll play around with it and see if I can come up with a better solution.
Actually, I solved it another way, but I was wondering if we can eventually do that.... use the original filename as a parameter. Thanks for your help
user M
!

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?