Skip to content

Commit d3ccbaa

Browse files
cursoragentclaude
andcommitted
fix: Eliminate redundant CLS/LCP tracking when span streaming is enabled
When span streaming is enabled, startTrackingWebVitals was registering handlers via _trackCLS() and _trackLCP() that accumulated values in _measurements, which were then immediately deleted in addPerformanceEntries. Meanwhile, trackClsAsSpan() and trackLcpAsSpan() registered their own handlers doing the actual work, resulting in two handlers firing on every CLS/LCP update. This fix adds recordClsOnPageloadSpan and recordLcpOnPageloadSpan parameters to startTrackingWebVitals to control whether _trackCLS() and _trackLCP() should be called. When span streaming is enabled, these are set to false, preventing the redundant handlers from being registered. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 4bf129b commit d3ccbaa

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/browser-utils/src/metrics/browserMetrics.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ let _clsEntry: LayoutShift | undefined;
7777
interface StartTrackingWebVitalsOptions {
7878
recordClsStandaloneSpans: boolean;
7979
recordLcpStandaloneSpans: boolean;
80+
recordClsOnPageloadSpan?: boolean;
81+
recordLcpOnPageloadSpan?: boolean;
8082
client: Client;
8183
}
8284

@@ -89,6 +91,8 @@ interface StartTrackingWebVitalsOptions {
8991
export function startTrackingWebVitals({
9092
recordClsStandaloneSpans,
9193
recordLcpStandaloneSpans,
94+
recordClsOnPageloadSpan = true,
95+
recordLcpOnPageloadSpan = true,
9296
client,
9397
}: StartTrackingWebVitalsOptions): () => void {
9498
const performance = getBrowserPerformanceAPI();
@@ -97,9 +101,21 @@ export function startTrackingWebVitals({
97101
if (performance.mark) {
98102
WINDOW.performance.mark('sentry-tracing-init');
99103
}
100-
const lcpCleanupCallback = recordLcpStandaloneSpans ? trackLcpAsStandaloneSpan(client) : _trackLCP();
104+
let lcpCleanupCallback: (() => void) | undefined;
105+
if (recordLcpStandaloneSpans) {
106+
trackLcpAsStandaloneSpan(client);
107+
} else if (recordLcpOnPageloadSpan) {
108+
lcpCleanupCallback = _trackLCP();
109+
}
110+
101111
const ttfbCleanupCallback = _trackTtfb();
102-
const clsCleanupCallback = recordClsStandaloneSpans ? trackClsAsStandaloneSpan(client) : _trackCLS();
112+
113+
let clsCleanupCallback: (() => void) | undefined;
114+
if (recordClsStandaloneSpans) {
115+
trackClsAsStandaloneSpan(client);
116+
} else if (recordClsOnPageloadSpan) {
117+
clsCleanupCallback = _trackCLS();
118+
}
103119

104120
return (): void => {
105121
lcpCleanupCallback?.();

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
519519
_collectWebVitals = startTrackingWebVitals({
520520
recordClsStandaloneSpans: !spanStreamingEnabled && (enableStandaloneClsSpans || false),
521521
recordLcpStandaloneSpans: !spanStreamingEnabled && (enableStandaloneLcpSpans || false),
522+
recordClsOnPageloadSpan: !spanStreamingEnabled,
523+
recordLcpOnPageloadSpan: !spanStreamingEnabled,
522524
client,
523525
});
524526

0 commit comments

Comments
 (0)