Skip to content

Commit a679290

Browse files
author
Luca Forstner
committed
fix(nextjs): Only apply tracing metadata to data fetcher data when data is an object
1 parent fcd2935 commit a679290

File tree

4 files changed

+31
-38
lines changed

4 files changed

+31
-38
lines changed

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

Lines changed: 17 additions & 20 deletions
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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,19 @@ 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

5451
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
5552
if (sentryTrace) {
56-
errorGetInitialProps._sentryTraceData = sentryTrace;
53+
(errorGetInitialProps as Record<string, unknown>)._sentryTraceData = sentryTrace;
5754
}
5855

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

6461
return errorGetInitialProps;

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

Lines changed: 10 additions & 11 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function wrapGetServerSidePropsWithSentry(
3636
sentryTrace,
3737
} = await (tracedGetServerSideProps.apply(thisArg, args) as ReturnType<typeof tracedGetServerSideProps>);
3838

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

0 commit comments

Comments
 (0)