Skip to content

Commit c6ae78b

Browse files
authored
fix(nextjs): Only apply tracing metadata to data fetcher data when data is an object (#14575)
1 parent 1c72b31 commit c6ae78b

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

packages/nextjs/src/common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.ts

+17-20
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,29 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
4141
sentryTrace,
4242
baggage,
4343
}: {
44-
data: {
45-
pageProps: {
46-
_sentryTraceData?: string;
47-
_sentryBaggage?: string;
48-
};
49-
};
44+
data?: unknown;
5045
sentryTrace?: string;
5146
baggage?: string;
5247
} = await tracedGetInitialProps.apply(thisArg, args);
5348

54-
// Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call
55-
// `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per
56-
// https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`.
57-
// For this reason, we just handle the case where `pageProps` doesn't exist explicitly.
58-
if (!appGetInitialProps.pageProps) {
59-
appGetInitialProps.pageProps = {};
60-
}
49+
if (typeof appGetInitialProps === 'object' && appGetInitialProps !== null) {
50+
// Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call
51+
// `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per
52+
// https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`.
53+
// For this reason, we just handle the case where `pageProps` doesn't exist explicitly.
54+
if (!(appGetInitialProps as Record<string, unknown>).pageProps) {
55+
(appGetInitialProps as Record<string, unknown>).pageProps = {};
56+
}
6157

62-
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
63-
if (sentryTrace) {
64-
appGetInitialProps.pageProps._sentryTraceData = sentryTrace;
65-
}
58+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
59+
if (sentryTrace) {
60+
(appGetInitialProps as { pageProps: Record<string, unknown> }).pageProps._sentryTraceData = sentryTrace;
61+
}
6662

67-
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
68-
if (baggage) {
69-
appGetInitialProps.pageProps._sentryBaggage = baggage;
63+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
64+
if (baggage) {
65+
(appGetInitialProps as { pageProps: Record<string, unknown> }).pageProps._sentryBaggage = baggage;
66+
}
7067
}
7168

7269
return appGetInitialProps;

packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ export function wrapErrorGetInitialPropsWithSentry(
4343
baggage,
4444
sentryTrace,
4545
}: {
46-
data: ErrorProps & {
47-
_sentryTraceData?: string;
48-
_sentryBaggage?: string;
49-
};
46+
data?: unknown;
5047
baggage?: string;
5148
sentryTrace?: string;
5249
} = await tracedGetInitialProps.apply(thisArg, args);
5350

54-
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
55-
if (sentryTrace) {
56-
errorGetInitialProps._sentryTraceData = sentryTrace;
57-
}
51+
if (typeof errorGetInitialProps === 'object' && errorGetInitialProps !== null) {
52+
if (sentryTrace) {
53+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
54+
(errorGetInitialProps as Record<string, unknown>)._sentryTraceData = sentryTrace;
55+
}
5856

59-
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
60-
if (baggage) {
61-
errorGetInitialProps._sentryBaggage = baggage;
57+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
58+
if (baggage) {
59+
(errorGetInitialProps as Record<string, unknown>)._sentryBaggage = baggage;
60+
}
6261
}
6362

6463
return errorGetInitialProps;

packages/nextjs/src/common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,21 @@ export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialPro
3939
baggage,
4040
sentryTrace,
4141
}: {
42-
data: {
43-
_sentryTraceData?: string;
44-
_sentryBaggage?: string;
45-
};
42+
data?: unknown;
4643
baggage?: string;
4744
sentryTrace?: string;
4845
} = (await tracedGetInitialProps.apply(thisArg, args)) ?? {}; // Next.js allows undefined to be returned from a getInitialPropsFunction.
4946

50-
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
51-
if (sentryTrace) {
52-
initialProps._sentryTraceData = sentryTrace;
53-
}
47+
if (typeof initialProps === 'object' && initialProps !== null) {
48+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
49+
if (sentryTrace) {
50+
(initialProps as Record<string, unknown>)._sentryTraceData = sentryTrace;
51+
}
5452

55-
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
56-
if (baggage) {
57-
initialProps._sentryBaggage = baggage;
53+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
54+
if (baggage) {
55+
(initialProps as Record<string, unknown>)._sentryBaggage = baggage;
56+
}
5857
}
5958

6059
return initialProps;

packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ export function wrapGetServerSidePropsWithSentry(
3434
data: serverSideProps,
3535
baggage,
3636
sentryTrace,
37+
}: {
38+
data?: unknown;
39+
baggage?: string;
40+
sentryTrace?: string;
3741
} = await (tracedGetServerSideProps.apply(thisArg, args) as ReturnType<typeof tracedGetServerSideProps>);
3842

39-
if (serverSideProps && 'props' in serverSideProps) {
43+
if (typeof serverSideProps === 'object' && serverSideProps !== null && 'props' in serverSideProps) {
4044
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
4145
if (sentryTrace) {
4246
(serverSideProps.props as Record<string, unknown>)._sentryTraceData = sentryTrace;

0 commit comments

Comments
 (0)