forked from checkinholiday/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uses-rel-preload.js
269 lines (236 loc) · 11.3 KB
/
uses-rel-preload.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
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import UrlUtils from '../lib/url-utils.js';
import {NetworkRequest} from '../lib/network-request.js';
import {Audit} from './audit.js';
import {CriticalRequestChains} from '../computed/critical-request-chains.js';
import * as i18n from '../lib/i18n/i18n.js';
import {MainResource} from '../computed/main-resource.js';
import {PageDependencyGraph} from '../computed/page-dependency-graph.js';
import {LoadSimulator} from '../computed/load-simulator.js';
const UIStrings = {
/** Imperative title of a Lighthouse audit that tells the user to use <link rel=preload> to initiate important network requests earlier during page load. This is displayed in a list of audit titles that Lighthouse generates. */
title: 'Preload key requests',
/** Description of a Lighthouse audit that tells the user *why* they should preload important network requests. The associated network requests are started halfway through pageload (or later) but should be started at the beginning. This is displayed after a user expands the section to see more. No character length limits. '<link rel=preload>' is the html code the user would include in their page and shouldn't be translated. The last sentence starting with 'Learn' becomes link text to additional documentation. */
description: 'Consider using `<link rel=preload>` to prioritize fetching resources that are ' +
'currently requested later in page load. ' +
'[Learn how to preload key requests](https://developer.chrome.com/docs/lighthouse/performance/uses-rel-preload/).',
/**
* @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 preload links.
* @example {https://example.com} preloadURL
* */
crossoriginWarning: 'A preload `<link>` was found for "{preloadURL}" but was not used ' +
'by the browser. Check that you are using the `crossorigin` attribute properly.',
};
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
const THRESHOLD_IN_MS = 100;
class UsesRelPreloadAudit extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'uses-rel-preload',
title: str_(UIStrings.title),
description: str_(UIStrings.description),
supportedModes: ['navigation'],
guidanceLevel: 3,
requiredArtifacts: ['devtoolsLogs', 'traces', 'URL'],
scoreDisplayMode: Audit.SCORING_MODES.METRIC_SAVINGS,
};
}
/**
* @param {LH.Artifacts.NetworkRequest} mainResource
* @param {LH.Gatherer.Simulation.GraphNode} graph
* @return {Set<string>}
*/
static getURLsToPreload(mainResource, graph) {
/** @type {Set<string>} */
const urls = new Set();
graph.traverse((node, traversalPath) => {
if (node.type !== 'network') return;
// Don't include the node itself or any CPU nodes in the initiatorPath
const path = traversalPath.slice(1).filter(initiator => initiator.type === 'network');
if (!UsesRelPreloadAudit.shouldPreloadRequest(node.record, mainResource, path)) return;
urls.add(node.record.url);
});
return urls;
}
/**
* Finds which URLs were attempted to be preloaded, but failed to be reused and were requested again.
*
* @param {LH.Gatherer.Simulation.GraphNode} graph
* @return {Set<string>}
*/
static getURLsFailedToPreload(graph) {
/** @type {Array<LH.Artifacts.NetworkRequest>} */
const requests = [];
graph.traverse(node => node.type === 'network' && requests.push(node.record));
const preloadRequests = requests.filter(req => req.isLinkPreload);
const preloadURLsByFrame = new Map();
for (const request of preloadRequests) {
const preloadURLs = preloadURLsByFrame.get(request.frameId) || new Set();
preloadURLs.add(request.url);
preloadURLsByFrame.set(request.frameId, preloadURLs);
}
// A failed preload attempt will manifest as a URL that was requested twice within the same frame.
// Once with `isLinkPreload` AND again without `isLinkPreload` but not hitting the cache.
const duplicateRequestsAfterPreload = requests.filter(request => {
const preloadURLsForFrame = preloadURLsByFrame.get(request.frameId);
if (!preloadURLsForFrame) return false;
if (!preloadURLsForFrame.has(request.url)) return false;
const fromCache = request.fromDiskCache ||
request.fromMemoryCache ||
request.fromPrefetchCache;
return !fromCache && !request.isLinkPreload;
});
return new Set(duplicateRequestsAfterPreload.map(req => req.url));
}
/**
* We want to preload all first party critical requests at depth 2.
* Third party requests can be tricky to know the URL ahead of time.
* Critical requests at depth 1 would already be identified by the browser for preloading.
* Critical requests deeper than depth 2 are more likely to be a case-by-case basis such that it
* would be a little risky to recommend blindly.
*
* @param {LH.Artifacts.NetworkRequest} request
* @param {LH.Artifacts.NetworkRequest} mainResource
* @param {Array<LH.Gatherer.Simulation.GraphNode>} initiatorPath
* @return {boolean}
*/
static shouldPreloadRequest(request, mainResource, initiatorPath) {
const mainResourceDepth = mainResource.redirects ? mainResource.redirects.length : 0;
// If it's already preloaded, no need to recommend it.
if (request.isLinkPreload) return false;
// It's not critical, don't recommend it.
if (!CriticalRequestChains.isCritical(request, mainResource)) return false;
// It's not a request loaded over the network, don't recommend it.
if (NetworkRequest.isNonNetworkRequest(request)) return false;
// It's not at the right depth, don't recommend it.
if (initiatorPath.length !== mainResourceDepth + 2) return false;
// It's not a request for the main frame, it wouldn't get reused even if you did preload it.
if (request.frameId !== mainResource.frameId) return false;
// We survived everything else, just check that it's a first party request.
return UrlUtils.rootDomainsMatch(request.url, mainResource.url);
}
/**
* Computes the estimated effect of preloading all the resources.
* @param {Set<string>} urls The array of byte savings results per resource
* @param {LH.Gatherer.Simulation.GraphNode} graph
* @param {LH.Gatherer.Simulation.Simulator} simulator
* @return {{wastedMs: number, results: Array<{url: string, wastedMs: number}>}}
*/
static computeWasteWithGraph(urls, graph, simulator) {
if (!urls.size) {
return {wastedMs: 0, results: []};
}
// Preload changes the ordering of requests, simulate the original graph with flexible ordering
// to have a reasonable baseline for comparison.
const simulationBeforeChanges = simulator.simulate(graph, {flexibleOrdering: true});
const modifiedGraph = graph.cloneWithRelationships();
/** @type {Array<LH.Gatherer.Simulation.GraphNetworkNode>} */
const nodesToPreload = [];
/** @type {LH.Gatherer.Simulation.GraphNode|null} */
let mainDocumentNode = null;
modifiedGraph.traverse(node => {
if (node.type !== 'network') return;
if (node.isMainDocument()) {
mainDocumentNode = node;
} else if (node.record && urls.has(node.record.url)) {
nodesToPreload.push(node);
}
});
if (!mainDocumentNode) {
// Should always find the main document node
throw new Error('Could not find main document node');
}
// Preload has the effect of moving the resource's only dependency to the main HTML document
// Remove all dependencies of the nodes
for (const node of nodesToPreload) {
node.removeAllDependencies();
node.addDependency(mainDocumentNode);
}
// Once we've modified the dependencies, simulate the new graph with flexible ordering.
const simulationAfterChanges = simulator.simulate(modifiedGraph, {flexibleOrdering: true});
const originalNodesByRecord = Array.from(simulationBeforeChanges.nodeTimings.keys())
// @ts-expect-error we don't care if all nodes without a record collect on `undefined`
.reduce((map, node) => map.set(node.record, node), new Map());
const results = [];
for (const node of nodesToPreload) {
const originalNode = originalNodesByRecord.get(node.record);
const timingAfter = simulationAfterChanges.nodeTimings.get(node);
const timingBefore = simulationBeforeChanges.nodeTimings.get(originalNode);
if (!timingBefore || !timingAfter) throw new Error('Missing preload node');
const wastedMs = Math.round(timingBefore.endTime - timingAfter.endTime);
if (wastedMs < THRESHOLD_IN_MS) continue;
results.push({url: node.record.url, wastedMs});
}
if (!results.length) {
return {wastedMs: 0, results};
}
return {
// Preload won't necessarily impact the deepest chain/overall time
// We'll use the maximum endTime improvement for now
wastedMs: Math.max(...results.map(item => item.wastedMs)),
results,
};
}
/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit_(artifacts, context) {
const trace = artifacts.traces[UsesRelPreloadAudit.DEFAULT_PASS];
const devtoolsLog = artifacts.devtoolsLogs[UsesRelPreloadAudit.DEFAULT_PASS];
const URL = artifacts.URL;
const simulatorOptions = {devtoolsLog, settings: context.settings};
const [mainResource, graph, simulator] = await Promise.all([
MainResource.request({devtoolsLog, URL}, context),
PageDependencyGraph.request({trace, devtoolsLog, URL}, context),
LoadSimulator.request(simulatorOptions, context),
]);
const urls = UsesRelPreloadAudit.getURLsToPreload(mainResource, graph);
const {results, wastedMs} = UsesRelPreloadAudit.computeWasteWithGraph(urls, graph, simulator);
// sort results by wastedTime DESC
results.sort((a, b) => b.wastedMs - a.wastedMs);
/** @type {Array<LH.IcuMessage>|undefined} */
let warnings;
const failedURLs = UsesRelPreloadAudit.getURLsFailedToPreload(graph);
if (failedURLs.size) {
warnings = Array.from(failedURLs)
.map(preloadURL => str_(UIStrings.crossoriginWarning, {preloadURL}));
}
/** @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: wastedMs, sortedBy: ['wastedMs']});
return {
score: results.length ? 0 : 1,
numericValue: wastedMs,
numericUnit: 'millisecond',
displayValue: wastedMs ?
str_(i18n.UIStrings.displayValueMsSavings, {wastedMs}) :
'',
details,
warnings,
};
}
/**
* @return {Promise<LH.Audit.Product>}
*/
static async audit() {
// Preload advice is on hold until https://github.com/GoogleChrome/lighthouse/issues/11960
// is resolved.
return {score: 1, notApplicable: true,
details: Audit.makeOpportunityDetails([], [], {overallSavingsMs: 0})};
}
}
export default UsesRelPreloadAudit;
export {UIStrings};