Skip to content

Commit 1417584

Browse files
logaretmclaude
andcommitted
fix(browser): Skip redundant CLS/LCP handlers when span streaming is enabled
When span streaming handles CLS/LCP, `startTrackingWebVitals` no longer registers throwaway `_trackCLS()`/`_trackLCP()` handlers. Instead of adding a separate skip flag, the existing `recordClsStandaloneSpans` and `recordLcpStandaloneSpans` options now accept `undefined` to mean "skip entirely" — three states via two flags instead of three flags. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 90f407a commit 1417584

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,18 @@ let _lcpEntry: LargestContentfulPaint | undefined;
7575
let _clsEntry: LayoutShift | undefined;
7676

7777
interface StartTrackingWebVitalsOptions {
78-
recordClsStandaloneSpans: boolean;
79-
recordLcpStandaloneSpans: boolean;
78+
/**
79+
* When `true`, CLS is tracked as a standalone span. When `false`, CLS is
80+
* recorded as a measurement on the pageload span. When `undefined`, CLS
81+
* tracking is skipped entirely (e.g. because span streaming handles it).
82+
*/
83+
recordClsStandaloneSpans: boolean | undefined;
84+
/**
85+
* When `true`, LCP is tracked as a standalone span. When `false`, LCP is
86+
* recorded as a measurement on the pageload span. When `undefined`, LCP
87+
* tracking is skipped entirely (e.g. because span streaming handles it).
88+
*/
89+
recordLcpStandaloneSpans: boolean | undefined;
8090
client: Client;
8191
}
8292

@@ -97,9 +107,22 @@ export function startTrackingWebVitals({
97107
if (performance.mark) {
98108
WINDOW.performance.mark('sentry-tracing-init');
99109
}
100-
const lcpCleanupCallback = recordLcpStandaloneSpans ? trackLcpAsStandaloneSpan(client) : _trackLCP();
110+
111+
const lcpCleanupCallback =
112+
recordLcpStandaloneSpans === true
113+
? trackLcpAsStandaloneSpan(client)
114+
: recordLcpStandaloneSpans === false
115+
? _trackLCP()
116+
: undefined;
117+
118+
const clsCleanupCallback =
119+
recordClsStandaloneSpans === true
120+
? trackClsAsStandaloneSpan(client)
121+
: recordClsStandaloneSpans === false
122+
? _trackCLS()
123+
: undefined;
124+
101125
const ttfbCleanupCallback = _trackTtfb();
102-
const clsCleanupCallback = recordClsStandaloneSpans ? trackClsAsStandaloneSpan(client) : _trackCLS();
103126

104127
return (): void => {
105128
lcpCleanupCallback?.();

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
522522
const spanStreamingEnabled = hasSpanStreamingEnabled(client);
523523

524524
_collectWebVitals = startTrackingWebVitals({
525-
recordClsStandaloneSpans: !spanStreamingEnabled && (enableStandaloneClsSpans || false),
526-
recordLcpStandaloneSpans: !spanStreamingEnabled && (enableStandaloneLcpSpans || false),
525+
recordClsStandaloneSpans: spanStreamingEnabled ? undefined : enableStandaloneClsSpans || false,
526+
recordLcpStandaloneSpans: spanStreamingEnabled ? undefined : enableStandaloneLcpSpans || false,
527527
client,
528528
});
529529

0 commit comments

Comments
 (0)