@@ -67,7 +67,11 @@ export function trackClsAsStandaloneSpan(): void {
67
67
setTimeout ( ( ) => {
68
68
const client = getClient ( ) ;
69
69
70
- const unsubscribeStartNavigation = client ?. on ( 'startNavigationSpan' , ( ) => {
70
+ if ( ! client ) {
71
+ return ;
72
+ }
73
+
74
+ const unsubscribeStartNavigation = client . on ( 'startNavigationSpan' , ( ) => {
71
75
_collectClsOnce ( ) ;
72
76
unsubscribeStartNavigation && unsubscribeStartNavigation ( ) ;
73
77
} ) ;
@@ -84,15 +88,15 @@ export function trackClsAsStandaloneSpan(): void {
84
88
function sendStandaloneClsSpan ( clsValue : number , entry : LayoutShift | undefined , pageloadSpanId : string ) {
85
89
DEBUG_BUILD && logger . log ( `Sending CLS span (${ clsValue } )` ) ;
86
90
87
- const startTime = msToSec ( ( browserPerformanceTimeOrigin || 0 ) + ( entry ? .startTime || 0 ) ) ;
91
+ const startTime = msToSec ( ( browserPerformanceTimeOrigin || 0 ) + ( ( entry && entry . startTime ) || 0 ) ) ;
88
92
const routeName = getCurrentScope ( ) . getScopeData ( ) . transactionName ;
89
93
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' ;
91
95
92
96
const attributes : SpanAttributes = dropUndefinedKeys ( {
93
97
[ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.http.browser.cls' ,
94
98
[ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'ui.webvital.cls' ,
95
- [ SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME ] : entry ? .duration || 0 ,
99
+ [ SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME ] : ( entry && entry . duration ) || 0 ,
96
100
// attach the pageload span id to the CLS span so that we can link them in the UI
97
101
'sentry.pageload.span_id' : pageloadSpanId ,
98
102
} ) ;
@@ -104,19 +108,21 @@ function sendStandaloneClsSpan(clsValue: number, entry: LayoutShift | undefined,
104
108
startTime,
105
109
} ) ;
106
110
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
+ } ) ;
111
116
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
+ }
115
121
}
116
122
117
123
function supportsLayoutShift ( ) : boolean {
118
124
try {
119
- return PerformanceObserver . supportedEntryTypes ? .includes ( 'layout-shift' ) ;
125
+ return PerformanceObserver . supportedEntryTypes . includes ( 'layout-shift' ) ;
120
126
} catch {
121
127
return false ;
122
128
}
0 commit comments