Skip to content

Commit 936b42e

Browse files
author
Luca Forstner
committed
Add explanation
1 parent f99d89b commit 936b42e

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/nextjs/src/performance/client.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,27 @@ interface SentryEnhancedNextData extends NextData {
2525
};
2626
}
2727

28-
// Author's note: It's really not that complicated.
29-
// eslint-disable-next-line complexity
28+
/**
29+
* Every Next.js page (static and dynamic ones) comes with a script tag with the id "__NEXT_DATA__". This script tag
30+
* contains a JSON object with data that was either generated at build time for static pages (`getStaticProps`), or at
31+
* runtime with data fetchers like `getServerSideProps.`.
32+
*
33+
* We can use this information to:
34+
* - Always get the parameterized route we're in when loading a page.
35+
* - Send trace information (trace-id, baggage) from the server to the client.
36+
*
37+
* This function extracts this information.
38+
*/
3039
function extractNextDataTagInformation(): {
31-
route: string;
32-
source: 'route' | 'url';
40+
route: string | undefined;
3341
traceId: string | undefined;
3442
baggage: string | undefined;
3543
params: ParsedUrlQuery | undefined;
3644
} {
3745
let nextData: SentryEnhancedNextData | undefined;
3846

47+
// Let's be on the safe side and actually check first if there is really a __NEXT_DATA__ script tag on the page.
48+
// Theoretically this should always be the case though.
3949
const nextDataTag = global.document.getElementById('__NEXT_DATA__');
4050
if (nextDataTag && nextDataTag.innerHTML) {
4151
try {
@@ -46,8 +56,7 @@ function extractNextDataTagInformation(): {
4656
}
4757

4858
// `nextData.page` always contains the parameterized route
49-
const route = (nextData || {}).page || global.document.location.pathname;
50-
const source = nextData ? 'route' : 'url';
59+
const route = (nextData || {}).page;
5160

5261
const getServerSidePropsTraceId = (((nextData || {}).props || {}).pageProps || {})._sentryGetServerSidePropsTraceId;
5362
const getInitialPropsTraceId = ((nextData || {}).props || {})._sentryGetInitialPropsTraceId;
@@ -58,7 +67,6 @@ function extractNextDataTagInformation(): {
5867

5968
return {
6069
route,
61-
source,
6270
params,
6371
// Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` so we give it priority.
6472
traceId: getInitialPropsTraceId || getServerSidePropsTraceId,
@@ -90,9 +98,10 @@ export function nextRouterInstrumentation(
9098
startTransaction = startTransactionCb;
9199

92100
if (startTransactionOnPageLoad) {
93-
const { route, source, traceId, baggage, params } = extractNextDataTagInformation();
101+
const { route, traceId, baggage, params } = extractNextDataTagInformation();
94102

95-
prevTransactionName = route;
103+
prevTransactionName = route || global.document.location.pathname;
104+
const source = route ? 'route' : 'url';
96105

97106
activeTransaction = startTransactionCb({
98107
name: prevTransactionName,

0 commit comments

Comments
 (0)