-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(image): throw if alt text is missing #4511
Conversation
🦋 Changeset detectedLatest commit: 618834e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Astro's `<Image />` and `<Picture />` components will throw an error if no `alt` text is provided. | ||
|
||
If the image is merely decorative (i.e. doesn't contribute to the understanding of the page), set `alt=""` so it is ignored by screen readers. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOVE this new feature, @DerYeger! I have installed some Chrome extensions myself to make sure I'm not allowed to Tweet without alt-text on my images. 😄
Please ALSO check the "Usage" heading further down the page where the attributes, and whether or not they are required are listed for both components. There should be an alt
heading and description for each one of them. It looks like it's currently not there for <Image />
but is there for <Picture />
though only saying "if it's provided."
These should both prominently display the new info, and double check all the examples to make sure they all show alt=
on every component.
Lastly, @DerYeger & @tony-navillus, what exactly is the error message that is thrown? (Sorry, hard to look through everything on my phone here) If it's not, can it be an explicit message to add alt text rather than a generic error?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I used the suggestion of #4291 (comment) as the error message, replacing image
with picture
for the respective component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! I've merged the PR for the docs "Images Guide" page just now. Just let me know if you're up for making the above edits under "Usage" (as an attribute, like all the other aspectRatio
, src
etc.) in addition to your note here, or whether you'd like me to take care of that! But, it DOES need to be there as an attribute for each component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes don't appear to have been made, so I'm making them now here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is excellent, thanks for the thorough test coverage!
Consider the feature work here 🎉 @sarah11918 I'll leave the docs approval up to you if you don't mind, once it has your approval as well I'll get this merged in and ready for release 🚀
Yay! Will look closely when I am back at a laptop! |
@DerYeger Let me know if you have any issues with the merge conflict! |
I'll get those tests passing, looks like the tests in main are too aggressive with hard-coded filename hashes |
@tony-sull Ooh, good! I need to make a fix to this README anyway. Please do not merge this until I've updated the docs here. 💜 |
OK @tony-sull PLEASE check all my alt-text stuff carefully! Code samples! etc! Please make sure it's actually correct and no typos. 💜 Otherwise, I'm happy. |
Thanks! Let me know I can help out anywhere else. |
Just opened #4579 with a big cleanup of the image tests. This PR was a perfect kick in the pants for me to stop dragging my feet on refactoring all the test code 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I had a few comments, mainly around documentation. Thanks for pushing us to add this, I'm a big +1 for this feature.
@@ -21,6 +23,10 @@ export type Props = LocalImageProps | RemoteImageProps; | |||
|
|||
const { loading = 'lazy', decoding = 'async', ...props } = Astro.props as Props; | |||
|
|||
if (props.alt === undefined || props.alt === null) { | |||
throw new Error('The <Image> component requires you provide alt text. If this image does not require an accessible label, set alt="".') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
link to more reading in case this is a new concept for the reader! https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-alt would probably be good.
FYI no need to worry too much about the final formatting here for this error message, as long as all of the info is correct. @tony-sull or myself will plan to finalize this ourselves with correct formatting before merging the PR, to match other errors in our codebase.
} | ||
|
||
interface RemoteImageProps extends TransformOptions, astroHTML.JSX.ImgHTMLAttributes { | ||
src: string; | ||
alt: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a helpful warning here as JSDoc as well? Something like:
/** Defines an alternative text description of the image. Set to an empty string (alt="") if the image is not a key part of the content (it's decoration or a tracking pixel). **/
alt: string;
Ditto for the LocalImageProps
type above.
@tony-sull @DerYeger I'm realizing that this will break most existing users who didn't add alt text, and my guess is that this is a lot of them. Even if we make this error helpful, that's still going to be a real pain to upgrade given that we won't even render the page anymore if Thoughts on making this a TS-only change first, and then adding the runtime error in a future minor release? This will still provide a very visible error in FYI to implement this change, everything about this PR can stay as-is except for removing the ~3 lines that throw the error at runtime. |
@FredKSchott are you proposing essentially we warn in 1 "major" (minor in this case because < 1.0) release and then break in the next? If so I think that's fine and in line with some other changes we've done. |
@FredKSchott That's a good callout, I like the idea of warning for now and throwing the error in a later release (either a future minor or 1.0) @DerYeger The tests should be almost there now, I refactored all the image tests to avoid using hard-coded filename hashes but it looks like I missed an |
@FredKSchott @sarah11918 I pushed a few updates here to include the comments above. Any additional feedback before merging this in? For the warning, I wrapped it in a helper to make sure it's only logged once per |
That’s a great change, thanks for cleaning up this PR! |
Thanks for this PR, @DerYeger! Excellent test coverage and docs updates 🎉 Very excited to see this flipped over to a build error in the future, I'm always a fan of build tools making sure I don't accidentally create a11y issues! |
Yup, pretty much! Glad everyone was on board for this, sgtm and excited to see this merged! |
Changes
@astrojs/image
should throw an error ifalt
text is missing #4291<Image>
and<Picture>
now throw ifalt
prop is missingalt
Testing
This change was tested by adding fixtures that have images without
alt
text.The tests assert that an error is thrown.
Docs
PR: withastro/docs#1421