1- import { browserTracingIntegration as originalBrowserTracingIntegration , WINDOW } from '@sentry/browser' ;
1+ import { browserTracingIntegration as originalBrowserTracingIntegration , startBrowserTracingPageLoadSpan , WINDOW } from '@sentry/browser' ;
22import type { Integration , TransactionSource } from '@sentry/core' ;
3- import { debug , SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core' ;
3+ import { browserPerformanceTimeOrigin , debug , SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core' ;
44import { DEBUG_BUILD } from '../debug-build' ;
55
66/**
@@ -18,35 +18,53 @@ function getMetaContent(metaName: string): string | undefined {
1818export function browserTracingIntegration (
1919 options : Parameters < typeof originalBrowserTracingIntegration > [ 0 ] = { } ,
2020) : Integration {
21- const integration = originalBrowserTracingIntegration ( options ) ;
21+ const integration = originalBrowserTracingIntegration ( { ... options , instrumentPageLoad : false } ) ;
2222
2323 return {
2424 ...integration ,
25- setup ( client ) {
26- // Original integration setup call
27- integration . setup ?.( client ) ;
28-
29- client . on ( 'afterStartPageLoadSpan' , pageLoadSpan => {
30- const routeNameFromMetaTags = getMetaContent ( 'sentry-route-name' ) ;
31-
32- if ( routeNameFromMetaTags ) {
33- let decodedRouteName ;
34- try {
35- decodedRouteName = decodeURIComponent ( routeNameFromMetaTags ) ;
36- } catch {
37- // We ignore errors here, e.g. if the value cannot be URL decoded.
38- return ;
39- }
40-
41- DEBUG_BUILD && debug . log ( `[Tracing] Using route name from Sentry HTML meta-tag: ${ decodedRouteName } ` ) ;
42-
43- pageLoadSpan . updateName ( decodedRouteName ) ;
44- pageLoadSpan . setAttributes ( {
45- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' as TransactionSource ,
46- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.pageload.astro' ,
25+ afterAllSetup ( client ) {
26+ // Original integration afterAllSetup call
27+ integration . afterAllSetup ?.( client ) ;
28+
29+ if ( WINDOW . location ) {
30+ if ( options . instrumentPageLoad != false ) {
31+ const origin = browserPerformanceTimeOrigin ( ) ;
32+
33+ const { name, source } = getPageloadSpanName ( ) ;
34+
35+ startBrowserTracingPageLoadSpan ( client , {
36+ name,
37+ // pageload should always start at timeOrigin (and needs to be in s, not ms)
38+ startTime : origin ? origin / 1000 : undefined ,
39+ attributes : {
40+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : source ,
41+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.pageload.astro' ,
42+ } ,
4743 } ) ;
4844 }
49- } ) ;
50- } ,
45+ }
46+ }
47+ } ;
48+ }
49+
50+ function getPageloadSpanName ( ) : { name : string , source : TransactionSource } {
51+ try {
52+ const routeNameFromMetaTags = getMetaContent ( 'sentry-route-name' ) ;
53+ if ( routeNameFromMetaTags ) {
54+ const decodedRouteName = decodeURIComponent ( routeNameFromMetaTags ) ;
55+
56+ DEBUG_BUILD && debug . log ( `[Tracing] Using route name from Sentry HTML meta-tag: ${ decodedRouteName } ` ) ;
57+
58+ return {
59+ name : decodedRouteName ,
60+ source : 'route' ,
61+ } ;
62+ }
63+ } catch {
64+ // fail silently if decoding or reading the meta tag fails
65+ }
66+ return {
67+ name : WINDOW . location . pathname ,
68+ source : 'url' ,
5169 } ;
5270}
0 commit comments