Skip to content

Commit 1a31ec4

Browse files
authored
ref(browser): Avoid optional chaining (#14457)
We apparently disabled the no-optional-chaining rule in browser-utils, which lead to us including this polyfill. Properly enforcing this rule should (hopefully?) reduce bundle size a bit.
1 parent d9909e0 commit 1a31ec4

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

packages/browser-utils/.eslintrc.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module.exports = {
66
overrides: [
77
{
88
files: ['src/**'],
9-
rules: {
10-
'@sentry-internal/sdk/no-optional-chaining': 'off',
11-
},
9+
rules: {},
1210
},
1311
{
1412
files: ['src/metrics/**'],

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

+18-12
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ export function trackClsAsStandaloneSpan(): void {
6767
setTimeout(() => {
6868
const client = getClient();
6969

70-
const unsubscribeStartNavigation = client?.on('startNavigationSpan', () => {
70+
if (!client) {
71+
return;
72+
}
73+
74+
const unsubscribeStartNavigation = client.on('startNavigationSpan', () => {
7175
_collectClsOnce();
7276
unsubscribeStartNavigation && unsubscribeStartNavigation();
7377
});
@@ -84,15 +88,15 @@ export function trackClsAsStandaloneSpan(): void {
8488
function sendStandaloneClsSpan(clsValue: number, entry: LayoutShift | undefined, pageloadSpanId: string) {
8589
DEBUG_BUILD && logger.log(`Sending CLS span (${clsValue})`);
8690

87-
const startTime = msToSec((browserPerformanceTimeOrigin || 0) + (entry?.startTime || 0));
91+
const startTime = msToSec((browserPerformanceTimeOrigin || 0) + ((entry && entry.startTime) || 0));
8892
const routeName = getCurrentScope().getScopeData().transactionName;
8993

90-
const name = entry ? htmlTreeAsString(entry.sources[0]?.node) : 'Layout shift';
94+
const name = entry ? htmlTreeAsString(entry.sources[0] && entry.sources[0].node) : 'Layout shift';
9195

9296
const attributes: SpanAttributes = dropUndefinedKeys({
9397
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser.cls',
9498
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'ui.webvital.cls',
95-
[SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME]: entry?.duration || 0,
99+
[SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME]: (entry && entry.duration) || 0,
96100
// attach the pageload span id to the CLS span so that we can link them in the UI
97101
'sentry.pageload.span_id': pageloadSpanId,
98102
});
@@ -104,19 +108,21 @@ function sendStandaloneClsSpan(clsValue: number, entry: LayoutShift | undefined,
104108
startTime,
105109
});
106110

107-
span?.addEvent('cls', {
108-
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: '',
109-
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: clsValue,
110-
});
111+
if (span) {
112+
span.addEvent('cls', {
113+
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: '',
114+
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: clsValue,
115+
});
111116

112-
// LayoutShift performance entries always have a duration of 0, so we don't need to add `entry.duration` here
113-
// see: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEntry/duration
114-
span?.end(startTime);
117+
// LayoutShift performance entries always have a duration of 0, so we don't need to add `entry.duration` here
118+
// see: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEntry/duration
119+
span.end(startTime);
120+
}
115121
}
116122

117123
function supportsLayoutShift(): boolean {
118124
try {
119-
return PerformanceObserver.supportedEntryTypes?.includes('layout-shift');
125+
return PerformanceObserver.supportedEntryTypes.includes('layout-shift');
120126
} catch {
121127
return false;
122128
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,14 @@ function _trackINP(): () => void {
112112
startTime,
113113
});
114114

115-
span?.addEvent('inp', {
116-
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: 'millisecond',
117-
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: metric.value,
118-
});
115+
if (span) {
116+
span.addEvent('inp', {
117+
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: 'millisecond',
118+
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: metric.value,
119+
});
119120

120-
span?.end(startTime + duration);
121+
span.end(startTime + duration);
122+
}
121123
});
122124
}
123125

packages/browser-utils/src/metrics/web-vitals/getINP.ts

-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ const processEntry = (entry: PerformanceEventTiming) => {
6666
// The least-long of the 10 longest interactions.
6767
const minLongestInteraction = longestInteractionList[longestInteractionList.length - 1];
6868

69-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7069
const existingInteraction = longestInteractionMap[entry.interactionId!];
7170

7271
// Only process the entry if it's possibly one of the ten longest,
@@ -82,7 +81,6 @@ const processEntry = (entry: PerformanceEventTiming) => {
8281
existingInteraction.latency = Math.max(existingInteraction.latency, entry.duration);
8382
} else {
8483
const interaction = {
85-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
8684
id: entry.interactionId!,
8785
latency: entry.duration,
8886
entries: [entry],

packages/browser-utils/test/utils/TestClient.ts

-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ export class TestClient extends BaseClient<TestClientOptions> {
2020
exception: {
2121
values: [
2222
{
23-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2423
type: exception.name,
2524
value: exception.message,
26-
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
2725
},
2826
],
2927
},

0 commit comments

Comments
 (0)