Issue with alt text not displaying for images in Sanity plugin media library.
12 replies
Last updated: Mar 8, 2022
D
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
D
hey
user A
the GROQ query is:- *[_type == "page" && !(_id in path('drafts.**'))][0] {"id": _id, ...}
Mar 4, 2022, 2:32 PM
D
Here is console data
image:asset:_ref: "image-6d2fe8729fb831257b98763b6734668daa27b696-2124x1590-png" _type: "reference"
Mar 4, 2022, 2:44 PM
D
Schema for image is :-
{ name: 'image', title: 'Image', type: 'image', },
Mar 4, 2022, 2:45 PM
D
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
D
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
S
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
S
Then you can also just query for the alt.text
Mar 6, 2022, 2:45 PM
S
*[_type == "page" && !(_id in path('drafts.**'))][0] {.., "id": _id,
"image": image.asset ->,
"alt": image.asset.alt
...}
"image": image.asset ->,
"alt": image.asset.alt
...}
Mar 6, 2022, 2:51 PM
D
Thank you so much...
Mar 7, 2022, 5:26 AM
S
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
S
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
D
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.