forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uses-rel-preconnect.js
284 lines (248 loc) · 12.1 KB
/
uses-rel-preconnect.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {Audit} from './audit.js';
import UrlUtils from '../lib/url-utils.js';
import * as i18n from '../lib/i18n/i18n.js';
import {NetworkRecords} from '../computed/network-records.js';
import {MainResource} from '../computed/main-resource.js';
import {LoadSimulator} from '../computed/load-simulator.js';
import {ProcessedNavigation} from '../computed/processed-navigation.js';
import {PageDependencyGraph} from '../computed/page-dependency-graph.js';
import {LanternLargestContentfulPaint} from '../computed/metrics/lantern-largest-contentful-paint.js';
import {LanternFirstContentfulPaint} from '../computed/metrics/lantern-first-contentful-paint.js';
// Preconnect establishes a "clean" socket. Chrome's socket manager will keep an unused socket
// around for 10s. Meaning, the time delta between processing preconnect a request should be <10s,
// otherwise it's wasted. We add a 5s margin so we are sure to capture all key requests.
// @see https://github.com/GoogleChrome/lighthouse/issues/3106#issuecomment-333653747
const PRECONNECT_SOCKET_MAX_IDLE_IN_MS = 15_000;
const IGNORE_THRESHOLD_IN_MS = 50;
const UIStrings = {
/** Imperative title of a Lighthouse audit that tells the user to connect early to internet domains that will be used to load page resources. Origin is the correct term, however 'domain name' could be used if neccsesary. This is displayed in a list of audit titles that Lighthouse generates. */
title: 'Preconnect to required origins',
/** Description of a Lighthouse audit that tells the user how to connect early to third-party domains that will be used to load page resources. This is displayed after a user expands the section to see more. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */
description:
'Consider adding `preconnect` or `dns-prefetch` resource hints to establish early ' +
'connections to important third-party origins. ' +
'[Learn how to preconnect to required origins](https://developer.chrome.com/docs/lighthouse/performance/uses-rel-preconnect/).',
/**
* @description A warning message that is shown when the user tried to follow the advice of the audit, but it's not working as expected.
* @example {https://example.com} securityOrigin
* */
unusedWarning: 'A `<link rel=preconnect>` was found for "{securityOrigin}" but was not used ' +
'by the browser. Only use `preconnect` for important origins ' +
'that the page will certainly request.',
/**
* @description A warning message that is shown when the user tried to follow the advice of the audit, but it's not working as expected. Forgetting to set the `crossorigin` HTML attribute, or setting it to an incorrect value, on the link is a common mistake when adding preconnect links.
* @example {https://example.com} securityOrigin
* */
crossoriginWarning: 'A `<link rel=preconnect>` was found for "{securityOrigin}" but was not ' +
'used by the browser. Check that you are using the `crossorigin` attribute properly.',
/** A warning message that is shown when found more than 2 preconnected links */
tooManyPreconnectLinksWarning: 'More than 2 `<link rel=preconnect>` connections were found. ' +
'These should be used sparingly and only to the most important origins.',
};
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
class UsesRelPreconnectAudit extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'uses-rel-preconnect',
title: str_(UIStrings.title),
description: str_(UIStrings.description),
supportedModes: ['navigation'],
guidanceLevel: 3,
requiredArtifacts: ['traces', 'devtoolsLogs', 'URL', 'LinkElements'],
scoreDisplayMode: Audit.SCORING_MODES.METRIC_SAVINGS,
};
}
/**
* Check if record has valid timing
* @param {LH.Artifacts.NetworkRequest} record
* @return {boolean}
*/
static hasValidTiming(record) {
return !!record.timing && record.timing.connectEnd >= 0 && record.timing.connectStart >= 0;
}
/**
* Check is the connection is already open
* @param {LH.Artifacts.NetworkRequest} record
* @return {boolean}
*/
static hasAlreadyConnectedToOrigin(record) {
if (!record.timing) return false;
// When these values are given as -1, that means the page has
// a connection for this origin and paid these costs already.
if (
record.timing.dnsStart === -1 && record.timing.dnsEnd === -1 &&
record.timing.connectStart === -1 && record.timing.connectEnd === -1
) {
return true;
}
// Less understood: if the connection setup took no time at all, consider
// it the same as the above. It is unclear if this is correct, or is even possible.
if (
record.timing.dnsEnd - record.timing.dnsStart === 0 &&
record.timing.connectEnd - record.timing.connectStart === 0
) {
return true;
}
return false;
}
/**
* Check is the connection has started before the socket idle time
* @param {LH.Artifacts.NetworkRequest} record
* @param {LH.Artifacts.NetworkRequest} mainResource
* @return {boolean}
*/
static socketStartTimeIsBelowThreshold(record, mainResource) {
const timeSinceMainEnd = Math.max(0, record.networkRequestTime - mainResource.networkEndTime);
return timeSinceMainEnd < PRECONNECT_SOCKET_MAX_IDLE_IN_MS;
}
/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const trace = artifacts.traces[UsesRelPreconnectAudit.DEFAULT_PASS];
const devtoolsLog = artifacts.devtoolsLogs[UsesRelPreconnectAudit.DEFAULT_PASS];
const settings = context.settings;
let maxWastedLcp = 0;
let maxWastedFcp = 0;
/** @type {Array<LH.IcuMessage>} */
const warnings = [];
const [networkRecords, mainResource, loadSimulator, processedNavigation, pageGraph] =
await Promise.all([
NetworkRecords.request(devtoolsLog, context),
MainResource.request({devtoolsLog, URL: artifacts.URL}, context),
LoadSimulator.request({devtoolsLog, settings}, context),
ProcessedNavigation.request(trace, context),
PageDependencyGraph.request({trace, devtoolsLog, URL: artifacts.URL}, context),
]);
const {rtt, additionalRttByOrigin} = loadSimulator.getOptions();
const lcpGraph =
await LanternLargestContentfulPaint.getPessimisticGraph(pageGraph, processedNavigation);
/** @type {Set<string>} */
const lcpGraphURLs = new Set();
lcpGraph.traverse(node => {
if (node.type === 'network') lcpGraphURLs.add(node.record.url);
});
const fcpGraph =
await LanternFirstContentfulPaint.getPessimisticGraph(pageGraph, processedNavigation);
const fcpGraphURLs = new Set();
fcpGraph.traverse(node => {
if (node.type === 'network') fcpGraphURLs.add(node.record.url);
});
/** @type {Map<string, LH.Artifacts.NetworkRequest[]>} */
const origins = new Map();
networkRecords
.forEach(record => {
if (
// Filter out all resources where timing info was invalid.
!UsesRelPreconnectAudit.hasValidTiming(record) ||
// Filter out all resources that are loaded by the document. Connections are already early.
record.initiator.url === mainResource.url ||
// Filter out urls that do not have an origin (data, file, etc).
!record.parsedURL || !record.parsedURL.securityOrigin ||
// Filter out all resources that have the same origin. We're already connected.
mainResource.parsedURL.securityOrigin === record.parsedURL.securityOrigin ||
// Filter out anything that wasn't part of LCP. Only recommend important origins.
!lcpGraphURLs.has(record.url) ||
// Filter out all resources where origins are already resolved.
UsesRelPreconnectAudit.hasAlreadyConnectedToOrigin(record) ||
// Make sure the requests are below the PRECONNECT_SOCKET_MAX_IDLE_IN_MS (15s) mark.
!UsesRelPreconnectAudit.socketStartTimeIsBelowThreshold(record, mainResource)
) {
return;
}
const securityOrigin = record.parsedURL.securityOrigin;
const records = origins.get(securityOrigin) || [];
records.push(record);
origins.set(securityOrigin, records);
});
const preconnectLinks = artifacts.LinkElements.filter(el => el.rel === 'preconnect');
const preconnectOrigins =
new Set(preconnectLinks.map(link => UrlUtils.getOrigin(link.href || '')));
/** @type {Array<{url: string, wastedMs: number}>}*/
let results = [];
origins.forEach(records => {
// Sometimes requests are done simultaneous and the connection has not been made
// chrome will try to connect for each network record, we get the first record
const firstRecordOfOrigin = records.reduce((firstRecord, record) => {
return (record.networkRequestTime < firstRecord.networkRequestTime) ? record : firstRecord;
});
// Skip the origin if we don't have timing information
if (!firstRecordOfOrigin.timing) return;
const securityOrigin = firstRecordOfOrigin.parsedURL.securityOrigin;
// Approximate the connection time with the duration of TCP (+potentially SSL) handshake
// DNS time can be large but can also be 0 if a commonly used origin that's cached, so make
// no assumption about DNS.
const additionalRtt = additionalRttByOrigin.get(securityOrigin) || 0;
let connectionTime = rtt + additionalRtt;
// TCP Handshake will be at least 2 RTTs for TLS connections
if (firstRecordOfOrigin.parsedURL.scheme === 'https') connectionTime = connectionTime * 2;
const timeBetweenMainResourceAndDnsStart =
firstRecordOfOrigin.networkRequestTime -
mainResource.networkEndTime +
firstRecordOfOrigin.timing.dnsStart;
const wastedMs = Math.min(connectionTime, timeBetweenMainResourceAndDnsStart);
if (wastedMs < IGNORE_THRESHOLD_IN_MS) return;
if (preconnectOrigins.has(securityOrigin)) {
// Add a warning for any origin the user tried to preconnect to but failed
warnings.push(str_(UIStrings.crossoriginWarning, {securityOrigin}));
return;
}
maxWastedLcp = Math.max(wastedMs, maxWastedLcp);
if (fcpGraphURLs.has(firstRecordOfOrigin.url)) {
maxWastedFcp = Math.max(wastedMs, maxWastedLcp);
}
results.push({
url: securityOrigin,
wastedMs: wastedMs,
});
});
results = results
.sort((a, b) => b.wastedMs - a.wastedMs);
// Add warnings for any preconnect origins that aren't being used.
for (const origin of preconnectOrigins) {
if (!origin) continue;
if (networkRecords.some(record => origin === record.parsedURL.securityOrigin)) continue;
warnings.push(str_(UIStrings.unusedWarning, {securityOrigin: origin}));
}
// Shortcut early with a pass when the user has already configured preconnect.
// https://twitter.com/_tbansal/status/1197771385172480001
if (preconnectLinks.length >= 2) {
return {
score: 1,
warnings: preconnectLinks.length >= 3 ?
[...warnings, str_(UIStrings.tooManyPreconnectLinksWarning)] : warnings,
metricSavings: {LCP: maxWastedLcp, FCP: maxWastedFcp},
};
}
/** @type {LH.Audit.Details.Opportunity['headings']} */
const headings = [
{key: 'url', valueType: 'url', label: str_(i18n.UIStrings.columnURL)},
{key: 'wastedMs', valueType: 'timespanMs', label: str_(i18n.UIStrings.columnWastedMs)},
];
const details = Audit.makeOpportunityDetails(headings, results,
{overallSavingsMs: maxWastedLcp, sortedBy: ['wastedMs']});
return {
score: results.length ? 0 : 1,
numericValue: maxWastedLcp,
numericUnit: 'millisecond',
displayValue: maxWastedLcp ?
str_(i18n.UIStrings.displayValueMsSavings, {wastedMs: maxWastedLcp}) :
'',
warnings,
details,
metricSavings: {LCP: maxWastedLcp, FCP: maxWastedFcp},
};
}
}
export default UsesRelPreconnectAudit;
export {UIStrings};