Skip to content

Commit 24c309e

Browse files
committed
Clamp LCP and FCP to 0 for prerendered pages
1 parent ab67c29 commit 24c309e

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/onFCP.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ export const onFCP = (onReport: FCPReportCallback, opts?: ReportOpts) => {
5050
if (entry.startTime < visibilityWatcher.firstHiddenTime) {
5151
// The activationStart reference is used because FCP should be
5252
// relative to page activation rather than navigation start if the
53-
// page was prerendered.
54-
metric.value = entry.startTime - getActivationStart();
53+
// page was prerendered. But in cases where `activationStart` occurs
54+
// after the FCP, this time should be clamped at 0.
55+
metric.value = Math.max(entry.startTime - getActivationStart(), 0);
5556
metric.entries.push(entry);
5657
report(true);
5758
}

src/onLCP.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {
5454
// The startTime attribute returns the value of the renderTime if it is
5555
// not 0, and the value of the loadTime otherwise. The activationStart
5656
// reference is used because LCP should be relative to page activation
57-
// rather than navigation start if the page was prerendered.
58-
const value = lastEntry.startTime - getActivationStart();
57+
// rather than navigation start if the page was prerendered. But in cases
58+
// where `activationStart` occurs after the LCP, this time should be
59+
// clamped at 0.
60+
const value = Math.max(lastEntry.startTime - getActivationStart(), 0);
5961

6062
// Only report if the page wasn't hidden prior to LCP.
6163
if (value < visibilityWatcher.firstHiddenTime) {

0 commit comments

Comments
 (0)