Skip to content
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

Make width, height and alt required props #1612

Open
Baroshem opened this issue Nov 27, 2024 · 7 comments
Open

Make width, height and alt required props #1612

Baroshem opened this issue Nov 27, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@Baroshem
Copy link
Collaborator

Hey there!

In Nuxt Cloudinary module we have these props as required to promote good practices for images.

If you omit these props, popular tools such as Lighthouse can report this as an issue.

Do you think that we could add these props to Nuxt Image as well as required? Or maybe add a warning in the console that you should add them to have better score and overall :)

I can create a draft PR with this change if you would like to test how it works :)

@w33bvGL
Copy link

w33bvGL commented Nov 27, 2024

The console warning is a particularly useful addition. Waiting for the draft

@Baroshem
Copy link
Collaborator Author

@danielroe what do you think about that? :)

@madc
Copy link
Contributor

madc commented Dec 1, 2024

There are cases where leaving the alt attribute empty makes sense. For example, decorative images that don't add meaningful context can be hidden from screen readers by using alt="". This helps keep the experience clear and focused for users relying on assistive technologies.

@Baroshem Baroshem added the enhancement New feature or request label Dec 3, 2024
@Baroshem
Copy link
Collaborator Author

Baroshem commented Dec 9, 2024

Thanks for the comment @madc

What about then checking if an alt is empty but provided in this case? So if a user will not provide prop it will display warning but but if they pass an empty string it will not display a warning? :)

@razbakov
Copy link
Contributor

razbakov commented Dec 14, 2024

Width and height can be auto generated. Let's assume demo.jpg is 2000x2000.

I think this <NuxtImg src="/demo.jpg" sizes="sm:50vw md:400px" /> should be interpreted as <NuxtImg src="/demo.jpg" sizes="50vw md:400px" />.

And following this logic I don't see the need for sizes, as it's possible to do extend width syntax:
<NuxtImg src="/demo.jpg" width="50vw md:400px" /> that would be <img width="160" height="160"> - src as fallback gets the smallest image, 50vw of smallest screen is 320*50% = 160. The image ratio is 1:1, so height is also 160.

With pixel-sizes it's even easier:
<NuxtImg src="/demo.jpg" width="500px md:800px" /> would be <img width="500" height="500">

And empty width:
<NuxtImg src="/demo.jpg" /> would be <img width="320" height="320"> - src as fallback gets the smallest image, the size of xs screen. The biggest width is the original image or the largest breakpoint.

@Baroshem
Copy link
Collaborator Author

Baroshem commented Dec 18, 2024

Sounds reasonable.

I wonder if this approach will not indicate to users that they could be using sizes for optimized images (in Lighthouse for example) and the users will try to use while it will no longer be needed.

And for the alt we could check if it was not provided (including the comment above) and if user will not provide anything, show a warning in the console.

WDYT?

@danielroe
Copy link
Member

@razbakov The width/height size doesn't matter (as the actual size of the image can be set in CSS), it's only the aspect ratio that is significant to the browser. Your example assumes we know it's square. If that's the case, you can just set width and height; this will work fine on any screen size.

More generally, it would be a very nice enhancement to support detecting sizes of images at build time, perhaps only with ipx provider.

I would also be up for (as an easy first step) showing a warning in development when height/width are omitted. (Later we can move this to the nuxt/hints module - cc: @Baroshem!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants