Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Apr 26, 2024
1 parent ef00c5d commit a37cbe1
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/onLCP.ts
Expand Up @@ -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);
Expand Down

0 comments on commit a37cbe1

Please sign in to comment.