-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
lantern-largest-contentful-paint.js
111 lines (98 loc) · 4 KB
/
lantern-largest-contentful-paint.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
/**
* @license Copyright 2019 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.
*/
import {makeComputedArtifact} from '../computed-artifact.js';
import {LanternMetric} from './lantern-metric.js';
import {LighthouseError} from '../../lib/lh-error.js';
import {LanternFirstContentfulPaint} from './lantern-first-contentful-paint.js';
/** @typedef {import('../../lib/dependency-graph/base-node.js').Node} Node */
class LanternLargestContentfulPaint extends LanternMetric {
/**
* @return {LH.Gatherer.Simulation.MetricCoefficients}
*/
static get COEFFICIENTS() {
return {
intercept: 0,
optimistic: 0.5,
pessimistic: 0.5,
};
}
/**
* Low priority image nodes are usually offscreen and very unlikely to be the
* resource that is required for LCP. Our LCP graphs include everything except for these images.
*
* @param {Node} node
* @return {boolean}
*/
static isNotLowPriorityImageNode(node) {
if (node.type !== 'network') return true;
const isImage = node.record.resourceType === 'Image';
const isLowPriority = node.record.priority === 'Low' || node.record.priority === 'VeryLow';
return !isImage || !isLowPriority;
}
/**
* @param {Node} dependencyGraph
* @param {LH.Artifacts.ProcessedNavigation} processedNavigation
* @return {Node}
*/
static getOptimisticGraph(dependencyGraph, processedNavigation) {
const lcp = processedNavigation.timestamps.largestContentfulPaint;
if (!lcp) {
throw new LighthouseError(LighthouseError.errors.NO_LCP);
}
return LanternFirstContentfulPaint.getFirstPaintBasedGraph(
dependencyGraph,
lcp,
LanternLargestContentfulPaint.isNotLowPriorityImageNode
);
}
/**
* @param {Node} dependencyGraph
* @param {LH.Artifacts.ProcessedNavigation} processedNavigation
* @return {Node}
*/
static getPessimisticGraph(dependencyGraph, processedNavigation) {
const lcp = processedNavigation.timestamps.largestContentfulPaint;
if (!lcp) {
throw new LighthouseError(LighthouseError.errors.NO_LCP);
}
return LanternFirstContentfulPaint.getFirstPaintBasedGraph(
dependencyGraph,
lcp,
_ => true,
// For pessimistic LCP we'll include *all* layout nodes
node => node.didPerformLayout()
);
}
/**
* @param {LH.Gatherer.Simulation.Result} simulationResult
* @return {LH.Gatherer.Simulation.Result}
*/
static getEstimateFromSimulation(simulationResult) {
const nodeTimesNotOffscreenImages = Array.from(simulationResult.nodeTimings.entries())
.filter(entry => LanternLargestContentfulPaint.isNotLowPriorityImageNode(entry[0]))
.map(entry => entry[1].endTime);
return {
timeInMs: Math.max(...nodeTimesNotOffscreenImages),
nodeTimings: simulationResult.nodeTimings,
};
}
/**
* @param {LH.Artifacts.MetricComputationDataInput} data
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async compute_(data, context) {
const fcpResult = await LanternFirstContentfulPaint.request(data, context);
const metricResult = await this.computeMetricWithGraphs(data, context);
metricResult.timing = Math.max(metricResult.timing, fcpResult.timing);
return metricResult;
}
}
const LanternLargestContentfulPaintComputed = makeComputedArtifact(
LanternLargestContentfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace', 'URL']
);
export {LanternLargestContentfulPaintComputed as LanternLargestContentfulPaint};