-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(browser): Keep the best element name captured for an interaction #24272
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
Open
+116
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const windowListeners = vi.hoisted(() => new Map<string, (event: unknown) => void>()); | ||
| const performanceHandlers = vi.hoisted(() => new Map<string, (data: { entries: unknown[] }) => void>()); | ||
|
|
||
| // `isBrowser()` is false under vitest even with the jsdom environment, and it gates the listeners. | ||
| vi.mock('@sentry/core', async () => { | ||
| const actual = await vi.importActual('@sentry/core'); | ||
| return { ...actual, isBrowser: () => true }; | ||
| }); | ||
|
|
||
| vi.mock('../../src/types', () => ({ | ||
| WINDOW: { | ||
| addEventListener: (type: string, listener: (event: unknown) => void) => windowListeners.set(type, listener), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../src/instrumentation/performanceObserver', async () => { | ||
| const actual = await vi.importActual('../../src/instrumentation/performanceObserver'); | ||
| return { | ||
| ...actual, | ||
| addPerformanceInstrumentationHandler: (type: string, callback: (data: { entries: unknown[] }) => void) => { | ||
| performanceHandlers.set(type, callback); | ||
| return () => undefined; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| /** Each test needs a fresh module: the element name cache is per page, so it's module-level. */ | ||
| async function loadInp() { | ||
| vi.resetModules(); | ||
| return import('../../src/web-vitals/inp'); | ||
| } | ||
|
|
||
| /** A target `htmlTreeAsString` can describe, versus one it cannot (yields `<unknown>`). */ | ||
| function element(id: string): unknown { | ||
| return { tagName: 'DIV', id, nodeType: 1 }; | ||
| } | ||
|
|
||
| /** Names the interaction by feeding an entry whose `target` is gone, forcing the timestamp lookup. */ | ||
| function nameFor(interactionId: number, startTime: number): void { | ||
| performanceHandlers.get('event')?.({ | ||
| entries: [{ entryType: 'event', name: 'click', interactionId, startTime, duration: 100, target: null }], | ||
| }); | ||
| } | ||
|
|
||
| describe('INP element name cache', () => { | ||
| beforeEach(() => { | ||
| windowListeners.clear(); | ||
| performanceHandlers.clear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('keeps the first name when later events of the same interaction describe a different element', async () => { | ||
| const { getCachedInteractionContext, registerInpInteractionListener } = await loadInp(); | ||
| registerInpInteractionListener(); | ||
|
|
||
| // The whole sequence shares a timestamp. The trailing `pointerover` is what the browser fires | ||
| // once the click handler has swapped out the element under the cursor. | ||
| windowListeners.get('click')?.({ target: element('clicked'), timeStamp: 1000 }); | ||
| windowListeners.get('pointerover')?.({ target: element('replacement'), timeStamp: 1000 }); | ||
|
|
||
| nameFor(42, 1000); | ||
|
|
||
| expect(getCachedInteractionContext(42)?.elementName).toBe('div#clicked'); | ||
| }); | ||
|
|
||
| it('replaces an unresolvable name with a real one from the same interaction', async () => { | ||
| const { getCachedInteractionContext, registerInpInteractionListener } = await loadInp(); | ||
| registerInpInteractionListener(); | ||
|
|
||
| // Targets that are not elements cannot be described, and they can come first in the sequence. | ||
| windowListeners.get('pointerover')?.({ target: {}, timeStamp: 2000 }); | ||
| windowListeners.get('click')?.({ target: element('clicked'), timeStamp: 2000 }); | ||
|
|
||
| nameFor(43, 2000); | ||
|
|
||
| expect(getCachedInteractionContext(43)?.elementName).toBe('div#clicked'); | ||
| }); | ||
|
|
||
| it('reports an unresolvable name when nothing in the interaction resolves', async () => { | ||
| const { getCachedInteractionContext, registerInpInteractionListener } = await loadInp(); | ||
| registerInpInteractionListener(); | ||
|
|
||
| windowListeners.get('click')?.({ target: {}, timeStamp: 3000 }); | ||
|
|
||
| nameFor(44, 3000); | ||
|
|
||
| expect(getCachedInteractionContext(44)?.elementName).toBe('<unknown>'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bug: Two separate interactions within the same millisecond can be assigned the same element name, as they round to the same timestamp key in
ELEMENT_NAME_TIMESTAMP_MAP, causing incorrect attribution.Severity: LOW
Suggested Fix
To prevent collisions between different interactions, the cache key should be more specific. Instead of relying solely on a rounded timestamp, consider a composite key that includes the
interactionIdor scope the element name cache on a per-interaction basis. This would ensure that element names from one interaction cannot be accidentally used for another.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.