Skip to content

Commit

Permalink
core(responsive-images): move images with no dimensions to offscreen …
Browse files Browse the repository at this point in the history
…audit (#4487)
  • Loading branch information
patrickhulce authored and paulirish committed Feb 9, 2018
1 parent 9825c6c commit c3620b1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lighthouse-cli/test/fixtures/byte-efficiency/tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ <h2>Byte efficiency tester page</h2>
<!-- FAIL(offscreen): image is offscreen -->
<img style="margin-top: 1000px; width: 120px; height: 80px;" src="lighthouse-480x320.webp">

<!-- PASS(optimized): image is JPEG optimized -->
<!-- PASS(webp): image is WebP optimized -->
<!-- PASS(responsive): image is not visible -->
<!-- FAIL(offscreen): image is not visible -->
<div class="onscreen" style="display: none;"><img class="onscreen" style="width: 120px; height: 80px;" src="lighthouse-480x320.webp?invisible"></div>

<!-- PASS(optimized): image is vector -->
<!-- PASS(webp): image is vector -->
<!-- PASS(responsive): image is vector -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ module.exports = [
url: /lighthouse-unoptimized.jpg$/,
}, {
url: /lighthouse-480x320.webp$/,
}, {
url: /lighthouse-480x320.webp\?invisible$/,
}, {
url: /large.svg$/,
},
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/audits/byte-efficiency/offscreen-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OffscreenImages extends ByteEfficiencyAudit {
name: 'offscreen-images',
description: 'Offscreen images',
informative: true,
helpText: 'Consider lazy-loading offscreen images to improve page load speed ' +
helpText: 'Consider lazy-loading offscreen and hidden images to improve page load speed ' +
'and time to interactive. ' +
'[Learn more](https://developers.google.com/web/tools/lighthouse/audits/offscreen-images).',
requiredArtifacts: ['ImageUsage', 'ViewportDimensions', 'traces', 'devtoolsLogs'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class UsesResponsiveImages extends ByteEfficiencyAudit {
const totalBytes = image.networkRecord.resourceSize;
const wastedBytes = Math.round(totalBytes * wastedRatio);

// If the image has 0 dimensions, it's probably hidden/offscreen, so let the offscreen-images
// audit handle it instead.
if (!usedPixels) {
return null;
}

if (!Number.isFinite(wastedRatio)) {
return new Error(`Invalid image sizing information ${url}`);
}
Expand Down Expand Up @@ -85,6 +91,8 @@ class UsesResponsiveImages extends ByteEfficiencyAudit {
}

const processed = UsesResponsiveImages.computeWaste(image, DPR);
if (!processed) return;

if (processed instanceof Error) {
debugString = processed.message;
Sentry.captureException(processed, {tags: {audit: this.meta.name}, level: 'warning'});
Expand Down

0 comments on commit c3620b1

Please sign in to comment.