Skip to content

Commit 1bde09a

Browse files
committed
Build nextDataTagInfo through mutation
1 parent 0ba0d6e commit 1bde09a

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

packages/nextjs/src/performance/client.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ interface SentryEnhancedNextData extends NextData {
3333
};
3434
}
3535

36+
interface NextDataTagInfo {
37+
route?: string;
38+
traceParentData?: TraceparentData;
39+
baggage?: string;
40+
params?: ParsedUrlQuery;
41+
}
42+
3643
/**
3744
* Every Next.js page (static and dynamic ones) comes with a script tag with the id "__NEXT_DATA__". This script tag
3845
* 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 {
4451
*
4552
* This function extracts this information.
4653
*/
47-
function extractNextDataTagInformation(): {
48-
route: string | undefined;
49-
traceParentData: TraceparentData | undefined;
50-
baggage: string | undefined;
51-
params: ParsedUrlQuery | undefined;
52-
} {
54+
function extractNextDataTagInformation(): NextDataTagInfo {
5355
let nextData: SentryEnhancedNextData | undefined;
54-
5556
// Let's be on the safe side and actually check first if there is really a __NEXT_DATA__ script tag on the page.
5657
// Theoretically this should always be the case though.
5758
const nextDataTag = global.document.getElementById('__NEXT_DATA__');
@@ -63,27 +64,39 @@ function extractNextDataTagInformation(): {
6364
}
6465
}
6566

66-
// `nextData.page` always contains the parameterized route
67-
const route = (nextData || {}).page;
68-
const params = (nextData || {}).query;
67+
if (!nextData) {
68+
return {};
69+
}
6970

70-
const getInitialPropsBaggage = ((nextData || {}).props || {})._sentryGetInitialPropsBaggage;
71-
const getServerSidePropsBaggage = (((nextData || {}).props || {}).pageProps || {})._sentryGetServerSidePropsBaggage;
71+
const nextDataTagInfo: NextDataTagInfo = {};
7272

73-
const getInitialPropsTraceData = ((nextData || {}).props || {})._sentryGetInitialPropsTraceData;
74-
const getServerSidePropsTraceData = (((nextData || {}).props || {}).pageProps || {})
75-
._sentryGetServerSidePropsTraceData;
73+
const { page, query, props } = nextData;
7674

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+
}
8089

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;
87100
}
88101

89102
const DEFAULT_TAGS = {

0 commit comments

Comments
 (0)