diff --git a/src/lib/onHidden.ts b/src/lib/onHidden.ts index f59d4c90..9a1251b5 100644 --- a/src/lib/onHidden.ts +++ b/src/lib/onHidden.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -export const onHidden = (cb: () => void) => { +export const onHidden = (cb: () => void, options?: boolean | AddEventListenerOptions) => { document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'hidden') { cb(); } - }); + }, options); }; diff --git a/src/lib/whenIdle.ts b/src/lib/whenIdle.ts index 9abb30cd..cbd65c2a 100644 --- a/src/lib/whenIdle.ts +++ b/src/lib/whenIdle.ts @@ -32,7 +32,7 @@ export const whenIdle = (cb: () => void): number => { cb(); } else { handle = rIC(cb); - onHidden(cb); + onHidden(cb, { once: true }); } return handle; }; diff --git a/src/onFID.ts b/src/onFID.ts index cde84b9d..91f2ad7b 100644 --- a/src/onFID.ts +++ b/src/onFID.ts @@ -85,7 +85,7 @@ export const onFID = ( handleEntries(po.takeRecords() as FIDMetric['entries']); po.disconnect(); }), - ); + {once: true}); onBFCacheRestore(() => { metric = initMetric('FID'); diff --git a/src/onLCP.ts b/src/onLCP.ts index 57f0a856..549f1c2c 100644 --- a/src/onLCP.ts +++ b/src/onLCP.ts @@ -110,7 +110,7 @@ export const onLCP = ( }); }); - onHidden(stopListening); + onHidden(stopListening, {once: true}); // Only report after a bfcache restore if the `PerformanceObserver` // successfully registered.