From a37cbe1c766ffddbb67d5473620e7651dd2f3d2b Mon Sep 17 00:00:00 2001 From: Barry Pollard Date: Fri, 26 Apr 2024 09:26:15 +0100 Subject: [PATCH] Review feedback --- src/onLCP.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/onLCP.ts b/src/onLCP.ts index 984793df..b3ad19eb 100644 --- a/src/onLCP.ts +++ b/src/onLCP.ts @@ -60,28 +60,24 @@ export const onLCP = (onReport: LCPReportCallback, opts?: ReportOpts) => { const handleEntries = (entries: LCPMetric['entries']) => { // If reportAllChanges is set then call this function for each entry // As otherwise only want to emit the last one. - if (opts!.reportAllChanges && entries.length > 1) { - entries.forEach((entry) => handleEntries([entry])); + if (!opts!.reportAllChanges) { + entries = entries.slice(-1); } - const lastEntry = entries[entries.length - 1] as LargestContentfulPaint; - if (lastEntry) { + entries.forEach((entry) => { // Only report if the page wasn't hidden prior to LCP. - if (lastEntry.startTime < visibilityWatcher.firstHiddenTime) { + if (entry.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]; + metric.value = Math.max(entry.startTime - getActivationStart(), 0); + metric.entries = [entry]; report(); } - } + }); }; const po = observe('largest-contentful-paint', handleEntries);