I see you got your Lighthouse score sorted but with the way you did it you are not taking into account display density.
Here is how I would refactor your code to take into account display density.
The thing is the browser doesn’t know the size at which the image will be rendered on the page until it has downloaded and parsed all the relevant CSS and JavaScript files. If we give sizes based on screen width then the browser will know which image to use.
In your case your image has three sizes it can be:
- Up to a screen width of 42rem the image is 100%
- Then up to a screen width of 64rem the image is 640px
- Larger than that the image is essentially 756px
So we set the sizes as:
sizes="(max-wdith: 42rem) 100vw, (max-width: 64rem) 640px, 756px"
And adding a range of srcset sizes so the browser can grab a suitable size. The browser is smart enough to know which size it needs, for example, with a double density screen on desktop the image will be 756px wide, but being double density it will grab the 1512px image.
Putting it all together would give us:
<img
src="https://thedixons.net/img/grandpacornall.jpg?nf_resize=fit&w=756"
srcset="
https://thedixons.net/img/grandpacornall.jpg?nf_resize=fit&w=320 320w,
https://thedixons.net/img/grandpacornall.jpg?nf_resize=fit&w=640 640w,
https://thedixons.net/img/grandpacornall.jpg?nf_resize=fit&w=756 756w,
https://thedixons.net/img/grandpacornall.jpg?nf_resize=fit&w=1280 1280w,
https://thedixons.net/img/grandpacornall.jpg?nf_resize=fit&w=1512 1512w"
sizes="(max-wdith: 42rem) 100vw, (max-width: 64rem) 640px, 756px"
>
Here is the code pen https://codepen.io/wanacode/full/BaoBaBO