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

The conversion to WebP on the Image component does not apply to the last image in the srcset #2899

Open
x1024 opened this issue Jan 8, 2024 · 0 comments

Comments

@x1024
Copy link

x1024 commented Jan 8, 2024

Describe the bug
The Builder.IO image component automatically changes any builder.io image URL to be converted to WebP.
For example, if the image is at https://cdn.builder.io/api/v1/image/IMAGEURL, the URL will be updated to https://cdn.builder.io/api/v1/image/IMAGEURL?format=webp. This will be done in the srcset property of the <picture> tag in the <Image /> component.

At least, that is the theroy. This update is done by replacing the ? character in the URL. However, due to the way images are used in Builder.IO, the srcset property includes one url without a '?' in it. Ironically, this is the largest image in the srcset, and it's also the one that gets loaded when Google evaluates the PageSpeed of the page. This ruins PageSpeed scores for absolutely no reason.

return state.srcSetToUse.replace(/\?/g, '?format=webp&');

<source srcSet={srcset.replace(/\?/g, '?format=webp&')} type="image/webp" />

Our solution was something to the tune of:

srcset = srcset.replace(/(\?|$)/g, "?format=webp&").replace(/&$/, "")

Notably, we also had to update the actual tag that's also part of the component.

To Reproduce
Steps to reproduce the behavior:

  1. Just use any Builder.IO component. In our setup, we use it through the builder.io-react NPM package with a Gatsby project, but it should work in any React app (also, anything that uses Image.lite.tsx)
  2. Look at the srcset - it will look something like this (simplified): "image.png?format=webp&width=100 100w, [...] image.png" - the last image won't have the ?format=webp part
  3. Test the published page in Google PageSpeed - it will show a bad PS score and say something like "Convert PNG images to WebP"

Expected behavior
If any of the images in the srcset should be converted to WebP, then they should all be.

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

No branches or pull requests

1 participant