A reader told me to ship sizes with my srcset. I had no srcset, and desktop wastes more than mobile
A comment on my last image post said: Ship sizes with it. With width descriptors and no sizes, the browser assumes the image is 100vw. Good advice. I went to check my markup before writing the fix. There was no srcse
A comment on my last image post said:
Ship
sizeswith it. With width descriptors and nosizes, the browser assumes the image is 100vw.
Good advice. I went to check my markup before writing the fix. There was no srcset to add sizes to.
What the page actually has
products.html, 122 <img> tags:
| attribute | count |
|---|---|
srcset |
0 |
sizes |
0 |
loading="lazy" |
120 |
fetchpriority="high" |
1 |
So the post I wrote about lazy loading was accurate and also beside the point. Lazy loading changes when an image is fetched. It does nothing about how big the file is. Every card on that page ships one fixed file, whatever the viewport.
Measuring rendered vs natural
Reading the HTML only tells you the width/height attributes, which are the intrinsic dimensions, not the layout size. So I loaded the page in a real browser at two viewports, scrolled to the bottom to trigger every lazy image, and compared naturalWidth * naturalHeight against getBoundingClientRect().
| viewport | images | natural px | rendered px | ratio |
|---|---|---|---|---|
| 390 x 844 | 122 | 22,711,325 | 5,447,876 | 4.2x |
| 1440 x 900 | 122 | 98,178,025 | 12,700,068 | 7.7x |
The counterintuitive part: the desktop layout wastes nearly twice as many pixels as mobile. I expected the opposite.
The reason is that the card is narrower on desktop than on mobile in this grid. Two cards per row at 1440px gives each card a 385px slot; at 390px the card is 293px wide. Both slots request the same 1200x1600 source file. The desktop page also has more cards above the fold and a wider viewport, so lazy loading holds back fewer of them.
Ratio distribution, desktop: 59 of 122 images are at 2x or worse. Median 4.5x. Max 38.1x.
The two worst offenders are not the ones you would guess
<img width="1600" height="1600" src="images/mujia_logo.webp">
<!-- rendered 42x42 on desktop, 48x48 on mobile -->
A 1600x1600 logo file rendering into a 42px box. 38x linear, and it is on every page.
<img width="1182" height="1300" src="..._card.webp"
style="width:74px;height:56px;object-fit:cover">
Intrinsic 1182x1300, rendered 74x56. 16x linear. Cumulative layout shift is fine because the width and height attributes reserve the correct box, but the network still downloads the full card file to fill a thumbnail.
Bytes on the wire
Pixels are an abstraction. I instrumented the response events and counted actual content-length per resource type, scrolled through the whole page, and waited for idle.
| viewport | image requests | image bytes | other bytes | total |
|---|---|---|---|---|
| 390 x 844 | 32 | 5,465,032 (5.21 MB) | 419,567 | 5.61 MB |
| 1440 x 900 | 76 | 13,936,048 (13.29 MB) | 419,567 | 13.69 MB |
Desktop pulls 2.55x the image bytes of mobile on the same URL, same content, no interaction beyond scrolling.
And the WebP migration is not finished
The desktop image bytes break down as WebP 7,543 KB and JPEG 6,066 KB. JPEG is 43.5% of the image payload at 1440px.
Counting the JPEGs referenced from the page:
- 41 distinct
.jpgfiles - 7.44 MB total
- 40 of 41 are over 100 KB
- 14 are over 200 KB
- largest:
mj819-4_swatch_card.jpgat 338.1 KB
So before srcset even enters the picture, there is a straight format conversion sitting there worth several megabytes.
What I am actually going to do, in order
- Convert those 41 JPEGs to WebP. This is a format swap with no layout risk and the largest single win.
- Add
srcsetwith 480 / 960 / 1600 width descriptors on the card images. - Add
sizesthat match the real card width, not100vw. Something in the range of(max-width: 600px) 45vw, 22vwfor a two-per-row grid, to be measured rather than guessed. - Leave
fetchpriority="high"where it is. It is already on the LCP card and it is the one image attribute I got right. - Re-measure with the same script and publish the delta, including if it does not move.
What I am not going to claim
I am not going to publish a projected percentage saved. The 4.2x and 7.7x are pixel ratios, and pixel ratios do not translate linearly into byte savings, because WebP compression is not linear in resolution and the browser picks from discrete variants. I will report what the same measurement script says after the change, and if it lands below my expectation I will write that down too. The page I am measuring is the product index, and the 127 article pages behind it have the same image markup.
The honest summary of this post: a reader gave me a correct piece of advice about a technique I had not implemented, and checking his advice turned up a bigger problem than the one he was warning about.
Thanks to michael1980 for the comment that started this.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.