日本語

Fix Lighthouse “Defer offscreen images” Warning with loading=”lazy”

​​Lighthouse​Chrome,HTML,Performance

When a Lighthouse 12 or earlier report shows "Defer offscreen images" (in Japanese reports, this audit is labeled as deferred loading of off-screen images), adding loading="lazy" to the <img> elements that are not in the first screen clears the warning. This is worth checking on a page with many photos, such as a blog or product list, that is slow to open on a phone. On the page I tested, the three flagged photos (271 KiB) were no longer loaded at first, and LCP (the time until the largest image or text block visible in the viewport is rendered) dropped from 3.2 s to 2.1 s.

This audit was removed in Lighthouse 13. The Lighthouse 13 report does not show it for the same page.

Note

The test environment was Ubuntu 24.04, Node.js 22, and Lighthouse 12.8.2 and 13.6.0 (npm packages). I ran Lighthouse from the command line with the default settings (mobile, 412×823) and checked only the performance category. I pointed the CHROME_PATH environment variable at Chrome for Testing 158, which comes with Playwright 1.63.0. The pages were served from a small server I ran locally (localhost:8080, HTTP/1.1). The photos are placeholder images made with sharp 0.33.5, not real photos. I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, desktop settings, browsers other than Chrome, or lazy loading with a JavaScript library.

The page that triggered the warning

I used the following page for testing. It has a large image hero.jpg (800×450, 122,915 bytes) at the top, followed by a 5,000px-tall body text, and three photos (600×450, about 92,000 bytes each) at the bottom. The last file, app.js, is a script that blocks the main thread for about 300 ms when it loads. I added it in place of heavy JavaScript (the reason it is needed is explained later). The following HTML is only the part inside <body>.

html
<h1>猫の手日記</h1>
<img src="hero.jpg" alt="窓辺で外を見る猫" width="800" height="450">
<div class="long" style="height:5000px"><p>今日の記録です。</p></div>
<h2>写真</h2>
<img src="photo1.jpg" alt="猫の写真 1" width="600" height="450">
<img src="photo2.jpg" alt="猫の写真 2" width="600" height="450">
<img src="photo3.jpg" alt="猫の写真 3" width="600" height="450">
<script src="app.js"></script>

I opened the page with Playwright at 412×823, the same screen size Lighthouse uses. The photos sit at 5,388px, 5,689px, and 5,990px from the top of the page, so none of them are in the first screen. Even so, all three were loaded as soon as the page opened.

I ran Lighthouse 12.8.2 on this page and used a small script (summarize.mjs) to pull the relevant parts from the JSON result.

console
$ npx lighthouse http://localhost:8080/far.html --only-categories=performance --output=json --output=html --output-path=./out/lh12-far --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
$ node summarize.mjs out/lh12-far.report.json
lighthouseVersion: 12.8.2  formFactor: mobile  viewport: 412x823
performance score: 70  LCP: 3.2 s  TBT: 1,150 ms
offscreen-images: score=0 scoreDisplayMode=metricSavings weight=0 title="Defer offscreen images" displayValue="Est savings of 271 KiB" metricSavings={"FCP":0,"LCP":1050}
  - http://localhost:8080/photo3.jpg totalBytes=92413 wastedBytes=92413 wastedPercent=100.0
  - http://localhost:8080/photo1.jpg totalBytes=92363 wastedBytes=92363 wastedPercent=100.0
  - http://localhost:8080/photo2.jpg totalBytes=92359 wastedBytes=92359 wastedPercent=100.0
image requests: hero.jpg, photo1.jpg, photo2.jpg, photo3.jpg
total-byte-weight: Total size was 394 KiB

(I left out the line about image delivery, "Improve image delivery".)

In the HTML report, the "Defer offscreen images" item, marked with a red triangle, lists each photo with its Resource Size and its Est Savings.

