Skip to content

Commit

Permalink
Move support check to beginning of onINP function (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton authored Jun 6, 2024
1 parent b32786b commit 66f3938
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export const onINP = (
onReport: (metric: INPMetric) => void,
opts?: ReportOpts,
) => {
// Return if the browser doesn't support all APIs needed to measure INP.
if (
!(
'PerformanceEventTiming' in self &&
'interactionId' in PerformanceEventTiming.prototype
)
) {
return;
}

// Set defaults
opts = opts || {};

Expand Down Expand Up @@ -104,15 +114,9 @@ export const onINP = (
);

if (po) {
// If browser supports interactionId (and so supports INP), also
// observe entries of type `first-input`. This is useful in cases
// Also observe entries of type `first-input`. This is useful in cases
// where the first interaction is less than the `durationThreshold`.
if (
'PerformanceEventTiming' in self &&
'interactionId' in PerformanceEventTiming.prototype
) {
po.observe({type: 'first-input', buffered: true});
}
po.observe({type: 'first-input', buffered: true});

onHidden(() => {
handleEntries(po.takeRecords() as INPMetric['entries']);
Expand Down

0 comments on commit 66f3938

Please sign in to comment.