@@ -33,6 +33,13 @@ interface SentryEnhancedNextData extends NextData {
33
33
} ;
34
34
}
35
35
36
+ interface NextDataTagInfo {
37
+ route ?: string ;
38
+ traceParentData ?: TraceparentData ;
39
+ baggage ?: string ;
40
+ params ?: ParsedUrlQuery ;
41
+ }
42
+
36
43
/**
37
44
* Every Next.js page (static and dynamic ones) comes with a script tag with the id "__NEXT_DATA__". This script tag
38
45
* contains a JSON object with data that was either generated at build time for static pages (`getStaticProps`), or at
@@ -44,14 +51,8 @@ interface SentryEnhancedNextData extends NextData {
44
51
*
45
52
* This function extracts this information.
46
53
*/
47
- function extractNextDataTagInformation ( ) : {
48
- route : string | undefined ;
49
- traceParentData : TraceparentData | undefined ;
50
- baggage : string | undefined ;
51
- params : ParsedUrlQuery | undefined ;
52
- } {
54
+ function extractNextDataTagInformation ( ) : NextDataTagInfo {
53
55
let nextData : SentryEnhancedNextData | undefined ;
54
-
55
56
// Let's be on the safe side and actually check first if there is really a __NEXT_DATA__ script tag on the page.
56
57
// Theoretically this should always be the case though.
57
58
const nextDataTag = global . document . getElementById ( '__NEXT_DATA__' ) ;
@@ -63,27 +64,39 @@ function extractNextDataTagInformation(): {
63
64
}
64
65
}
65
66
66
- // ` nextData.page` always contains the parameterized route
67
- const route = ( nextData || { } ) . page ;
68
- const params = ( nextData || { } ) . query ;
67
+ if ( ! nextData ) {
68
+ return { } ;
69
+ }
69
70
70
- const getInitialPropsBaggage = ( ( nextData || { } ) . props || { } ) . _sentryGetInitialPropsBaggage ;
71
- const getServerSidePropsBaggage = ( ( ( nextData || { } ) . props || { } ) . pageProps || { } ) . _sentryGetServerSidePropsBaggage ;
71
+ const nextDataTagInfo : NextDataTagInfo = { } ;
72
72
73
- const getInitialPropsTraceData = ( ( nextData || { } ) . props || { } ) . _sentryGetInitialPropsTraceData ;
74
- const getServerSidePropsTraceData = ( ( ( nextData || { } ) . props || { } ) . pageProps || { } )
75
- . _sentryGetServerSidePropsTraceData ;
73
+ const { page, query, props } = nextData ;
76
74
77
- // Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` so we give it priority.
78
- const baggage = getInitialPropsBaggage || getServerSidePropsBaggage ;
79
- const traceData = getInitialPropsTraceData || getServerSidePropsTraceData ;
75
+ // `nextData.page` always contains the parameterized route
76
+ nextDataTagInfo . route = page ;
77
+ nextDataTagInfo . params = query ;
78
+
79
+ if ( props ) {
80
+ const { pageProps } = props ;
81
+
82
+ const getInitialPropsBaggage = props . _sentryGetInitialPropsBaggage ;
83
+ const getServerSidePropsBaggage = pageProps && pageProps . _sentryGetServerSidePropsBaggage ;
84
+ // Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` so we give it priority.
85
+ const baggage = getInitialPropsBaggage || getServerSidePropsBaggage ;
86
+ if ( baggage ) {
87
+ nextDataTagInfo . baggage = baggage ;
88
+ }
80
89
81
- return {
82
- route,
83
- params,
84
- traceParentData : traceData ? extractTraceparentData ( traceData ) : undefined ,
85
- baggage,
86
- } ;
90
+ const getInitialPropsTraceData = props . _sentryGetInitialPropsTraceData ;
91
+ const getServerSidePropsTraceData = pageProps && pageProps . _sentryGetServerSidePropsTraceData ;
92
+ // Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` so we give it priority.
93
+ const traceData = getInitialPropsTraceData || getServerSidePropsTraceData ;
94
+ if ( traceData ) {
95
+ nextDataTagInfo . traceParentData = extractTraceparentData ( traceData ) ;
96
+ }
97
+ }
98
+
99
+ return nextDataTagInfo ;
87
100
}
88
101
89
102
const DEFAULT_TAGS = {
0 commit comments