-
Notifications
You must be signed in to change notification settings - Fork 443
INP Loses Target Element After SPA Route Change #567
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
Comments
Can you create an example to show the issue here? Chrome does increment interactionids by 7 but FID should be the first interactionId for the page, so odd that it's later. |
I described how to reproduce the problem above. To do this, you need to create a SPA application (I used Vite + React). I generate the INP metric via select with a shift of the block DOM element.
Result :
Why do I think this is a bug: the if we assume that the event that occurred is correctly wiped by |
The problem is reproduced periodically, but steadily. |
I do not use Vite nor React. Can you point me to an existing MVP app that demonstrates the issue? |
I can create the MVP app and give you a link to clone the repo or if you have better idea Im ready to do. |
That works. I'm sure I could figure it out myself, but I always find it's best not to make assumptions and get an exact repro in case |I do something slightly different. |
No problem. I'm glad to help. It happens in several applications. I can't share the private repo, that I work for. But I reproduce the problem for almost default vite + react project. I prepared the repo link to clone Added description to README |
OK the issue is as follows:
If 1 is longer than 2 then you will get a target. On a fast machine they both take similar times (I'm seeing 32ms or 40ms) so they are similar but on occasion one is faster than the other. And depending which is faster you will see the intermittently. If you made the button take longer with some blocking work, then you would consistently see the target. As noted in #477 this saving of the target is not perfect and "will not help if the element is removed before the first event entry from that interactionId is reported so is not a full solution, but will help in some cases". So to solve this we need a similar fix in the spec and then in Chrome as the library cannot do anything further than this. TLDR this is a duplicate of #335 but the library is limited to what it can do here so we're waiting on the fix in Event Timing spec and Chrome to make further process here. |
Would collecting INP with the |
If you're just trying to add a route-URL (for attribution) to match a single page-level INP report, you really just need to know which route was active when the longest interaction happened. You probably can do this by just comparing the timestamp of the event to the timestamps of your route changes (assuming you have a log of these). Note: I would use If you actually want a full collection of interactions per every SPA route, you really are asking for a complete "reset" of collection of interactions. But you don't need to use just the INP metric. You can just measure Event Timing directly-- its quite easy! Then, you can slice and dice and reset as you please. Here is one possible example: function startTrackingLongestInteraction() {
let longestInteraction = null;
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.interactionId && (!longestInteraction || entry.duration > longestInteraction.duration)) {
longestInteraction = entry;
}
}
});
observer.observe({ type: 'event', buffered: true, durationThreshold: 0 });
return function getLongestInteractionAndReset() {
const current = longestInteraction;
longestInteraction = null;
return current;
};
} That's the whole library. Usage: const getLongestInteractionAndReset = startTrackingLongestInteraction();
// ... after time delay or after some interactions
const eventTimingEntry = getLongestInteractionAndReset();
console.log(eventTimingEntry.duration); Then you can try to integrate into your client router (or perhaps just use navigation api). It doesn't offer the same attribution benefits as |
Also web-vitals already tries to get the element details at the time the event timing entry was emitted (even if it doesn’t report it until later, if this flag is off, when the element may have since been removed). It has done this since v4.0.0 and allows us to report However if the element has been removed by the time the event timing entry is emitted, then that won’t be filled in and using So in most cases (and particularly in this example), I would not expect this to mitigate it. The only case where it might help is if your callback references |
First of all: THANK YOU A LOT @mmocny and @tunetheweb for the detailed answers, I really appreciate it 🙏
This is exactly what I'm trying to do, because 80% of our INP events don't have the selector (because in one of the main Preply's pages, a lot of interactions happen inside modals), so we are missing crucial data for our goal of improving the page's INP. In the end, what I'm doing is the following:
I'm sending this to prod today/tomorrow, and I'm more than happy to share the code if someone is trying to solve the same problem 😊 |
Why is Or are you saying you save the details separately as part of your event handler callback (so before event timing is emitted), and then referencing it from the |
You are welcome! If you are interacting with Modals which are being closed as part of the interaction, it might well be that by the time the Event Timing entry is reported to the performance timeline, the event.target element is already removed and GC-ed. I think the only solution at the time is to literally capture the event target as part of the event listener which will close the modal. You can save this target in some metadata map, and then use it as a lookup for your metrics reporting. You can do this because:
Here's an old example of how you might do this: (Notice how |
It makes a lot of sense, thank you!!! FYI: the solution I implemented resulted in a low 25% accuracy in detecting the missing |
How to reproduce
Not always, but systematically, there is a loss of the target element and selector. At the same time,
firstEntryWithTarget?.target == undefined
andinteractionTargetMap
contains an element byinteractionId
, butfirstEntry.interactionId
differs from the one saved ininteractionTargetMap
and is always greater than it by7
The text was updated successfully, but these errors were encountered: