Skip to content

Commit e6e300b

Browse files
committed
a bit more deduplication
1 parent 557b55f commit e6e300b

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { DEBUG_BUILD } from '../debug-build';
1818
import { addClsInstrumentationHandler } from './instrument';
1919
import type { WebVitalReportEvent } from './utils';
20-
import { msToSec, startStandaloneWebVitalSpan } from './utils';
20+
import { msToSec, startStandaloneWebVitalSpan, supportsWebVital } from './utils';
2121
import { onHidden } from './web-vitals/lib/onHidden';
2222
import { runOnce } from './web-vitals/lib/runOnce';
2323

@@ -35,7 +35,7 @@ export function trackClsAsStandaloneSpan(): void {
3535
let standaloneClsEntry: LayoutShift | undefined;
3636
let pageloadSpanId: string | undefined;
3737

38-
if (!supportsLayoutShift()) {
38+
if (!supportsWebVital('layout-shift')) {
3939
return;
4040
}
4141

@@ -139,11 +139,3 @@ function sendStandaloneClsSpan(
139139
span.end(startTime);
140140
}
141141
}
142-
143-
function supportsLayoutShift(): boolean {
144-
try {
145-
return PerformanceObserver.supportedEntryTypes.includes('layout-shift');
146-
} catch {
147-
return false;
148-
}
149-
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { DEBUG_BUILD } from '../debug-build';
1818
import { addLcpInstrumentationHandler } from './instrument';
1919
import type { WebVitalReportEvent } from './utils';
20-
import { msToSec, startStandaloneWebVitalSpan } from './utils';
20+
import { msToSec, startStandaloneWebVitalSpan, supportsWebVital } from './utils';
2121
import { onHidden } from './web-vitals/lib/onHidden';
2222
import { runOnce } from './web-vitals/lib/runOnce';
2323

@@ -35,7 +35,7 @@ export function trackLcpAsStandaloneSpan(): void {
3535
let standaloneLcpEntry: LargestContentfulPaint | undefined;
3636
let pageloadSpanId: string | undefined;
3737

38-
if (!supportsLargestContentfulPaint()) {
38+
if (!supportsWebVital('largest-contentful-paint')) {
3939
return;
4040
}
4141

@@ -151,11 +151,3 @@ export function _sendStandaloneLcpSpan(
151151
span.end(startTime);
152152
}
153153
}
154-
155-
function supportsLargestContentfulPaint(): boolean {
156-
try {
157-
return PerformanceObserver.supportedEntryTypes.includes('largest-contentful-paint');
158-
} catch {
159-
return false;
160-
}
161-
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,14 @@ export function extractNetworkProtocol(nextHopProtocol: string): { name: string;
170170
}
171171
return { name, version };
172172
}
173+
174+
/**
175+
* Generic support check for web vitals
176+
*/
177+
export function supportsWebVital(entryType: 'layout-shift' | 'largest-contentful-paint'): boolean {
178+
try {
179+
return PerformanceObserver.supportedEntryTypes.includes(entryType);
180+
} catch {
181+
return false;
182+
}
183+
}

0 commit comments

Comments
 (0)