Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nextjs): Only apply tracing metadata to data fetcher data when data is an object #14575

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>).pageProps) {
(appGetInitialProps as Record<string, unknown>).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<string, unknown> }).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<string, unknown> }).pageProps._sentryBaggage = baggage;
}
}

return appGetInitialProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ 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;
}
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<string, unknown>)._sentryTraceData = sentryTrace;
}

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

return errorGetInitialProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>)._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<string, unknown>)._sentryBaggage = baggage;
}
}

return initialProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ export function wrapGetServerSidePropsWithSentry(
data: serverSideProps,
baggage,
sentryTrace,
}: {
data?: unknown;
baggage?: string;
sentryTrace?: string;
} = await (tracedGetServerSideProps.apply(thisArg, args) as ReturnType<typeof tracedGetServerSideProps>);

if (serverSideProps && '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<string, unknown>)._sentryTraceData = sentryTrace;
Expand Down
Loading