Lighthouse 12.8.2 report with the Defer offscreen images audit expanded. It shows "Est savings of 271 KiB", and a table lists /photo3.jpg, /photo1.jpg, and /photo2.jpg, each with Resource Size 90.2 KiB and Est Savings 90.2 KiB
Report flagging the three photos that are off-screen

When the report is in Japanese (--locale=ja), the audit name is shown as the deferred loading of off-screen images, and the savings text is "Estimated reduction 271 KiB" (Est savings of 271 KiB). The description means: "Consider deferring off-screen, hidden images until all critical resources have loaded to reduce time to interactive."

In Lighthouse 12.8.2 with the default settings, this audit has a weight of 0 in the performance score. It does not count directly toward the score.

Adding loading="lazy" to off-screen img elements

The fix is to add loading="lazy" to the photo <img> elements.

html
<img src="photo1.jpg" alt="猫の写真 1" width="600" height="450" loading="lazy">
<img src="photo2.jpg" alt="猫の写真 2" width="600" height="450" loading="lazy">
<img src="photo3.jpg" alt="猫の写真 3" width="600" height="450" loading="lazy">

According to MDN's img element reference, an image with loading="lazy" is not loaded until it gets within a certain distance of the viewport. Without the attribute, the image behaves like eager, so it loads right away even when it is off-screen.

After this change, I ran Lighthouse 12.8.2 again. The warning was gone. Only hero.jpg was loaded, and the total transfer dropped from 394 KiB to 122 KiB.

console
$ node summarize.mjs out/lh12-lazy.report.json
lighthouseVersion: 12.8.2  formFactor: mobile  viewport: 412x823
performance score: 76  LCP: 2.1 s  TBT: 1,150 ms
offscreen-images: score=1 scoreDisplayMode=metricSavings weight=0 title="Defer offscreen images" displayValue="" metricSavings={"FCP":0,"LCP":0}
image requests: hero.jpg
total-byte-weight: Total size was 122 KiB

(I left out the Lighthouse command and the line about image delivery, "Improve image delivery".)

The score went from 70 to 76, and LCP went from 3.2 s to 2.1 s. This LCP is a simulated value that Lighthouse calculates under throttled network and CPU conditions. Before the fix, I ran the page four times, including one run with the report in Japanese. LCP was 3.2–3.3 s, and the score was 69–70.

The photos still load when the user needs them. I opened the same 412×823 screen in Playwright and scrolled to the bottom. All three photos loaded at that point.

console
lazy: img top(px)=hero.jpg:87 photo1.jpg:5388 photo2.jpg:5689 photo3.jpg:5990
  requested on load=hero.jpg
  requested after scrolling to the bottom=photo1.jpg, photo2.jpg, photo3.jpg
Warning

Do not add loading="lazy" to images that are in the first screen. The web.dev guide to lazy loading also advises against lazy loading images that are likely to be visible when the page opens, especially the LCP image. I did not add it to the top image, hero.jpg, either.

When the warning appears

Off-screen images are not always flagged. I read the audit code in Lighthouse 12.8.2 (core/audits/byte-efficiency/offscreen-images.js) and then changed the page to check the conditions.

Page Photo position and markup Photos loaded Warning
Before the fix 5,388px from top, heavy script present 3 Yes (271 KiB)
loading="lazy" 5,388px from top, heavy script present 0 No
Photos close to the top 1,888px from top, heavy script present 3 No
No script 5,388px from top, no heavy script 3 No
loading="eager" 5,388px from top, heavy script present 3 No

Images within four screen heights of the top are not counted

When the code works out how much of an image is inside the screen, it extends the bottom edge of the screen down by three times the screen height (ALLOWABLE_OFFSCREEN_BOTTOM_IN_VIEWPORTS = 3). With the mobile setting, the screen height is 823px, so images that lie within 3,292px from the top of the page are not counted as off-screen.

