Skip to content

Commit

Permalink
Prevent LCP being reported for hidden prerendered page (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb authored Mar 6, 2023
1 parent 39dbf5d commit 87358fc
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {
const handleEntries = (entries: LCPMetric['entries']) => {
const lastEntry = entries[entries.length - 1] as LargestContentfulPaint;
if (lastEntry) {
// 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. 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) {
metric.value = value;
if (lastEntry.startTime < visibilityWatcher.firstHiddenTime) {
// 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. But in cases
// where `activationStart` occurs after the LCP, this time should be
// clamped at 0.
metric.value = Math.max(
lastEntry.startTime - getActivationStart(),
0
);
metric.entries = [lastEntry];
report();
}
Expand Down

0 comments on commit 87358fc

Please sign in to comment.