-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(browser): Source FCP from web-vitals onFCP and rebase FP against activationStart
#23032
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
Merged
Merged
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
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
128 changes: 128 additions & 0 deletions
128
packages/browser-utils/test/web-vitals/tracking-fp-fcp-prerender.test.ts
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,128 @@ | ||
| import { getClient, getMainCarrier, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { addWebVitalsToSpan, startTrackingWebVitals } from '../../src/web-vitals/tracking'; | ||
| import { getDefaultClientOptions, TestClient } from '../utils/TestClient'; | ||
|
|
||
| // Lives in its own file rather than alongside the regular-page-load case: the paint observers, the | ||
| // `instrumented` registry and the visibility watcher are all module-level singletons that can only | ||
| // be armed once, so a second scenario in the same file would reuse the first one's state. | ||
|
|
||
| interface ObserverRegistration { | ||
| callback: (list: PerformanceObserverEntryList) => void; | ||
| buffered: boolean; | ||
| } | ||
|
|
||
| const observers: ObserverRegistration[] = []; | ||
| const emittedEntries: PerformanceEntry[] = []; | ||
|
|
||
| function deliver(callback: (list: PerformanceObserverEntryList) => void, entries: PerformanceEntry[]): void { | ||
| callback({ getEntries: () => entries } as PerformanceObserverEntryList); | ||
| } | ||
|
|
||
| /** | ||
| * Models the two things the real `PerformanceObserver` does that matter here: entries go to every | ||
| * observer already listening, and an observer that registers later with `buffered: true` is replayed | ||
| * the ones it missed. That difference is the point of this test: our paint observer is listening | ||
| * during the prerender, web-vitals' only registers after activation. | ||
| */ | ||
| class MockPerformanceObserver { | ||
| public static supportedEntryTypes = ['paint']; | ||
|
|
||
| private _registration: ObserverRegistration; | ||
|
|
||
| public constructor(callback: (list: PerformanceObserverEntryList) => void) { | ||
| this._registration = { callback, buffered: false }; | ||
| observers.push(this._registration); | ||
| } | ||
|
|
||
| public observe(options?: { buffered?: boolean }): void { | ||
| this._registration.buffered = !!options?.buffered; | ||
|
|
||
| if (this._registration.buffered && emittedEntries.length) { | ||
| deliver(this._registration.callback, emittedEntries); | ||
| } | ||
| } | ||
|
|
||
| public disconnect(): void { | ||
| // noop | ||
| } | ||
| } | ||
|
|
||
| function emitPaintEntries(entries: PerformanceEntry[]): void { | ||
| emittedEntries.push(...entries); | ||
|
|
||
| for (const { callback } of observers) { | ||
| deliver(callback, entries); | ||
| } | ||
| } | ||
|
|
||
| function flush(): Promise<void> { | ||
| return new Promise(resolve => setTimeout(resolve, 0)); | ||
| } | ||
|
|
||
| describe('startTrackingWebVitals', () => { | ||
| const realPerformance = globalThis.performance; | ||
|
|
||
| beforeEach(() => { | ||
| getMainCarrier().__SENTRY__ = undefined; | ||
|
|
||
| const client = new TestClient(getDefaultClientOptions({ tracesSampleRate: 1 })); | ||
| setCurrentClient(client); | ||
| client.init(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('rebases fp and fcp against activationStart for prerendered pages', async () => { | ||
| // The document is still sitting in the prerender buffer, so `activationStart` reads 0. It only | ||
| // becomes the real activation time once the user navigates to the page. | ||
| const navigationEntry = { type: 'navigate', responseStart: 1, activationStart: 0 } as PerformanceNavigationTiming; | ||
| const documentStub = { prerendering: true, readyState: 'complete', visibilityState: 'visible' }; | ||
| const pageListeners: Record<string, Array<() => void>> = {}; | ||
|
|
||
| vi.stubGlobal('PerformanceObserver', MockPerformanceObserver); | ||
| vi.stubGlobal('addEventListener', (type: string, listener: () => void) => { | ||
| (pageListeners[type] ??= []).push(listener); | ||
| }); | ||
| vi.stubGlobal('removeEventListener', vi.fn()); | ||
| vi.stubGlobal('document', documentStub); | ||
| vi.stubGlobal('performance', { | ||
| timeOrigin: realPerformance.timeOrigin, | ||
| now: () => realPerformance.now(), | ||
| getEntries: () => [], | ||
| getEntriesByType: (type: string) => (type === 'navigation' ? [navigationEntry] : []), | ||
| }); | ||
|
|
||
| const cleanupWebVitals = startTrackingWebVitals({ trackCls: false, trackLcp: false, client: getClient()! }); | ||
|
|
||
| // The page paints while it is still prerendering, 5s into the prerender navigation. Our paint | ||
| // observer sees this right away; web-vitals' `onFCP` is still waiting on `prerenderingchange`. | ||
| emitPaintEntries([ | ||
| { entryType: 'paint', name: 'first-paint', duration: 0, startTime: 5012, toJSON: () => ({}) }, | ||
| { entryType: 'paint', name: 'first-contentful-paint', duration: 0, startTime: 5018, toJSON: () => ({}) }, | ||
| ] as PerformanceEntry[]); | ||
| await flush(); | ||
|
|
||
| // The user clicks the link and the page activates 5s into the prerender navigation, so the | ||
| // paints they actually perceived happened 12ms and 18ms after the click. | ||
| documentStub.prerendering = false; | ||
| navigationEntry.activationStart = 5000; | ||
| pageListeners.prerenderingchange?.forEach(listener => listener()); | ||
| await flush(); | ||
|
|
||
| cleanupWebVitals(); | ||
|
|
||
| const pageloadSpan = new SentrySpan({ op: 'pageload', name: '/', sampled: true }); | ||
| addWebVitalsToSpan(pageloadSpan, { | ||
| recordClsOnPageloadSpan: true, | ||
| recordLcpOnPageloadSpan: true, | ||
| spanStreamingEnabled: true, | ||
| }); | ||
|
|
||
| expect(spanToJSON(pageloadSpan).attributes['browser.web_vital.fp.value']).toBe(12); | ||
| expect(spanToJSON(pageloadSpan).attributes['browser.web_vital.fcp.value']).toBe(18); | ||
| }); | ||
| }); |
90 changes: 90 additions & 0 deletions
90
packages/browser-utils/test/web-vitals/tracking-fp-fcp.test.ts
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,90 @@ | ||
| import { getClient, getMainCarrier, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { addWebVitalsToSpan, startTrackingWebVitals } from '../../src/web-vitals/tracking'; | ||
| import { getDefaultClientOptions, TestClient } from '../utils/TestClient'; | ||
|
|
||
| // FCP comes from web-vitals' `onFCP` and FP from our own paint observer, so both register their own | ||
| // `PerformanceObserver`. Every constructed observer is collected here and paint entries are handed | ||
| // to all of them, the way the browser would. | ||
| const paintObserverCallbacks: Array<(list: PerformanceObserverEntryList) => void> = []; | ||
|
|
||
| class MockPerformanceObserver { | ||
| public static supportedEntryTypes = ['paint']; | ||
|
|
||
| public constructor(callback: (list: PerformanceObserverEntryList) => void) { | ||
| paintObserverCallbacks.push(callback); | ||
| } | ||
|
|
||
| public observe(): void { | ||
| // noop | ||
| } | ||
|
|
||
| public disconnect(): void { | ||
| // noop | ||
| } | ||
| } | ||
|
|
||
| function emitPaintEntries(entries: PerformanceEntry[]): Promise<void> { | ||
| for (const callback of paintObserverCallbacks) { | ||
| callback({ getEntries: () => entries } as PerformanceObserverEntryList); | ||
| } | ||
|
|
||
| // Both observers hand off to their handlers in a microtask, so let the queue drain. | ||
| return new Promise(resolve => setTimeout(resolve, 0)); | ||
| } | ||
|
|
||
| describe('startTrackingWebVitals', () => { | ||
| const realPerformance = globalThis.performance; | ||
|
|
||
| beforeEach(() => { | ||
| getMainCarrier().__SENTRY__ = undefined; | ||
|
|
||
| const client = new TestClient(getDefaultClientOptions({ tracesSampleRate: 1 })); | ||
| setCurrentClient(client); | ||
| client.init(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('records fp and fcp on a regular (non-prerendered) page load', async () => { | ||
| vi.stubGlobal('PerformanceObserver', MockPerformanceObserver); | ||
| vi.stubGlobal('addEventListener', vi.fn()); | ||
| vi.stubGlobal('removeEventListener', vi.fn()); | ||
| vi.stubGlobal('document', { | ||
| prerendering: false, | ||
| readyState: 'complete', | ||
| visibilityState: 'visible', | ||
| }); | ||
| vi.stubGlobal('performance', { | ||
| timeOrigin: realPerformance.timeOrigin, | ||
| now: () => realPerformance.now(), | ||
| getEntries: () => [], | ||
| getEntriesByType: (type: string) => | ||
| type === 'navigation' | ||
| ? [{ type: 'navigate', responseStart: 1, activationStart: 0 } as PerformanceNavigationTiming] | ||
| : [], | ||
| }); | ||
|
|
||
| const cleanupWebVitals = startTrackingWebVitals({ trackCls: false, trackLcp: false, client: getClient()! }); | ||
|
|
||
| await emitPaintEntries([ | ||
| { entryType: 'paint', name: 'first-paint', duration: 0, startTime: 12, toJSON: () => ({}) }, | ||
| { entryType: 'paint', name: 'first-contentful-paint', duration: 0, startTime: 18, toJSON: () => ({}) }, | ||
| ] as PerformanceEntry[]); | ||
|
|
||
| cleanupWebVitals(); | ||
|
|
||
| const pageloadSpan = new SentrySpan({ op: 'pageload', name: '/', sampled: true }); | ||
| addWebVitalsToSpan(pageloadSpan, { | ||
| recordClsOnPageloadSpan: true, | ||
| recordLcpOnPageloadSpan: true, | ||
| spanStreamingEnabled: true, | ||
| }); | ||
|
|
||
| expect(spanToJSON(pageloadSpan).attributes['browser.web_vital.fp.value']).toBe(12); | ||
| expect(spanToJSON(pageloadSpan).attributes['browser.web_vital.fcp.value']).toBe(18); | ||
| }); | ||
| }); |
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.
FCP dropped before prerender activation
High Severity
Sourcing
fcpfrom web-vitalsonFCPwaits for page activation before reporting. On a Speculation Rules prerender the pageload span often finishes during the hidden prerender (load plus idle timeout), sofinalizeWebVitalsremoves the handler beforeonFCPruns and First Contentful Paint never reaches the span.Additional Locations (1)
packages/browser-utils/src/instrumentation/performanceObserver.ts#L286-L296Reviewed by Cursor Bugbot for commit b2a1abc. Configure here.