Skip to content

Commit f46520d

Browse files
committed
fix(browser): Keep the first element name captured for an interaction
The INP element-name cache is keyed by the rounded event timestamp and every event of one interaction shares that timestamp, so the last write won. When a handler swaps out the element under the cursor, the browser then fires `pointerover`/`mouseover` for the new element carrying that same timestamp, and those overwrote the cached name with the post-mutation DOM. An INP span for a click that navigated away was named after the new body rather than the element that was clicked. Keeping the first name pins the entry to the element actually interacted with. Chromium 141 happened to round the follow-up events to a different millisecond, which is why this only surfaced on a newer browser.
1 parent 85494a6 commit f46520d

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

  • packages/browser-utils/src/web-vitals

packages/browser-utils/src/web-vitals/inp.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ export function registerInpInteractionListener(): void {
8484
const elementName = htmlTreeAsString(target);
8585
const timestamp = Math.round(event.timeStamp);
8686

87+
// Every event of one interaction shares a timestamp, and so do the `pointerover`/`mouseover`
88+
// the browser fires afterwards when a handler swaps out the element under the cursor. Those
89+
// arrive last and describe the new DOM, so keeping the first name is what pins the entry to the
90+
// element that was actually interacted with.
91+
if (ELEMENT_NAME_TIMESTAMP_MAP.has(timestamp)) {
92+
return;
93+
}
94+
8795
// Store the element name by timestamp so we can match it with the PerformanceEntry
8896
ELEMENT_NAME_TIMESTAMP_MAP.set(timestamp, elementName);
8997

0 commit comments

Comments
 (0)