@@ -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 {
8488function 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
117123function supportsLayoutShift ( ) : boolean {
118124 try {
119- return PerformanceObserver . supportedEntryTypes ? .includes ( 'layout-shift' ) ;
125+ return PerformanceObserver . supportedEntryTypes . includes ( 'layout-shift' ) ;
120126 } catch {
121127 return false ;
122128 }
0 commit comments