-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0db7c3
commit f1c36ac
Showing
3 changed files
with
132 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import * as Lantern from '../types/lantern.js'; | ||
import {Metric} from '../metric.js'; | ||
import {LighthouseError} from '../../../lib/lh-error.js'; | ||
import {FirstContentfulPaint} from './first-contentful-paint.js'; | ||
|
||
/** @typedef {import('../base-node.js').Node} Node */ | ||
|
||
class LargestContentfulPaint extends Metric { | ||
/** | ||
* @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 FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, { | ||
cutoffTimestamp: lcp, | ||
treatNodeAsRenderBlocking: LargestContentfulPaint.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 FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, { | ||
cutoffTimestamp: lcp, | ||
treatNodeAsRenderBlocking: _ => true, | ||
// For pessimistic LCP we'll include *all* layout nodes | ||
additionalCpuNodesToTreatAsRenderBlocking: 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 => LargestContentfulPaint.isNotLowPriorityImageNode(entry[0])) | ||
.map(entry => entry[1].endTime); | ||
|
||
return { | ||
timeInMs: Math.max(...nodeTimesNotOffscreenImages), | ||
nodeTimings: simulationResult.nodeTimings, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {Lantern.Simulation.MetricComputationDataInput} data | ||
* @param {Omit<import('../metric.js').Extras, 'optimistic'>=} extras | ||
* @return {Promise<LH.Artifacts.LanternMetric>} | ||
*/ | ||
static async compute(data, extras) { | ||
const fcpResult = extras?.fcpResult; | ||
if (!fcpResult) { | ||
throw new Error('FCP is required to calculate the Interactive metric'); | ||
} | ||
|
||
const metricResult = await super.compute(data, extras); | ||
metricResult.timing = Math.max(metricResult.timing, fcpResult.timing); | ||
return metricResult; | ||
} | ||
} | ||
|
||
export {LargestContentfulPaint}; |
24 changes: 11 additions & 13 deletions
24
.../lantern-largest-contentful-paint-test.js → .../lantern-largest-contentful-paint-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters