Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lighthouse-core/gather/gatherers/image-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,28 @@ function determineNaturalSize(url) {
}

class ImageElements extends Gatherer {
constructor() {
super();
/** @type {Map<string, {naturalWidth: number, naturalHeight: number}>} */
this._naturalSizeCache = new Map();
}
/**
* @param {Driver} driver
* @param {LH.Artifacts.ImageElement} element
* @return {Promise<LH.Artifacts.ImageElement>}
*/
async fetchElementWithSizeInformation(driver, element) {
const url = JSON.stringify(element.src);
if (this._naturalSizeCache.has(url)) {
return Object.assign(element, this._naturalSizeCache.get(url));
}

try {
// We don't want this to take forever, 250ms should be enough for images that are cached
driver.setNextProtocolTimeout(250);
/** @type {{naturalWidth: number, naturalHeight: number}} */
const size = await driver.evaluateAsync(`(${determineNaturalSize.toString()})(${url})`);
this._naturalSizeCache.set(url, size);
return Object.assign(element, size);
} catch (_) {
// determineNaturalSize fails on invalid images, which we treat as non-visible
Expand Down