-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
uses-optimized-images.js
158 lines (142 loc) · 5.48 KB
/
uses-optimized-images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @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 This audit determines if the images used are sufficiently larger
* than Lighthouse optimized versions of the images (as determined by the gatherer).
* Audit will fail if one of the conditions are met:
* * There is at least one JPEG or bitmap image that was larger than canvas encoded JPEG.
* * There is at least one image that would have saved more than 50KB by using WebP.
* * The savings of moving all images to WebP is greater than 100KB.
*/
'use strict';
const Audit = require('../audit');
const URL = require('../../lib/url-shim');
const Formatter = require('../../formatters/formatter');
const KB_IN_BYTES = 1024;
const IGNORE_THRESHOLD_IN_BYTES = 2 * KB_IN_BYTES;
const TOTAL_WASTED_BYTES_THRESHOLD = 100 * KB_IN_BYTES;
const WEBP_ALREADY_OPTIMIZED_THRESHOLD_IN_BYTES = 50 * KB_IN_BYTES;
class UsesOptimizedImages extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Images',
name: 'uses-optimized-images',
description: 'Has optimized images',
helpText: 'Images should be optimized to save network bytes. ' +
'The following images could have smaller file sizes when compressed with ' +
'[WebP](https://developers.google.com/speed/webp/) or JPEG at 80 quality. ' +
'[Learn more about image optimization](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization).',
requiredArtifacts: ['OptimizedImages', 'networkRecords']
};
}
/**
* @param {{originalSize: number, webpSize: number, jpegSize: number}} image
* @param {string} type
* @return {{bytes: number, kb: number, percent: number}}
*/
static computeSavings(image, type) {
const bytes = image.originalSize - image[type + 'Size'];
const kb = Math.round(bytes / KB_IN_BYTES);
const percent = Math.round(100 * bytes / image.originalSize);
return {bytes, kb, percent};
}
/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const networkRecords = artifacts.networkRecords[Audit.DEFAULT_PASS];
return artifacts.requestNetworkThroughput(networkRecords).then(networkThroughput => {
return UsesOptimizedImages.audit_(artifacts, networkThroughput);
});
}
/**
* @param {!Artifacts} artifacts
* @param {number} networkThroughput
* @return {!AuditResult}
*/
static audit_(artifacts, networkThroughput) {
const images = artifacts.OptimizedImages;
const failedImages = [];
let totalWastedBytes = 0;
let hasAllEfficientImages = true;
const results = images.reduce((results, image) => {
if (image.failed) {
failedImages.push(image);
return results;
} else if (image.originalSize < Math.max(IGNORE_THRESHOLD_IN_BYTES, image.webpSize)) {
return results;
}
const originalKb = Math.round(image.originalSize / KB_IN_BYTES);
const webpSavings = UsesOptimizedImages.computeSavings(image, 'webp');
if (webpSavings.bytes > WEBP_ALREADY_OPTIMIZED_THRESHOLD_IN_BYTES) {
hasAllEfficientImages = false;
}
let jpegSavingsLabel;
if (/(jpeg|bmp)/.test(image.mimeType)) {
const jpegSavings = UsesOptimizedImages.computeSavings(image, 'jpeg');
if (jpegSavings.bytes > 0) {
hasAllEfficientImages = false;
jpegSavingsLabel = `${jpegSavings.percent}%`;
}
}
totalWastedBytes += webpSavings.bytes;
results.push({
url: URL.getDisplayName(image.url),
preview: {url: image.url, mimeType: image.mimeType},
total: `${originalKb} KB`,
webpSavings: `${webpSavings.percent}%`,
jpegSavings: jpegSavingsLabel
});
return results;
}, []);
let displayValue = '';
if (totalWastedBytes > 1000) {
const totalWastedKb = Math.round(totalWastedBytes / KB_IN_BYTES);
// Only round to nearest 10ms since we're relatively hand-wavy
const totalWastedMs = Math.round(totalWastedBytes / networkThroughput * 100) * 10;
displayValue = `${totalWastedKb}KB (~${totalWastedMs}ms) potential savings`;
}
let debugString;
if (failedImages.length) {
const urls = failedImages.map(image => URL.getDisplayName(image.url));
debugString = `Lighthouse was unable to decode some of your images: ${urls.join(', ')}`;
}
return UsesOptimizedImages.generateAuditResult({
displayValue,
debugString,
rawValue: hasAllEfficientImages && totalWastedBytes < TOTAL_WASTED_BYTES_THRESHOLD,
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.TABLE,
value: {
results,
tableHeadings: {
preview: '',
url: 'URL',
total: 'Original (KB)',
webpSavings: 'WebP Savings (%)',
jpegSavings: 'JPEG Savings (%)',
}
}
}
});
}
}
module.exports = UsesOptimizedImages;