@@ -21,7 +21,6 @@ import {
2121 getActiveSpan ,
2222 getClient ,
2323 getCurrentScope ,
24- getDomElement ,
2524 getDynamicSamplingContextFromSpan ,
2625 getIsolationScope ,
2726 getRootSpan ,
@@ -39,6 +38,12 @@ import { defaultRequestInstrumentationOptions, instrumentOutgoingRequests } from
3938
4039export const BROWSER_TRACING_INTEGRATION_ID = 'BrowserTracing' ;
4140
41+ /**
42+ * This is just a small wrapper that makes `document` optional.
43+ * We want to be extra-safe and always check that this exists, to ensure weird environments do not blow up.
44+ */
45+ const optionalWindowDocument = WINDOW . document as ( typeof WINDOW ) [ 'document' ] | undefined ;
46+
4247interface RouteInfo {
4348 name : string | undefined ;
4449 source : TransactionSource | undefined ;
@@ -273,13 +278,13 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
273278 } ) ;
274279
275280 function emitFinish ( ) : void {
276- if ( [ 'interactive' , 'complete' ] . includes ( WINDOW . document . readyState ) ) {
281+ if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
277282 client . emit ( 'idleSpanEnableAutoFinish' , idleSpan ) ;
278283 }
279284 }
280285
281- if ( isPageloadTransaction && WINDOW . document ) {
282- WINDOW . document . addEventListener ( 'readystatechange' , ( ) => {
286+ if ( isPageloadTransaction && optionalWindowDocument ) {
287+ optionalWindowDocument . addEventListener ( 'readystatechange' , ( ) => {
283288 emitFinish ( ) ;
284289 } ) ;
285290
@@ -462,12 +467,8 @@ export function startBrowserTracingNavigationSpan(client: Client, spanOptions: S
462467
463468/** Returns the value of a meta tag */
464469export function getMetaContent ( metaName : string ) : string | undefined {
465- // Can't specify generic to `getDomElement` because tracing can be used
466- // in a variety of environments, have to disable `no-unsafe-member-access`
467- // as a result.
468- const metaTag = getDomElement ( `meta[name=${ metaName } ]` ) ;
469- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
470- return metaTag ? metaTag . getAttribute ( 'content' ) : undefined ;
470+ const metaTag = optionalWindowDocument && optionalWindowDocument . querySelector ( `meta[name=${ metaName } ]` ) ;
471+ return ( metaTag && metaTag . getAttribute ( 'content' ) ) || undefined ;
471472}
472473
473474/** Start listener for interaction transactions */
@@ -519,7 +520,7 @@ function registerInteractionListener(
519520 ) ;
520521 } ;
521522
522- if ( WINDOW . document ) {
523+ if ( optionalWindowDocument ) {
523524 addEventListener ( 'click' , registerInteractionTransaction , { once : false , capture : true } ) ;
524525 }
525526}
0 commit comments