Two diagrams side by side: one page with photos at 1,888px from the top and one with photos at 5,388px. A dashed line sits at 3,292px. Photos above the line are not flagged. Photos below the line are flagged (271 KiB)
Photo positions and the point where Lighthouse 12 starts counting images as off-screen

On the page with photos at 1,888px, all three were off-screen and loaded when the page opened, but the audit still did not flag them. The audit also drops an image when less than 75% of it is counted as off-screen, or when the estimated savings are under 2,048 bytes.

Pages without a heavy script may not be flagged

On the page without app.js, the photos were in the same position and all three loaded, but no warning appeared. TBT (the total time the main thread is blocked long enough to stop the page responding to input) went from 1,150 ms to 0 ms.

With the default settings, the code uses the simulated timeline. It finds the last long CPU task (50 ms or more), and flags only images whose requests started at least 50 ms before that task began. On a page without any long task, the audit does not report this item, even if off-screen images are loaded. So the absence of this warning does not confirm that you are avoiding loading off-screen images.

Images with loading="eager" are excluded

The code treats images with loading set to lazy or eager as passing, without checking them. On the page with loading="eager", all three photos loaded when the page opened, but no warning appeared. Adding eager to clear the warning does not reduce the amount of data loaded.

Images in hidden tabs are also flagged

I also tested a page with a photo inside a tab hidden with the hidden attribute, placed near the top.

html
<div id="tab2" hidden>
<img src="photo1.jpg" alt="猫の写真 1" width="600" height="450">
</div>

Even though the photo was not visible, it loaded when the page opened, and Lighthouse 12.8.2 reported it under "Defer offscreen images" with 90 KiB of estimated savings. The code treats an image with a rendered size of 0 as entirely off-screen.

When I added loading="lazy" to this <img>, it did not load when the page opened, and the warning went away. In Playwright, I scrolled to the bottom, scrolled back to the top, and removed hidden. The photo loaded at that point.

console
hidden-lazy: img top(px)=hero.jpg:87 photo1.jpg:0
  requested on load=hero.jpg
  requested after scrolling to the bottom=(none)
  requested after scrolling back to the top and showing #tab2=photo1.jpg

The audit was removed in Lighthouse 13

The Lighthouse 13.0.0 changelog lists "remove offscreen-images audit." The Lighthouse 13 announcement includes it among the audits removed without a replacement. The stated reason is that browsers already lower the priority of off-screen images, so lazy loading reduces transfer size but has little effect on what Lighthouse measures.

When I ran Lighthouse 13.6.0 on the same page before the fix, the JSON result had no offscreen-images audit.

console
$ node summarize.mjs out/lh13-far.report.json
lighthouseVersion: 13.6.0  formFactor: mobile  viewport: 412x823
performance score: 70  LCP: 3.2 s  TBT: 1,150 ms
offscreen-images: (no such audit)
image requests: hero.jpg, photo1.jpg, photo2.jpg, photo3.jpg
total-byte-weight: Total size was 394 KiB
$ grep -c offscreen-images out/lh13-far.report.json
0

(I left out the Lighthouse command and the line about image delivery, "Improve image delivery". I also reordered the grep line from the order I ran the commands.)

With Lighthouse 13.6.0, the page with loading="lazy" loaded only hero.jpg, for a total of 122 KiB and an LCP of 2.1 s. Before the fix, it was 394 KiB and 3.2 s. On this page, lazy loading still showed up in the numbers in Lighthouse 13.

To check off-screen images in Lighthouse 13, look at the "Avoids enormous network payloads" audit (in Japanese reports, its label means "avoid excessive network payloads"). It lists loaded files from largest to smallest, so you can see whether any images outside the first screen are being loaded. On this page the audit passed, so it was under "Passed audits," which I expanded. There, hero.jpg was followed by the three photos.

The "Defer offscreen images" audit that appears in Lighthouse 12 reports and guides does not exist in Lighthouse 13. When you review a Lighthouse 13 result, do not look for this item. Check the list of loaded images instead.

​Posted by 86