-
Notifications
You must be signed in to change notification settings - Fork 415
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
Finish adding getINP() #221
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look solid, nice!
Most comments are nits, but I think a few may be actual bugs.
I didn't review or try the unit tests.
src/getINP.ts
Outdated
if (existingInteractionIndex >= 0) { | ||
const interaction = longestInteractions[existingInteractionIndex]; | ||
interaction.latency = Math.max(interaction.latency, entry.duration); | ||
interaction.entries.push(entry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will these be sorted by duration ever?
I think we have sample code that uses .entries[0]
and performance.measure, as a simple way to visualize the latency on timeline, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was an open question I had (what to sort them by). I think (?) we want to keep them in the order they were dispatched and leave any sorting up to the debug code, but let me know if you disagree and think it's important to have the largest duration first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, fair enough.
While I think making the first entry the largest would be reasonable given the current implementation -- my ultimate preference is to report all entries that present the same frame such that we can create a summary of where time was spent overall. (Or even just report the summary as part of the Metric return value).
So leaving it up to debug code sgtm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I very much agree with that ultimate goal. Let's keep it simple for now and re-evaluate once we have more options there or when we plan to release this as stable.
} | ||
const handleEntries = (entries: Metric['entries']) => { | ||
(entries as PerformanceEventTiming[]).forEach((entry) => { | ||
if (entry.interactionId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, we could potentially polyfill for Firefox by assigning an interactionId for the entry here, for events of specifics types. This would include more events than we want, ideally, but could still be useful.
interactionCount estimate would be off, so we could just always return 1.
I'm not sure how much error that approach would have, and how that compares to historical attempts to polyfill features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can experiment.
I did experience some with your earlier polyfill using eventCounts
, and that didn't work in Firefox because (IIRC) it doesn't seem to emit pointercancel
events. I could try this and see how well it works.
src/getINP.ts
Outdated
// The use of 50 here as a balance between not wanting the callback to | ||
// run for too many already fast events (one frame or less), but also | ||
// get enough fidelity below the recommended "good" threshold of 200. | ||
durationThreshold: 50, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Event Timing is rounded to the nearest 8ms. That makes the duration a nice round number (either 48ms or 56ms).
That means that this 50ms durationThreshold is effectively a 56ms duration threshold, which we may want to be explicit about.
TL;DR I think this is a good threshold. But here are some nuances:
- At 60hz, 56ms is 3.5 frames, which means it can effectively get presented at the same time as other entries with 64ms of latency.
- Conversely, if you were to change to 48ms durationThreshold, you really should probably use 40ms to include entries that take 2.5 frames @ 60hz.
- If you wanted to group entries by frame (i.e. by presentation time) you would need to take startTime + duration and then group entries which are within 8ms windows. A single shared presentation time can include entries with a wide range of durations. An entry with less than
durationThreshold
duration can have large processing time. (This is one reason why we may want a dedicated api)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe 40 makes more sense then? I think the spirit of that comment is actually what I'm trying to report on. I.e. missing a frame deadline is probably inevitable for most pages, but once you're missing more than 2 or more frames it's probably worth knowing about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
##### [`v4.2.3](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v423-2024-08-06) - Fix missing LoAF entries in INP attribution ([#512](GoogleChrome/web-vitals#512)) ##### [`v4.2.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v422-2024-07-17) - Fix interaction count after bfcache restore ([#505](GoogleChrome/web-vitals#505)) ##### [`v4.2.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v421-2024-06-30) - Fix compatibility issues with TypeScript v5.5 ([#497](GoogleChrome/web-vitals#497)) ##### [`v4.2.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v420-2024-06-20) - Refactor INP attribution code to fix errors on Windows 10 ([#495](GoogleChrome/web-vitals#495)) ##### [`v4.1.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v411-2024-06-10) - Fix pending LoAF cleanup logic ([#493](GoogleChrome/web-vitals#493)) ##### [`v4.1.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v410-2024-06-06) - Move the support check to the top of the onINP() function ([#490](GoogleChrome/web-vitals#490)) - Fix missing LoAF attribution when entries are dispatched before event entries ([#487](GoogleChrome/web-vitals#487)) ##### [`v4.0.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v401-2024-05-21) - Add the `ReportCallback` type back but deprecate it ([#483](GoogleChrome/web-vitals#483)) ##### [`v4.0.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v400-2024-05-13) - **\[BREAKING]** Update types to support more generic usage ([#471](GoogleChrome/web-vitals#471)) - **\[BREAKING]** Split `waitingDuration` to make it easier to understand redirect delays ([#458](GoogleChrome/web-vitals#458)) - **\[BREAKING]** Rename `TTFBAttribution` fields from `*Time` to `*Duration` ([#453](GoogleChrome/web-vitals#453)) - **\[BREAKING]** Rename `resourceLoadTime` to `resourceLoadDuration` in LCP attribution ([#450](GoogleChrome/web-vitals#450)) - **\[BREAKING]** Add INP breakdown timings and LoAF attribution ([#442](GoogleChrome/web-vitals#442)) - **\[BREAKING]** Deprecate `onFID()` and remove previously deprecated APIs ([#435](GoogleChrome/web-vitals#435)) - Expose the target element in INP attribution ([#479](GoogleChrome/web-vitals#479)) - Save INP target after interactions to reduce null values when removed from the DOM ([#477](GoogleChrome/web-vitals#477)) - Cap TTFB in attribution ([#440](GoogleChrome/web-vitals#440)) - Fix `reportAllChanges` behavior for LCP when library is loaded late ([#468](GoogleChrome/web-vitals#468)) ##### [`v3.5.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v352-2024-01-25) - Pick the first non-null `target` for INP attribution ([#421](GoogleChrome/web-vitals#421)) ##### [`v3.5.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v351-2023-12-27) - Add extra guard for `PerformanceEventTiming` not existing ([#403](GoogleChrome/web-vitals#403)) ##### [`v3.5.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v350-2023-09-28) - Run `onLCP` callback in separate task ([#386](GoogleChrome/web-vitals#386)) - Fix INP durationThreshold bug when set to 0 ([#372](GoogleChrome/web-vitals#372)) - Prevent FID entries being emitted as INP for non-supporting browsers ([#368](GoogleChrome/web-vitals#368)) ##### [`v3.4.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v340-2023-07-11) - Make `bindReporter` generic over metric type ([#359](GoogleChrome/web-vitals#359)) - Update INP status in README ([#362](GoogleChrome/web-vitals#362)) - Fix Metric types for better TypeScript support ([#356](GoogleChrome/web-vitals#356)) - Fix selector for SVGs for attribution build ([#354](GoogleChrome/web-vitals#354)) ##### [`v3.3.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v332-2023-05-29) - Fix attribution types ([#348](GoogleChrome/web-vitals#348)) - Safe access navigation entry type ([#290](GoogleChrome/web-vitals#290)) ##### [`v3.3.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v331-2023-04-04) - Export metric rating thresholds in attribution build as well. ##### [`v3.3.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v330-2023-03-09) - Export metric rating thresholds, add explicit `MetricRatingThresholds` type ([#323](GoogleChrome/web-vitals#323)) - Trim classname selector ([#328](GoogleChrome/web-vitals#328)) - Add link to CrUX versus RUM blog post ([#327](GoogleChrome/web-vitals#327)) - Prevent LCP being reported for hidden prerendered pages ([#326](GoogleChrome/web-vitals#326)) - Add Server Timing information to docs ([#324](GoogleChrome/web-vitals#324)) - Fix link in `onINP()` thresholds comment ([#318](GoogleChrome/web-vitals#318)) - Update web.dev link for `onINP()` ([#307](GoogleChrome/web-vitals#307)) - Add a note about when to load the library ([#305](GoogleChrome/web-vitals#305)) ##### [`v3.1.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v311-2023-01-10) - Defer CLS logic until after `onFCP()` callback ([#297](GoogleChrome/web-vitals#297)) ##### [`v3.1.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v310-2022-11-15) - Add support for `'restore'` as a `navigationType` ([#284](GoogleChrome/web-vitals#284)) - Report initial CLS value when `reportAllChanges` is true ([#283](GoogleChrome/web-vitals#283)) - Defer all observers until after activation ([#282](GoogleChrome/web-vitals#282)) - Ignore TTFB for loads where responseStart is zero ([#281](GoogleChrome/web-vitals#281)) - Defer execution of observer callbacks ([#278](GoogleChrome/web-vitals#278)) ##### [`v3.0.4](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v304-2022-10-18) - Clamp LCP and FCP to 0 for prerendered pages ([#270](GoogleChrome/web-vitals#270)) ##### [`v3.0.3](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v303-2022-10-04) - Ensure `attribution` object is always present in attribution build ([#265](GoogleChrome/web-vitals#265)) ##### [`v3.0.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v302-2022-09-14) - Set an explicit unpkg dist file ([#261](GoogleChrome/web-vitals#261)) ##### [`v3.0.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v301-2022-08-31) - Use the cjs extension for all UMD builds ([#257](GoogleChrome/web-vitals#257)) ##### [`v3.0.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v300-2022-08-24) - **\[BREAKING]** Add a config object param to all metric functions ([#225](GoogleChrome/web-vitals#225)) - **\[BREAKING]** Report TTFB after a bfcache restore ([#220](GoogleChrome/web-vitals#220)) - **\[BREAKING]** Only include last LCP entry in metric entries ([#218](GoogleChrome/web-vitals#218)) - Update the metric ID prefix for v3 ([#251](GoogleChrome/web-vitals#251)) - Move the Navigation Timing API polyfill to the base+polyfill build ([#248](GoogleChrome/web-vitals#248)) - Add a metric rating property ([#246](GoogleChrome/web-vitals#246)) - Add deprecation notices for base+polyfill builds ([#242](GoogleChrome/web-vitals#242)) - Add a new attribution build for debugging issues in the field ([#237](GoogleChrome/web-vitals#237), [#244](GoogleChrome/web-vitals#244)) - Add support for prerendered pages ([#233](GoogleChrome/web-vitals#233)) - Rename the `ReportHandler` type to `ReportCallback`, with alias for back-compat ([#225](GoogleChrome/web-vitals#225), [#227](GoogleChrome/web-vitals#227)) - Add support for the new INP metric ([#221](GoogleChrome/web-vitals#221), [#232](GoogleChrome/web-vitals#232)) - Rename `getXXX()` functions to `onXXX()` ([#222](GoogleChrome/web-vitals#222)) - Add a `navigationType` property to the Metric object ([#219](GoogleChrome/web-vitals#219))
##### [`v4.2.3](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v423-2024-08-06) - Fix missing LoAF entries in INP attribution ([#512](GoogleChrome/web-vitals#512)) ##### [`v4.2.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v422-2024-07-17) - Fix interaction count after bfcache restore ([#505](GoogleChrome/web-vitals#505)) ##### [`v4.2.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v421-2024-06-30) - Fix compatibility issues with TypeScript v5.5 ([#497](GoogleChrome/web-vitals#497)) ##### [`v4.2.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v420-2024-06-20) - Refactor INP attribution code to fix errors on Windows 10 ([#495](GoogleChrome/web-vitals#495)) ##### [`v4.1.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v411-2024-06-10) - Fix pending LoAF cleanup logic ([#493](GoogleChrome/web-vitals#493)) ##### [`v4.1.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v410-2024-06-06) - Move the support check to the top of the onINP() function ([#490](GoogleChrome/web-vitals#490)) - Fix missing LoAF attribution when entries are dispatched before event entries ([#487](GoogleChrome/web-vitals#487)) ##### [`v4.0.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v401-2024-05-21) - Add the `ReportCallback` type back but deprecate it ([#483](GoogleChrome/web-vitals#483)) ##### [`v4.0.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v400-2024-05-13) - **\[BREAKING]** Update types to support more generic usage ([#471](GoogleChrome/web-vitals#471)) - **\[BREAKING]** Split `waitingDuration` to make it easier to understand redirect delays ([#458](GoogleChrome/web-vitals#458)) - **\[BREAKING]** Rename `TTFBAttribution` fields from `*Time` to `*Duration` ([#453](GoogleChrome/web-vitals#453)) - **\[BREAKING]** Rename `resourceLoadTime` to `resourceLoadDuration` in LCP attribution ([#450](GoogleChrome/web-vitals#450)) - **\[BREAKING]** Add INP breakdown timings and LoAF attribution ([#442](GoogleChrome/web-vitals#442)) - **\[BREAKING]** Deprecate `onFID()` and remove previously deprecated APIs ([#435](GoogleChrome/web-vitals#435)) - Expose the target element in INP attribution ([#479](GoogleChrome/web-vitals#479)) - Save INP target after interactions to reduce null values when removed from the DOM ([#477](GoogleChrome/web-vitals#477)) - Cap TTFB in attribution ([#440](GoogleChrome/web-vitals#440)) - Fix `reportAllChanges` behavior for LCP when library is loaded late ([#468](GoogleChrome/web-vitals#468)) ##### [`v3.5.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v352-2024-01-25) - Pick the first non-null `target` for INP attribution ([#421](GoogleChrome/web-vitals#421)) ##### [`v3.5.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v351-2023-12-27) - Add extra guard for `PerformanceEventTiming` not existing ([#403](GoogleChrome/web-vitals#403)) ##### [`v3.5.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v350-2023-09-28) - Run `onLCP` callback in separate task ([#386](GoogleChrome/web-vitals#386)) - Fix INP durationThreshold bug when set to 0 ([#372](GoogleChrome/web-vitals#372)) - Prevent FID entries being emitted as INP for non-supporting browsers ([#368](GoogleChrome/web-vitals#368)) ##### [`v3.4.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v340-2023-07-11) - Make `bindReporter` generic over metric type ([#359](GoogleChrome/web-vitals#359)) - Update INP status in README ([#362](GoogleChrome/web-vitals#362)) - Fix Metric types for better TypeScript support ([#356](GoogleChrome/web-vitals#356)) - Fix selector for SVGs for attribution build ([#354](GoogleChrome/web-vitals#354)) ##### [`v3.3.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v332-2023-05-29) - Fix attribution types ([#348](GoogleChrome/web-vitals#348)) - Safe access navigation entry type ([#290](GoogleChrome/web-vitals#290)) ##### [`v3.3.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v331-2023-04-04) - Export metric rating thresholds in attribution build as well. ##### [`v3.3.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v330-2023-03-09) - Export metric rating thresholds, add explicit `MetricRatingThresholds` type ([#323](GoogleChrome/web-vitals#323)) - Trim classname selector ([#328](GoogleChrome/web-vitals#328)) - Add link to CrUX versus RUM blog post ([#327](GoogleChrome/web-vitals#327)) - Prevent LCP being reported for hidden prerendered pages ([#326](GoogleChrome/web-vitals#326)) - Add Server Timing information to docs ([#324](GoogleChrome/web-vitals#324)) - Fix link in `onINP()` thresholds comment ([#318](GoogleChrome/web-vitals#318)) - Update web.dev link for `onINP()` ([#307](GoogleChrome/web-vitals#307)) - Add a note about when to load the library ([#305](GoogleChrome/web-vitals#305)) ##### [`v3.1.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v311-2023-01-10) - Defer CLS logic until after `onFCP()` callback ([#297](GoogleChrome/web-vitals#297)) ##### [`v3.1.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v310-2022-11-15) - Add support for `'restore'` as a `navigationType` ([#284](GoogleChrome/web-vitals#284)) - Report initial CLS value when `reportAllChanges` is true ([#283](GoogleChrome/web-vitals#283)) - Defer all observers until after activation ([#282](GoogleChrome/web-vitals#282)) - Ignore TTFB for loads where responseStart is zero ([#281](GoogleChrome/web-vitals#281)) - Defer execution of observer callbacks ([#278](GoogleChrome/web-vitals#278)) ##### [`v3.0.4](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v304-2022-10-18) - Clamp LCP and FCP to 0 for prerendered pages ([#270](GoogleChrome/web-vitals#270)) ##### [`v3.0.3](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v303-2022-10-04) - Ensure `attribution` object is always present in attribution build ([#265](GoogleChrome/web-vitals#265)) ##### [`v3.0.2](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v302-2022-09-14) - Set an explicit unpkg dist file ([#261](GoogleChrome/web-vitals#261)) ##### [`v3.0.1](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v301-2022-08-31) - Use the cjs extension for all UMD builds ([#257](GoogleChrome/web-vitals#257)) ##### [`v3.0.0](https://github.com/GoogleChrome/web-vitals/blob/HEAD/CHANGELOG.md#v300-2022-08-24) - **\[BREAKING]** Add a config object param to all metric functions ([#225](GoogleChrome/web-vitals#225)) - **\[BREAKING]** Report TTFB after a bfcache restore ([#220](GoogleChrome/web-vitals#220)) - **\[BREAKING]** Only include last LCP entry in metric entries ([#218](GoogleChrome/web-vitals#218)) - Update the metric ID prefix for v3 ([#251](GoogleChrome/web-vitals#251)) - Move the Navigation Timing API polyfill to the base+polyfill build ([#248](GoogleChrome/web-vitals#248)) - Add a metric rating property ([#246](GoogleChrome/web-vitals#246)) - Add deprecation notices for base+polyfill builds ([#242](GoogleChrome/web-vitals#242)) - Add a new attribution build for debugging issues in the field ([#237](GoogleChrome/web-vitals#237), [#244](GoogleChrome/web-vitals#244)) - Add support for prerendered pages ([#233](GoogleChrome/web-vitals#233)) - Rename the `ReportHandler` type to `ReportCallback`, with alias for back-compat ([#225](GoogleChrome/web-vitals#225), [#227](GoogleChrome/web-vitals#227)) - Add support for the new INP metric ([#221](GoogleChrome/web-vitals#221), [#232](GoogleChrome/web-vitals#232)) - Rename `getXXX()` functions to `onXXX()` ([#222](GoogleChrome/web-vitals#222)) - Add a `navigationType` property to the Metric object ([#219](GoogleChrome/web-vitals#219))
Fixes #208.
This PR builds on top of what @mmocny added in #213 with the following:
observe
module logic to support processing a list of entries rather than each individually.interactionCount
logic into its own module and rewrites it as a "polyfill" so it won't run if the browser natively supportsinteractionCount
(hopefully coming to Chrome soon).Metric.entries
value to all reported entries from the INP interaction, rather than just the longest. (As discussed we should extend this to all entries in the frame, but we probably need a dedicated API for this first.)durationThreshold
to 50 as a compromise between performance and more accurate duration values at lower percentiles.