Skip to content

fix(nextjs): Don't assert existence of req and res in data-fetching wrappers #5866

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

Merged
merged 2 commits into from
Oct 3, 2022
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 @@ -28,27 +28,23 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A

const errorWrappedAppGetInitialProps = withErrorInstrumentation(origAppGetInitialProps);

if (hasTracingEnabled()) {
// Since this wrapper is only applied to `getInitialProps` running on the server, we can assert that `req` and
// `res` are always defined: https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// Generally we can assume that `req` and `res` are always defined on the server:
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
// span with each other when there are no req or res objects, we simply do not trace them at all here.
if (hasTracingEnabled() && req && res) {
const appGetInitialProps: {
pageProps: {
_sentryTraceData?: string;
_sentryBaggage?: string;
};
} = await callTracedServerSideDataFetcher(
errorWrappedAppGetInitialProps,
appGetInitialPropsArguments,
req!,
res!,
{
dataFetcherRouteName: '/_app',
requestedRouteName: context.ctx.pathname,
dataFetchingMethodName: 'getInitialProps',
},
);
} = await callTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, appGetInitialPropsArguments, req, res, {
dataFetcherRouteName: '/_app',
requestedRouteName: context.ctx.pathname,
dataFetchingMethodName: 'getInitialProps',
});

const requestTransaction = getTransactionFromRequest(req!);
const requestTransaction = getTransactionFromRequest(req);
if (requestTransaction) {
appGetInitialProps.pageProps._sentryTraceData = requestTransaction.toTraceparent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,16 @@ export function withSentryServerSideDocumentGetInitialProps(

const errorWrappedGetInitialProps = withErrorInstrumentation(origDocumentGetInitialProps);

if (hasTracingEnabled()) {
// Since this wrapper is only applied to `getInitialProps` running on the server, we can assert that `req` and
// `res` are always defined: https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
return callTracedServerSideDataFetcher(
errorWrappedGetInitialProps,
documentGetInitialPropsArguments,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
req!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
res!,
{
dataFetcherRouteName: '/_document',
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
},
);
// Generally we can assume that `req` and `res` are always defined on the server:
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
// span with each other when there are no req or res objects, we simply do not trace them at all here.
if (hasTracingEnabled() && req && res) {
return callTracedServerSideDataFetcher(errorWrappedGetInitialProps, documentGetInitialPropsArguments, req, res, {
dataFetcherRouteName: '/_document',
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
});
} else {
return errorWrappedGetInitialProps(...documentGetInitialPropsArguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,21 @@ export function withSentryServerSideErrorGetInitialProps(

const errorWrappedGetInitialProps = withErrorInstrumentation(origErrorGetInitialProps);

if (hasTracingEnabled()) {
// Since this wrapper is only applied to `getInitialProps` running on the server, we can assert that `req` and
// `res` are always defined: https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// Generally we can assume that `req` and `res` are always defined on the server:
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
// span with each other when there are no req or res objects, we simply do not trace them at all here.
if (hasTracingEnabled() && req && res) {
const errorGetInitialProps: ErrorProps & {
_sentryTraceData?: string;
_sentryBaggage?: string;
} = await callTracedServerSideDataFetcher(
errorWrappedGetInitialProps,
errorGetInitialPropsArguments,
req!,
res!,
{
dataFetcherRouteName: '/_error',
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
},
);
} = await callTracedServerSideDataFetcher(errorWrappedGetInitialProps, errorGetInitialPropsArguments, req, res, {
dataFetcherRouteName: '/_error',
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
});

const requestTransaction = getTransactionFromRequest(req!);
const requestTransaction = getTransactionFromRequest(req);
if (requestTransaction) {
errorGetInitialProps._sentryTraceData = requestTransaction.toTraceparent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInit

const errorWrappedGetInitialProps = withErrorInstrumentation(origGetInitialProps);

if (hasTracingEnabled()) {
// Since this wrapper is only applied to `getInitialProps` running on the server, we can assert that `req` and
// `res` are always defined: https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// Generally we can assume that `req` and `res` are always defined on the server:
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
// span with each other when there are no req or res objects, we simply do not trace them at all here.
if (hasTracingEnabled() && req && res) {
const initialProps: {
_sentryTraceData?: string;
_sentryBaggage?: string;
} = await callTracedServerSideDataFetcher(errorWrappedGetInitialProps, getInitialPropsArguments, req!, res!, {
} = await callTracedServerSideDataFetcher(errorWrappedGetInitialProps, getInitialPropsArguments, req, res, {
dataFetcherRouteName: context.pathname,
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
});

const requestTransaction = getTransactionFromRequest(req!);
const requestTransaction = getTransactionFromRequest(req);
if (requestTransaction) {
initialProps._sentryTraceData = requestTransaction.toTraceparent();

Expand Down