Skip to content

Commit e709375

Browse files
committed
feat: include web-vitals#635
1 parent 14c34d4 commit e709375

File tree

1 file changed

+17
-5
lines changed
  • packages/browser-utils/src/metrics/web-vitals

1 file changed

+17
-5
lines changed

packages/browser-utils/src/metrics/web-vitals/getLCP.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,30 @@ export const onLCP = (onReport: (metric: LCPMetric) => void, opts: ReportOpts =
8888
report(true);
8989
});
9090

91+
// Need a separate wrapper to ensure the `runOnce` function above is
92+
// common for all three functions
93+
const stopListeningWrapper = (event: Event) => {
94+
if (event.isTrusted) {
95+
// Wrap the listener in an idle callback so it's run in a separate
96+
// task to reduce potential INP impact.
97+
// https://github.com/GoogleChrome/web-vitals/issues/383
98+
whenIdleOrHidden(stopListening);
99+
if (WINDOW.document) {
100+
removeEventListener(event.type, stopListeningWrapper, {
101+
capture: true,
102+
});
103+
}
104+
}
105+
};
106+
91107
// Stop listening after input or visibilitychange.
92108
// Note: while scrolling is an input that stops LCP observation, it's
93109
// unreliable since it can be programmatically generated.
94110
// See: https://github.com/GoogleChrome/web-vitals/issues/75
95111
for (const type of ['keydown', 'click', 'visibilitychange']) {
96-
// Wrap the listener in an idle callback so it's run in a separate
97-
// task to reduce potential INP impact.
98-
// https://github.com/GoogleChrome/web-vitals/issues/383
99112
if (WINDOW.document) {
100-
addEventListener(type, () => whenIdleOrHidden(stopListening), {
113+
addEventListener(type, stopListeningWrapper, {
101114
capture: true,
102-
once: true,
103115
});
104116
}
105117
}

0 commit comments

Comments
 (0)