Skip to content

Commit

Permalink
Merge pull request #270 from tunetheweb/prerender-LCP-FCP
Browse files Browse the repository at this point in the history
Clamp LCP and FCP to 0 for prerendered pages
  • Loading branch information
tunetheweb committed Oct 18, 2022
2 parents c334fb7 + 24c309e commit c41fed3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/onFCP.ts
Expand Up @@ -50,8 +50,9 @@ export const onFCP = (onReport: FCPReportCallback, opts?: ReportOpts) => {
if (entry.startTime < visibilityWatcher.firstHiddenTime) {
// The activationStart reference is used because FCP should be
// relative to page activation rather than navigation start if the
// page was prerendered.
metric.value = entry.startTime - getActivationStart();
// page was prerendered. But in cases where `activationStart` occurs
// after the FCP, this time should be clamped at 0.
metric.value = Math.max(entry.startTime - getActivationStart(), 0);
metric.entries.push(entry);
report(true);
}
Expand Down
6 changes: 4 additions & 2 deletions src/onLCP.ts
Expand Up @@ -54,8 +54,10 @@ export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {
// The startTime attribute returns the value of the renderTime if it is
// not 0, and the value of the loadTime otherwise. The activationStart
// reference is used because LCP should be relative to page activation
// rather than navigation start if the page was prerendered.
const value = lastEntry.startTime - getActivationStart();
// rather than navigation start if the page was prerendered. But in cases
// where `activationStart` occurs after the LCP, this time should be
// clamped at 0.
const value = Math.max(lastEntry.startTime - getActivationStart(), 0);

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

0 comments on commit c41fed3

Please sign in to comment.