Choose the right strategy for uploading images

Choose the right strategy for uploading images

Images play a significant role on the web, and they can be used in various ways to enhance content, improve user experience, and boost SEO.

So how to add it on your site ?

<img src="photo.jpeg" alt="my photo" />        

And everyone used it in general. Who cares ? But we have Largest Contentful Paint (LCP Metric), which recommends using different formats/smaller sizes etc

LCP

Problem: What if we want to add responsive images?

responsive images
responsive images

Solution: use srcset attribute for responsive images

<img src="photo.jpg" srcset="photo-400w.jpg 400w, photo-800w.jpg 800w" sizes="(max-width: 600px) 400px, 800px" alt="Responsive photo">        

How to do? For example

https://www.responsivebreakpoints.com/


Problem: what if we want to add optimized images for different browsers?

cats
cats

Solution: using <picture> tag with srcset attribute

picture description. How it works
picture tag description
responsive picture
picture responsive example

How to do:

Here we have a lot of tools that allow you to compress images for example

https://squoosh.app/

https://squish.addy.ie/

Problem: Largest Contentful Paint metric

Here is two examples from web.dev and you can see the problem. Image shifts other content and LCP is triggered when it fully loads

Green rect is LCP

Solution: Use Progressive JPEG for hero images

I would recommend use Progressive JPEG for "Hero" images otherwise avif with a graceful degradation to jpeg/png etc

What is progressive JPEG?

Default JPEG loading line by line
Default JPEG loaded line by line
Progressive JPEG
And then we have Progressive enchancement of your JPEG

Progressive AVIF?

You also can emulate now progressive AVIF to load superlow AVIF image and load good one and replace when it ends. @JakeArchibald propose it few years ago


Note: If you want to avoid unnecessary Cumulative Layout Shift use aspect-ratio in your CSS. It helps browser reserve a place for your image/video etc
img {
    aspect-ratio:  16 / 9;
}        

Here is a calculator for aspect ratio


Last but not least is a lazy loading strategies

lazy
Do not add loading=lazy attribute to frist-screen images. It can affect LCP due to network bandwidth issues
<!-- visible in the viewport -->
<img src="product-1.jpg" alt="..." width="200" height="200">
<img src="product-2.jpg" alt="..." width="200" height="200">
<img src="product-3.jpg" alt="..." width="200" height="200">

<!-- offscreen images -->
<img src="product-4.jpg" loading="lazy" alt="..." width="200" height="200">
<img src="product-5.jpg" loading="lazy" alt="..." width="200" height="200">
<img src="product-6.jpg" loading="lazy" alt="..." width="200" height="200">        

It also works with a <picture> tag but you have to add it to <img> tag

<picture>
  <source srcset="test.avif" type="image/avif">
  <source srcset="test.webp" type="image/webp">
  <img src="test.png" alt="test image" loading="lazy">
</picture>        

Conclusion

Utilize a progressive JPEG for the hero image. Implement the AVIF format to reduce unnecessary network activity, employ graceful degradation for older browsers, and remember to consider lazy loading

I cover some based topics but there are a lot of new techniques such as use Vary, Client Hint headers, Save-data header, lambda functions like CloudFront Image Optimizer etc which i can cover if you are interested. Let me know!



要查看或添加评论,请登录

Victor Tkachenko的更多文章

  • Loading resources

    Loading resources

    Browsers limit the number of simultaneous connections to the same domain. For example, most modern browsers allow 6-8…

    2 条评论
  • Choose a right strategy for web application in 2025

    Choose a right strategy for web application in 2025

    Most web developers only care about which framework to choose (although React has almost monopolized the market). I…

社区洞察

其他会员也浏览了