Skip to content

Commit ad4e9f7

Browse files
authored
fix(nextjs): Don't put undefined values in props (#12131)
1 parent 53298b9 commit ad4e9f7

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

packages/nextjs/src/common/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,20 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
5656
}
5757

5858
if (requestSpan) {
59-
appGetInitialProps.pageProps._sentryTraceData = spanToTraceHeader(requestSpan);
59+
const sentryTrace = spanToTraceHeader(requestSpan);
60+
61+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
62+
if (sentryTrace) {
63+
appGetInitialProps.pageProps._sentryTraceData = sentryTrace;
64+
}
65+
6066
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestSpan);
61-
appGetInitialProps.pageProps._sentryBaggage =
62-
dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
67+
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
68+
69+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
70+
if (baggage) {
71+
appGetInitialProps.pageProps._sentryBaggage = baggage;
72+
}
6373
}
6474

6575
return appGetInitialProps;

packages/nextjs/src/common/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,20 @@ export function wrapErrorGetInitialPropsWithSentry(
4949
const requestSpan = getSpanFromRequest(req) ?? (activeSpan ? getRootSpan(activeSpan) : undefined);
5050

5151
if (requestSpan) {
52-
errorGetInitialProps._sentryTraceData = spanToTraceHeader(requestSpan);
52+
const sentryTrace = spanToTraceHeader(requestSpan);
53+
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+
}
5358

5459
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestSpan);
55-
errorGetInitialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
60+
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
61+
62+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
63+
if (baggage) {
64+
errorGetInitialProps._sentryBaggage = baggage;
65+
}
5666
}
5767

5868
return errorGetInitialProps;

packages/nextjs/src/common/wrapGetInitialPropsWithSentry.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@ export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialPro
4545
const requestSpan = getSpanFromRequest(req) ?? (activeSpan ? getRootSpan(activeSpan) : undefined);
4646

4747
if (requestSpan) {
48-
initialProps._sentryTraceData = spanToTraceHeader(requestSpan);
48+
const sentryTrace = spanToTraceHeader(requestSpan);
49+
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+
}
4954

5055
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestSpan);
51-
initialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
56+
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
57+
58+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
59+
if (baggage) {
60+
initialProps._sentryBaggage = baggage;
61+
}
5262
}
5363

5464
return initialProps;

packages/nextjs/src/common/wrapGetServerSidePropsWithSentry.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ export function wrapGetServerSidePropsWithSentry(
3838

3939
if (serverSideProps && 'props' in serverSideProps) {
4040
const activeSpan = getActiveSpan();
41-
const requestTransaction = getSpanFromRequest(req) ?? (activeSpan ? getRootSpan(activeSpan) : undefined);
42-
if (requestTransaction) {
43-
(serverSideProps.props as Record<string, unknown>)._sentryTraceData = spanToTraceHeader(requestTransaction);
44-
45-
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestTransaction);
46-
(serverSideProps.props as Record<string, unknown>)._sentryBaggage =
47-
dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
41+
const requestSpan = getSpanFromRequest(req) ?? (activeSpan ? getRootSpan(activeSpan) : undefined);
42+
if (requestSpan) {
43+
const sentryTrace = spanToTraceHeader(requestSpan);
44+
45+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
46+
if (sentryTrace) {
47+
(serverSideProps.props as Record<string, unknown>)._sentryTraceData = sentryTrace;
48+
}
49+
50+
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestSpan);
51+
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
52+
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
53+
if (baggage) {
54+
(serverSideProps.props as Record<string, unknown>)._sentryBaggage = baggage;
55+
}
4856
}
4957
}
5058

0 commit comments

Comments
 (0)