From 66f3938e8d5bb9391f72db3376d2f3de24040585 Mon Sep 17 00:00:00 2001 From: Philip Walton Date: Thu, 6 Jun 2024 11:34:24 -0700 Subject: [PATCH] Move support check to beginning of onINP function (#490) --- src/onINP.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/onINP.ts b/src/onINP.ts index f817287..1770771 100644 --- a/src/onINP.ts +++ b/src/onINP.ts @@ -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 || {}; @@ -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']);