Skip to content

Commit

Permalink
Remove the redundant reportedMetricIDs check
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Oct 16, 2024
1 parent 403cb79 commit 10e42db
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import {LCPMetric, MetricRatingThresholds, ReportOpts} from './types.js';
/** Thresholds for LCP. See https://web.dev/articles/lcp#what_is_a_good_lcp_score */
export const LCPThresholds: MetricRatingThresholds = [2500, 4000];

const reportedMetricIDs: Record<string, boolean> = {};

/**
* Calculates the [LCP](https://web.dev/articles/lcp) value for the current page and
* calls the `callback` function once the value is ready (along with the
Expand Down Expand Up @@ -84,24 +82,22 @@ export const onLCP = (
opts!.reportAllChanges,
);

// Ensure this logic only runs once, and wrap it in an idle callback
// so the callback is run in a separate task to avoid extending the
// keyboard/click handler to reduce INP impact.
// https://github.com/GoogleChrome/web-vitals/issues/383
// Ensure this logic only runs once, since it can be triggered from
// any of three different event listeners below.
const stopListening = runOnce(() => {
if (!reportedMetricIDs[metric.id]) {
handleEntries(po!.takeRecords() as LCPMetric['entries']);
po!.disconnect();
reportedMetricIDs[metric.id] = true;
report(true);
}
handleEntries(po!.takeRecords() as LCPMetric['entries']);
po!.disconnect();
report(true);
});

// Stop listening after input or visibilitychange.
// Note: while scrolling is an input that stops LCP observation, it's
// unreliable since it can be programmatically generated.
// See: https://github.com/GoogleChrome/web-vitals/issues/75
for (const type of ['keydown', 'click', 'visibilitychange']) {
// Wrap the listener in an idle callback so it's run in a separate
// task to reduce potential INP impact.
// https://github.com/GoogleChrome/web-vitals/issues/383
addEventListener(type, () => whenIdleOrHidden(stopListening), {
capture: true,
once: true,
Expand All @@ -121,7 +117,6 @@ export const onLCP = (

doubleRAF(() => {
metric.value = performance.now() - event.timeStamp;
reportedMetricIDs[metric.id] = true;
report(true);
});
});
Expand Down

0 comments on commit 10e42db

Please sign in to comment.