Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure one-time event listeners only run once #538

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/lib/onHidden.ts

This file was deleted.

6 changes: 1 addition & 5 deletions src/lib/whenIdle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* limitations under the License.
*/

import {onHidden} from './onHidden.js';
import {runOnce} from './runOnce.js';

/**
* Runs the passed callback during the next idle period, or immediately
* if the browser's visibility state is (or becomes) hidden.
Expand All @@ -25,14 +22,13 @@ export const whenIdle = (cb: () => void): number => {
const rIC = globalThis.requestIdleCallback || setTimeout;

let handle = -1;
cb = runOnce(cb);
// If the document is hidden, run the callback immediately, otherwise
// race an idle callback with the next `visibilitychange` event.
if (document.visibilityState === 'hidden') {
cb();
} else {
handle = rIC(cb);
onHidden(cb);
document.addEventListener('visibilitychange', cb, {once: true});
}
return handle;
};
9 changes: 5 additions & 4 deletions src/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {initMetric} from './lib/initMetric.js';
import {observe} from './lib/observe.js';
import {bindReporter} from './lib/bindReporter.js';
import {doubleRAF} from './lib/doubleRAF.js';
import {onHidden} from './lib/onHidden.js';
import {runOnce} from './lib/runOnce.js';
import {onFCP} from './onFCP.js';
import {CLSMetric, MetricRatingThresholds, ReportOpts} from './types.js';
Expand Down Expand Up @@ -107,9 +106,11 @@ export const onCLS = (
opts!.reportAllChanges,
);

onHidden(() => {
handleEntries(po.takeRecords() as CLSMetric['entries']);
report(true);
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
handleEntries(po.takeRecords() as CLSMetric['entries']);
report(true);
}
});

// Only report after a bfcache restore if the `PerformanceObserver`
Expand Down
9 changes: 5 additions & 4 deletions src/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
resetInteractions,
} from './lib/interactions.js';
import {observe} from './lib/observe.js';
import {onHidden} from './lib/onHidden.js';
import {initInteractionCountPolyfill} from './lib/polyfills/interactionCountPolyfill.js';
import {whenActivated} from './lib/whenActivated.js';
import {whenIdle} from './lib/whenIdle.js';
Expand Down Expand Up @@ -126,9 +125,11 @@ export const onINP = (
// where the first interaction is less than the `durationThreshold`.
po.observe({type: 'first-input', buffered: true});

onHidden(() => {
handleEntries(po.takeRecords() as INPMetric['entries']);
report(true);
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
handleEntries(po.takeRecords() as INPMetric['entries']);
report(true);
}
});

// Only report after a bfcache restore if the `PerformanceObserver`
Expand Down
17 changes: 9 additions & 8 deletions src/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {getActivationStart} from './lib/getActivationStart.js';
import {getVisibilityWatcher} from './lib/getVisibilityWatcher.js';
import {initMetric} from './lib/initMetric.js';
import {observe} from './lib/observe.js';
import {onHidden} from './lib/onHidden.js';
import {runOnce} from './lib/runOnce.js';
import {whenActivated} from './lib/whenActivated.js';
import {whenIdle} from './lib/whenIdle.js';
Expand Down Expand Up @@ -94,18 +93,20 @@ export const onLCP = (
}
});

// Stop listening after input. 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']) {
// 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 in a setTimeout 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
addEventListener(type, () => whenIdle(stopListening), true);
addEventListener(type, () => whenIdle(stopListening), {
capture: true,
once: true,
});
}

onHidden(stopListening);

// Only report after a bfcache restore if the `PerformanceObserver`
// successfully registered.
onBFCacheRestore((event) => {
Expand Down