-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
938feb3
commit fe5470d
Showing
6 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
lighthouse-core/audits/byte-efficiency/unused-images.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* @fileoverview Checks to see if images are displayed only outside of the viewport. | ||
*/ | ||
'use strict'; | ||
|
||
const Audit = require('./byte-efficiency-audit'); | ||
const URL = require('../../lib/url-shim'); | ||
|
||
const ALLOWABLE_OFFSCREEN_X = 100; | ||
const ALLOWABLE_OFFSCREEN_Y = 200; | ||
|
||
const IGNORE_THRESHOLD_IN_BYTES = 2048; | ||
|
||
class UnusedImages extends Audit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'Images', | ||
name: 'unused-images', | ||
description: 'Unused images', | ||
helpText: 'Images that are not above the fold should be lazy loaded on interaction', | ||
requiredArtifacts: ['ImageUsage', 'ViewportDimensions', 'networkRecords'] | ||
}; | ||
} | ||
|
||
/** | ||
* @param {{top: number, right: number, bottom: number, left: number}} imageRect | ||
* @param {{scrollWidth: number, scrollHeight: number}} viewportDimensions | ||
* @return {number} | ||
*/ | ||
static computeVisiblePixels(imageRect, viewportDimensions) { | ||
const scrollWidth = viewportDimensions.scrollWidth; | ||
const scrollHeight = viewportDimensions.scrollHeight; | ||
|
||
const top = Math.max(imageRect.top, -1 * ALLOWABLE_OFFSCREEN_Y); | ||
const right = Math.min(imageRect.right, scrollWidth + ALLOWABLE_OFFSCREEN_X); | ||
const bottom = Math.min(imageRect.bottom, scrollHeight + ALLOWABLE_OFFSCREEN_Y); | ||
const left = Math.max(imageRect.left, -1 * ALLOWABLE_OFFSCREEN_X); | ||
|
||
return Math.max(right - left, 0) * Math.max(bottom - top, 0); | ||
} | ||
|
||
/** | ||
* @param {!Object} image | ||
* @param {{scrollWidth: number, scrollHeight: number}} viewportDimensions | ||
* @return {?Object} | ||
*/ | ||
static computeWaste(image, viewportDimensions) { | ||
const url = URL.getDisplayName(image.src, {preserveQuery: true}); | ||
const totalPixels = image.clientWidth * image.clientHeight; | ||
const visiblePixels = this.computeVisiblePixels(image.clientRect, viewportDimensions); | ||
const wastedRatio = 1 - visiblePixels / totalPixels; | ||
const totalBytes = image.networkRecord.resourceSize; | ||
const wastedBytes = Math.round(totalBytes * wastedRatio); | ||
|
||
if (!Number.isFinite(wastedRatio)) { | ||
return new Error(`Invalid image sizing information ${url}`); | ||
} | ||
|
||
return { | ||
url, | ||
preview: { | ||
url: image.networkRecord.url, | ||
mimeType: image.networkRecord.mimeType | ||
}, | ||
totalBytes, | ||
wastedBytes, | ||
wastedPercent: 100 * wastedRatio, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {{results: !Array<Object>, tableHeadings: Object, | ||
* passes: boolean=, debugString: string=}} | ||
*/ | ||
static audit_(artifacts) { | ||
const images = artifacts.ImageUsage; | ||
const viewportDimensions = artifacts.ViewportDimensions; | ||
|
||
let debugString; | ||
const resultsMap = images.reduce((results, image) => { | ||
if (!image.networkRecord) { | ||
return results; | ||
} | ||
|
||
const processed = UnusedImages.computeWaste(image, viewportDimensions); | ||
if (processed instanceof Error) { | ||
debugString = processed.message; | ||
return results; | ||
} | ||
|
||
// Don't warn about an image that was also used appropriately | ||
const existing = results.get(processed.preview.url); | ||
if (!existing || existing.wastedBytes > processed.wastedBytes) { | ||
results.set(processed.preview.url, processed); | ||
} | ||
|
||
return results; | ||
}, new Map()); | ||
|
||
const results = Array.from(resultsMap.values()) | ||
.filter(item => item.wastedBytes > IGNORE_THRESHOLD_IN_BYTES); | ||
return { | ||
debugString, | ||
results, | ||
tableHeadings: { | ||
preview: '', | ||
url: 'URL', | ||
totalKb: 'Original', | ||
potentialSavings: 'Potential Savings', | ||
} | ||
}; | ||
} | ||
} | ||
|
||
module.exports = UnusedImages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
lighthouse-core/test/audits/byte-efficiency/unused-images-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const UnusedImages = | ||
require('../../../audits/byte-efficiency/unused-images.js'); | ||
const assert = require('assert'); | ||
|
||
/* eslint-env mocha */ | ||
function generateRecord(resourceSizeInKb, mimeType = 'image/png') { | ||
return { | ||
mimeType, | ||
resourceSize: resourceSizeInKb * 1024, | ||
}; | ||
} | ||
|
||
function generateSize(width, height, prefix = 'client') { | ||
const size = {}; | ||
size[`${prefix}Width`] = width; | ||
size[`${prefix}Height`] = height; | ||
return size; | ||
} | ||
|
||
function generateImage(size, coords, networkRecord, src = 'https://google.com/logo.png') { | ||
Object.assign(networkRecord || {}, {url: src}); | ||
|
||
const x = coords[0]; | ||
const y = coords[1]; | ||
|
||
const clientRect = { | ||
top: y, | ||
bottom: y + size.clientHeight, | ||
left: x, | ||
right: x + size.clientWidth, | ||
}; | ||
const image = {src, networkRecord, clientRect}; | ||
Object.assign(image, size); | ||
return image; | ||
} | ||
|
||
describe('UnusedImages audit', () => { | ||
const DEFAULT_DIMENSIONS = {scrollWidth: 1920, scrollHeight: 1080}; | ||
|
||
it('handles images without network record', () => { | ||
const auditResult = UnusedImages.audit_({ | ||
ViewportDimensions: DEFAULT_DIMENSIONS, | ||
ImageUsage: [ | ||
generateImage(generateSize(100, 100), [0, 0]), | ||
], | ||
}); | ||
|
||
assert.equal(auditResult.results.length, 0); | ||
}); | ||
|
||
it('does not find used images', () => { | ||
const urlB = 'https://google.com/logo2.png'; | ||
const urlC = 'data:image/jpeg;base64,foobar'; | ||
const auditResult = UnusedImages.audit_({ | ||
ViewportDimensions: DEFAULT_DIMENSIONS, | ||
ImageUsage: [ | ||
generateImage(generateSize(200, 200), [0, 0], generateRecord(100)), | ||
generateImage(generateSize(100, 100), [0, 1080], generateRecord(100), urlB), | ||
generateImage(generateSize(400, 400), [1720, 1080], generateRecord(3), urlC), | ||
], | ||
}); | ||
|
||
assert.equal(auditResult.results.length, 0); | ||
}); | ||
|
||
it('finds unused images', () => { | ||
const url = s => `https://google.com/logo${s}.png`; | ||
const auditResult = UnusedImages.audit_({ | ||
ViewportDimensions: DEFAULT_DIMENSIONS, | ||
ImageUsage: [ | ||
// offscreen to the right | ||
generateImage(generateSize(200, 200), [3000, 0], generateRecord(100)), | ||
// offscreen to the bottom | ||
generateImage(generateSize(100, 100), [0, 2000], generateRecord(100), url('B')), | ||
// offscreen to the top-left | ||
generateImage(generateSize(100, 100), [-2000, -1000], generateRecord(100), url('C')), | ||
// offscreen to the bottom-right | ||
generateImage(generateSize(100, 100), [3000, 2000], generateRecord(100), url('D')), | ||
// half offscreen to the top | ||
generateImage(generateSize(1000, 1000), [0, -500], generateRecord(100), url('E')), | ||
], | ||
}); | ||
|
||
assert.equal(auditResult.results.length, 5); | ||
const wasted = auditResult.results[4].wastedBytes; | ||
assert.ok(wasted < 100 * 1024 * 0.5, 'computes wastedBytes by ratio offscreen'); | ||
}); | ||
|
||
it('de-dupes images', () => { | ||
const urlB = 'https://google.com/logo2.png'; | ||
const auditResult = UnusedImages.audit_({ | ||
ViewportDimensions: DEFAULT_DIMENSIONS, | ||
ImageUsage: [ | ||
generateImage(generateSize(50, 50), [0, 0], generateRecord(50)), | ||
generateImage(generateSize(1000, 1000), [1000, 1000], generateRecord(50)), | ||
generateImage(generateSize(50, 50), [0, 1500], generateRecord(200), urlB), | ||
generateImage(generateSize(400, 400), [0, 1500], generateRecord(90), urlB), | ||
], | ||
}); | ||
|
||
assert.equal(auditResult.results.length, 1); | ||
}); | ||
}); |