👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Issue with alt text not displaying for images in Sanity plugin media library.

12 replies
Last updated: Mar 8, 2022
Hello everyone, please help me with this issue. The issue is:- I add sanity plugin media in the sanity dashboard. There are some options given on the Sanity plugin image widget. Like alt text and I update alt text for the image. But the issue is how I get all the data on the front end. I don't get any alt text on Groq. Only getting the image URL.
Mar 3, 2022, 2:33 PM
hey
user A
the GROQ query is:-
*[_type == "page" && !(_id in path('drafts.**'))][0] {"id": _id, ...}
Mar 4, 2022, 2:32 PM
Here is console data
image:asset:_ref: "image-6d2fe8729fb831257b98763b6734668daa27b696-2124x1590-png"
_type: "reference"
Mar 4, 2022, 2:44 PM
Schema for image is :-
{
      name: 'image',
      title: 'Image',
      type: 'image',
    },
Mar 4, 2022, 2:45 PM
Library i used on sanity is:-
"sanity-plugin-media-library": "^2.0.2",
image is previewing on frontend but the alt tag is not working. please help.
Mar 4, 2022, 2:46 PM
import Image from 'next/image';
import imageUrlBuilder from '@sanity/image-url';

import sanity from '../../utils/sanity';
import config from '../../utils/config';

export function urlFor(source) {
  return imageUrlBuilder(sanity).image(source);
}

const SanityImage = ({ src, layout, width, height, alt, ...props }) => (
  <Image
    src={urlFor(src).width(width).height(height).url()}
    width={width}
    height={height}
    layout={layout || 'responsive'}
    alt={alt || config.siteName}
    {...props}
  />
);

export default SanityImage;
Mar 4, 2022, 3:55 PM
You need to resolve the reference in a porjection.
*[_type == "page" && !(_id in path('drafts.**'))][0] {..,
"id": _id, 
"Image": image.asset ->,
...}
Mar 6, 2022, 2:44 PM
Then you can also just query for the alt.text
Mar 6, 2022, 2:45 PM
*[_type == "page" &amp;&amp; !(_id in path('drafts.**'))][0] {.., "id": _id,
"image": image.asset -&gt;,
"alt": image.asset.alt
...}
Mar 6, 2022, 2:51 PM
Thank you so much...
Mar 7, 2022, 5:26 AM
Happy to help. I struggeled with this in the beginning as well. https://hdoro.dev/learn-groq this here helped me quite a lot to understand the more “nested” functionality of groq
Mar 7, 2022, 3:06 PM
Happy to help. I struggeled with this in the beginning as well. https://hdoro.dev/learn-groq this here helped me quite a lot to understand the more “nested” functionality of groq
Mar 7, 2022, 3:06 PM
great thank you
Mar 8, 2022, 6:42 AM

Sanity– build remarkable experiences at scale

Sanity is a modern 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?