forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace-elements.js
311 lines (272 loc) · 11.1 KB
/
trace-elements.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* @license Copyright 2020 The Lighthouse Authors. 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';
/* global getNodeDetails */
/**
* @fileoverview
* This gatherer identifies elements that contribrute to metrics in the trace (LCP, CLS, etc.).
* We take the backend nodeId from the trace and use it to find the corresponding element in the DOM.
*/
const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const {resolveNodeIdToObjectId} = require('../driver/dom.js');
const pageFunctions = require('../../lib/page-functions.js');
const RectHelpers = require('../../lib/rect-helpers.js');
const Sentry = require('../../lib/sentry.js');
const Trace = require('./trace.js');
const ProcessedTrace = require('../../computed/processed-trace.js');
const ProcessedNavigation = require('../../computed/processed-navigation.js');
const LighthouseError = require('../../lib/lh-error.js');
/** @typedef {{nodeId: number, score?: number, animations?: {name?: string, failureReasonsMask?: number, unsupportedProperties?: string[]}[]}} TraceElementData */
/**
* @this {HTMLElement}
*/
/* c8 ignore start */
function getNodeDetailsData() {
const elem = this.nodeType === document.ELEMENT_NODE ? this : this.parentElement; // eslint-disable-line no-undef
let traceElement;
if (elem) {
// @ts-expect-error - getNodeDetails put into scope via stringification
traceElement = {node: getNodeDetails(elem)};
}
return traceElement;
}
/* c8 ignore stop */
class TraceElements extends FRGatherer {
/** @type {LH.Gatherer.GathererMeta<'Trace'>} */
meta = {
supportedModes: ['timespan', 'navigation'],
dependencies: {Trace: Trace.symbol},
};
/** @type {Map<string, string>} */
animationIdToName = new Map();
constructor() {
super();
this._onAnimationStarted = this._onAnimationStarted.bind(this);
}
/** @param {LH.Crdp.Animation.AnimationStartedEvent} args */
_onAnimationStarted({animation: {id, name}}) {
if (name) this.animationIdToName.set(id, name);
}
/**
* @param {Array<number>} rect
* @return {LH.Artifacts.Rect}
*/
static traceRectToLHRect(rect) {
const rectArgs = {
x: rect[0],
y: rect[1],
width: rect[2],
height: rect[3],
};
return RectHelpers.addRectTopAndBottom(rectArgs);
}
/**
* This function finds the top (up to 5) elements that contribute to the CLS score of the page.
* Each layout shift event has a 'score' which is the amount added to the CLS as a result of the given shift(s).
* We calculate the score per element by taking the 'score' of each layout shift event and
* distributing it between all the nodes that were shifted, proportianal to the impact region of
* each shifted element.
* @param {Array<LH.TraceEvent>} mainThreadEvents
* @return {Array<TraceElementData>}
*/
static getTopLayoutShiftElements(mainThreadEvents) {
/** @type {Map<number, number>} */
const clsPerNode = new Map();
const shiftEvents = mainThreadEvents
.filter(e => e.name === 'LayoutShift')
.map(e => e.args?.data);
const indexFirstEventWithoutInput =
shiftEvents.findIndex(event => event && !event.had_recent_input);
shiftEvents.forEach((event, index) => {
if (!event || !event.impacted_nodes || !event.score) {
return;
}
// Ignore events with input, unless it's one of the initial events.
// See comment in computed/metrics/cumulative-layout-shift.js.
if (indexFirstEventWithoutInput !== -1 && index >= indexFirstEventWithoutInput) {
if (event.had_recent_input) return;
}
let totalAreaOfImpact = 0;
/** @type {Map<number, number>} */
const pixelsMovedPerNode = new Map();
event.impacted_nodes.forEach(node => {
if (!node.node_id || !node.old_rect || !node.new_rect) {
return;
}
const oldRect = TraceElements.traceRectToLHRect(node.old_rect);
const newRect = TraceElements.traceRectToLHRect(node.new_rect);
const areaOfImpact = RectHelpers.getRectArea(oldRect) +
RectHelpers.getRectArea(newRect) -
RectHelpers.getRectOverlapArea(oldRect, newRect);
pixelsMovedPerNode.set(node.node_id, areaOfImpact);
totalAreaOfImpact += areaOfImpact;
});
for (const [nodeId, pixelsMoved] of pixelsMovedPerNode.entries()) {
let clsContribution = clsPerNode.get(nodeId) || 0;
clsContribution += (pixelsMoved / totalAreaOfImpact) * event.score;
clsPerNode.set(nodeId, clsContribution);
}
});
const topFive = [...clsPerNode.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
.map(([nodeId, clsContribution]) => {
return {
nodeId: nodeId,
score: clsContribution,
};
});
return topFive;
}
/**
* Find the node ids of elements which are animated using the Animation trace events.
* @param {Array<LH.TraceEvent>} mainThreadEvents
* @return {Promise<Array<TraceElementData>>}
*/
async getAnimatedElements(mainThreadEvents) {
/** @type {Map<string, {begin: LH.TraceEvent | undefined, status: LH.TraceEvent | undefined}>} */
const animationPairs = new Map();
for (const event of mainThreadEvents) {
if (event.name !== 'Animation') continue;
if (!event.id2 || !event.id2.local) continue;
const local = event.id2.local;
const pair = animationPairs.get(local) || {begin: undefined, status: undefined};
if (event.ph === 'b') {
pair.begin = event;
} else if (
event.ph === 'n' &&
event.args.data &&
event.args.data.compositeFailed !== undefined) {
pair.status = event;
}
animationPairs.set(local, pair);
}
/** @type {Map<number, Set<{animationId: string, failureReasonsMask?: number, unsupportedProperties?: string[]}>>} */
const elementAnimations = new Map();
for (const {begin, status} of animationPairs.values()) {
const nodeId = begin?.args?.data?.nodeId;
const animationId = begin?.args?.data?.id;
const failureReasonsMask = status?.args?.data?.compositeFailed;
const unsupportedProperties = status?.args?.data?.unsupportedProperties;
if (!nodeId || !animationId) continue;
const animationIds = elementAnimations.get(nodeId) || new Set();
animationIds.add({animationId, failureReasonsMask, unsupportedProperties});
elementAnimations.set(nodeId, animationIds);
}
/** @type {Array<TraceElementData>} */
const animatedElementData = [];
for (const [nodeId, animationIds] of elementAnimations) {
const animations = [];
for (const {animationId, failureReasonsMask, unsupportedProperties} of animationIds) {
const animationName = this.animationIdToName.get(animationId);
animations.push({name: animationName, failureReasonsMask, unsupportedProperties});
}
animatedElementData.push({nodeId, animations});
}
return animatedElementData;
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
*/
async startInstrumentation(context) {
await context.driver.defaultSession.sendCommand('Animation.enable');
context.driver.defaultSession.on('Animation.animationStarted', this._onAnimationStarted);
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
*/
async stopInstrumentation(context) {
context.driver.defaultSession.off('Animation.animationStarted', this._onAnimationStarted);
await context.driver.defaultSession.sendCommand('Animation.disable');
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @param {LH.Trace|undefined} trace
* @return {Promise<LH.Artifacts['TraceElements']>}
*/
async _getArtifact(context, trace) {
const session = context.driver.defaultSession;
if (!trace) {
throw new Error('Trace is missing!');
}
const processedTrace = await ProcessedTrace.request(trace, context);
const {largestContentfulPaintEvt} = await ProcessedNavigation
.request(processedTrace, context)
.catch(err => {
// If we were running in timespan mode and there was no paint, treat LCP as missing.
if (context.gatherMode === 'timespan' && err.code === LighthouseError.errors.NO_FCP.code) {
return {largestContentfulPaintEvt: undefined};
}
throw err;
});
const {mainThreadEvents} = processedTrace;
const lcpNodeId = largestContentfulPaintEvt?.args?.data?.nodeId;
const clsNodeData = TraceElements.getTopLayoutShiftElements(mainThreadEvents);
const animatedElementData =
await this.getAnimatedElements(mainThreadEvents);
/** @type {Map<string, TraceElementData[]>} */
const backendNodeDataMap = new Map([
['largest-contentful-paint', lcpNodeId ? [{nodeId: lcpNodeId}] : []],
['layout-shift', clsNodeData],
['animation', animatedElementData],
]);
const traceElements = [];
for (const [traceEventType, backendNodeData] of backendNodeDataMap) {
for (let i = 0; i < backendNodeData.length; i++) {
const backendNodeId = backendNodeData[i].nodeId;
let response;
try {
const objectId = await resolveNodeIdToObjectId(session, backendNodeId);
if (!objectId) continue;
response = await session.sendCommand('Runtime.callFunctionOn', {
objectId,
functionDeclaration: `function () {
${getNodeDetailsData.toString()};
${pageFunctions.getNodeDetailsString};
return getNodeDetailsData.call(this);
}`,
returnByValue: true,
awaitPromise: true,
});
} catch (err) {
Sentry.captureException(err, {
tags: {gatherer: this.name},
level: 'error',
});
continue;
}
if (response?.result?.value) {
traceElements.push({
traceEventType,
...response.result.value,
score: backendNodeData[i].score,
animations: backendNodeData[i].animations,
nodeId: backendNodeId,
});
}
}
}
return traceElements;
}
/**
* @param {LH.Gatherer.FRTransitionalContext<'Trace'>} context
* @return {Promise<LH.Artifacts.TraceElement[]>}
*/
async getArtifact(context) {
return this._getArtifact(context, context.dependencies.Trace);
}
/**
* @param {LH.Gatherer.PassContext} passContext
* @param {LH.Gatherer.LoadData} loadData
* @return {Promise<LH.Artifacts.TraceElement[]>}
*/
async afterPass(passContext, loadData) {
const context = {...passContext, dependencies: {}};
await this.stopInstrumentation(context);
return this._getArtifact(context, loadData.trace);
}
}
module.exports = TraceElements;