From a6792907bef2e3bbe2f535fd68d1c77cacff6811 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 4 Dec 2024 15:36:53 +0000 Subject: [PATCH 1/2] fix(nextjs): Only apply tracing metadata to data fetcher data when data is an object --- .../wrapAppGetInitialPropsWithSentry.ts | 37 +++++++++---------- .../wrapErrorGetInitialPropsWithSentry.ts | 9 ++--- .../wrapGetInitialPropsWithSentry.ts | 21 +++++------ .../wrapGetServerSidePropsWithSentry.ts | 2 +- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.ts index 10f783b9e9e6..bc0f9ff170c6 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.ts @@ -41,32 +41,29 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI sentryTrace, baggage, }: { - data: { - pageProps: { - _sentryTraceData?: string; - _sentryBaggage?: string; - }; - }; + data?: unknown; sentryTrace?: string; baggage?: string; } = await tracedGetInitialProps.apply(thisArg, args); - // Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call - // `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per - // https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`. - // For this reason, we just handle the case where `pageProps` doesn't exist explicitly. - if (!appGetInitialProps.pageProps) { - appGetInitialProps.pageProps = {}; - } + if (typeof appGetInitialProps === 'object' && appGetInitialProps !== null) { + // Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call + // `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per + // https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`. + // For this reason, we just handle the case where `pageProps` doesn't exist explicitly. + if (!(appGetInitialProps as Record).pageProps) { + (appGetInitialProps as Record).pageProps = {}; + } - // The Next.js serializer throws on undefined values so we need to guard for it (#12102) - if (sentryTrace) { - appGetInitialProps.pageProps._sentryTraceData = sentryTrace; - } + // The Next.js serializer throws on undefined values so we need to guard for it (#12102) + if (sentryTrace) { + (appGetInitialProps as { pageProps: Record }).pageProps._sentryTraceData = sentryTrace; + } - // The Next.js serializer throws on undefined values so we need to guard for it (#12102) - if (baggage) { - appGetInitialProps.pageProps._sentryBaggage = baggage; + // The Next.js serializer throws on undefined values so we need to guard for it (#12102) + if (baggage) { + (appGetInitialProps as { pageProps: Record }).pageProps._sentryBaggage = baggage; + } } return appGetInitialProps; diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts index 731d3fe1e24a..c677e2503719 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts @@ -43,22 +43,19 @@ export function wrapErrorGetInitialPropsWithSentry( baggage, sentryTrace, }: { - data: ErrorProps & { - _sentryTraceData?: string; - _sentryBaggage?: string; - }; + data?: unknown; baggage?: string; sentryTrace?: string; } = await tracedGetInitialProps.apply(thisArg, args); // The Next.js serializer throws on undefined values so we need to guard for it (#12102) if (sentryTrace) { - errorGetInitialProps._sentryTraceData = sentryTrace; + (errorGetInitialProps as Record)._sentryTraceData = sentryTrace; } // The Next.js serializer throws on undefined values so we need to guard for it (#12102) if (baggage) { - errorGetInitialProps._sentryBaggage = baggage; + (errorGetInitialProps as Record)._sentryBaggage = baggage; } return errorGetInitialProps; diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.ts index 97246ec9d122..2ba9bbb3156d 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.ts @@ -39,22 +39,21 @@ export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialPro baggage, sentryTrace, }: { - data: { - _sentryTraceData?: string; - _sentryBaggage?: string; - }; + data?: unknown; baggage?: string; sentryTrace?: string; } = (await tracedGetInitialProps.apply(thisArg, args)) ?? {}; // Next.js allows undefined to be returned from a getInitialPropsFunction. - // The Next.js serializer throws on undefined values so we need to guard for it (#12102) - if (sentryTrace) { - initialProps._sentryTraceData = sentryTrace; - } + if (typeof initialProps === 'object' && initialProps !== null) { + // The Next.js serializer throws on undefined values so we need to guard for it (#12102) + if (sentryTrace) { + (initialProps as Record)._sentryTraceData = sentryTrace; + } - // The Next.js serializer throws on undefined values so we need to guard for it (#12102) - if (baggage) { - initialProps._sentryBaggage = baggage; + // The Next.js serializer throws on undefined values so we need to guard for it (#12102) + if (baggage) { + (initialProps as Record)._sentryBaggage = baggage; + } } return initialProps; diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts index 7c4b4101d80e..dc813ff2bc9c 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts @@ -36,7 +36,7 @@ export function wrapGetServerSidePropsWithSentry( sentryTrace, } = await (tracedGetServerSideProps.apply(thisArg, args) as ReturnType); - if (serverSideProps && 'props' in serverSideProps) { + if (typeof serverSideProps === 'object' && 'props' in serverSideProps) { // The Next.js serializer throws on undefined values so we need to guard for it (#12102) if (sentryTrace) { (serverSideProps.props as Record)._sentryTraceData = sentryTrace; From 75f77c4392dbbe505bb63aede55106e0157df53b Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Thu, 5 Dec 2024 09:08:47 +0000 Subject: [PATCH 2/2] unfumble --- .../wrapErrorGetInitialPropsWithSentry.ts | 16 +++++++++------- .../wrapGetServerSidePropsWithSentry.ts | 6 +++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts index c677e2503719..0a0ee1e2867f 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.ts @@ -48,14 +48,16 @@ export function wrapErrorGetInitialPropsWithSentry( sentryTrace?: string; } = await tracedGetInitialProps.apply(thisArg, args); - // The Next.js serializer throws on undefined values so we need to guard for it (#12102) - if (sentryTrace) { - (errorGetInitialProps as Record)._sentryTraceData = sentryTrace; - } + if (typeof errorGetInitialProps === 'object' && errorGetInitialProps !== null) { + if (sentryTrace) { + // The Next.js serializer throws on undefined values so we need to guard for it (#12102) + (errorGetInitialProps as Record)._sentryTraceData = sentryTrace; + } - // The Next.js serializer throws on undefined values so we need to guard for it (#12102) - if (baggage) { - (errorGetInitialProps as Record)._sentryBaggage = baggage; + // The Next.js serializer throws on undefined values so we need to guard for it (#12102) + if (baggage) { + (errorGetInitialProps as Record)._sentryBaggage = baggage; + } } return errorGetInitialProps; diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts index dc813ff2bc9c..328f35d8b350 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.ts @@ -34,9 +34,13 @@ export function wrapGetServerSidePropsWithSentry( data: serverSideProps, baggage, sentryTrace, + }: { + data?: unknown; + baggage?: string; + sentryTrace?: string; } = await (tracedGetServerSideProps.apply(thisArg, args) as ReturnType); - if (typeof serverSideProps === 'object' && 'props' in serverSideProps) { + if (typeof serverSideProps === 'object' && serverSideProps !== null && 'props' in serverSideProps) { // The Next.js serializer throws on undefined values so we need to guard for it (#12102) if (sentryTrace) { (serverSideProps.props as Record)._sentryTraceData = sentryTrace;