Skip to content

Commit 2fa11eb

Browse files
authored
fix(nextjs): Pass this through wrappers (#6572)
1 parent 2a063d9 commit 2fa11eb

7 files changed

+138
-120
lines changed

packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import { GetServerSideProps } from 'next';
44

55
import { isBuild } from '../../utils/isBuild';
6-
import { callTracedServerSideDataFetcher, getTransactionFromRequest, withErrorInstrumentation } from './wrapperUtils';
6+
import { getTransactionFromRequest, withErrorInstrumentation, withTracedServerSideDataFetcher } from './wrapperUtils';
77

88
/**
99
* Create a wrapped version of the user's exported `getServerSideProps` function
@@ -16,30 +16,26 @@ export function withSentryGetServerSideProps(
1616
origGetServerSideProps: GetServerSideProps,
1717
parameterizedRoute: string,
1818
): GetServerSideProps {
19-
return async function (
20-
...getServerSidePropsArguments: Parameters<GetServerSideProps>
21-
): ReturnType<GetServerSideProps> {
19+
return async function (this: unknown, ...args: Parameters<GetServerSideProps>): ReturnType<GetServerSideProps> {
2220
if (isBuild()) {
23-
return origGetServerSideProps(...getServerSidePropsArguments);
21+
return origGetServerSideProps.apply(this, args);
2422
}
2523

26-
const [context] = getServerSidePropsArguments;
24+
const [context] = args;
2725
const { req, res } = context;
2826

2927
const errorWrappedGetServerSideProps = withErrorInstrumentation(origGetServerSideProps);
3028

3129
if (hasTracingEnabled()) {
32-
const serverSideProps = await callTracedServerSideDataFetcher(
33-
errorWrappedGetServerSideProps,
34-
getServerSidePropsArguments,
35-
req,
36-
res,
37-
{
38-
dataFetcherRouteName: parameterizedRoute,
39-
requestedRouteName: parameterizedRoute,
40-
dataFetchingMethodName: 'getServerSideProps',
41-
},
42-
);
30+
const tracedGetServerSideProps = withTracedServerSideDataFetcher(errorWrappedGetServerSideProps, req, res, {
31+
dataFetcherRouteName: parameterizedRoute,
32+
requestedRouteName: parameterizedRoute,
33+
dataFetchingMethodName: 'getServerSideProps',
34+
});
35+
36+
const serverSideProps = await (tracedGetServerSideProps.apply(this, args) as ReturnType<
37+
typeof tracedGetServerSideProps
38+
>);
4339

4440
if ('props' in serverSideProps) {
4541
const requestTransaction = getTransactionFromRequest(req);
@@ -53,7 +49,7 @@ export function withSentryGetServerSideProps(
5349

5450
return serverSideProps;
5551
} else {
56-
return errorWrappedGetServerSideProps(...getServerSidePropsArguments);
52+
return errorWrappedGetServerSideProps.apply(this, args);
5753
}
5854
};
5955
}

packages/nextjs/src/config/wrappers/withSentryServerSideAppGetInitialProps.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import App from 'next/app';
44

55
import { isBuild } from '../../utils/isBuild';
6-
import { callTracedServerSideDataFetcher, getTransactionFromRequest, withErrorInstrumentation } from './wrapperUtils';
6+
import { getTransactionFromRequest, withErrorInstrumentation, withTracedServerSideDataFetcher } from './wrapperUtils';
77

88
type AppGetInitialProps = typeof App['getInitialProps'];
99

@@ -16,14 +16,12 @@ type AppGetInitialProps = typeof App['getInitialProps'];
1616
* @returns A wrapped version of the function
1717
*/
1818
export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
19-
return async function (
20-
...appGetInitialPropsArguments: Parameters<AppGetInitialProps>
21-
): ReturnType<AppGetInitialProps> {
19+
return async function (this: unknown, ...args: Parameters<AppGetInitialProps>): ReturnType<AppGetInitialProps> {
2220
if (isBuild()) {
23-
return origAppGetInitialProps(...appGetInitialPropsArguments);
21+
return origAppGetInitialProps.apply(this, args);
2422
}
2523

26-
const [context] = appGetInitialPropsArguments;
24+
const [context] = args;
2725
const { req, res } = context.ctx;
2826

2927
const errorWrappedAppGetInitialProps = withErrorInstrumentation(origAppGetInitialProps);
@@ -33,16 +31,18 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A
3331
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3432
// span with each other when there are no req or res objects, we simply do not trace them at all here.
3533
if (hasTracingEnabled() && req && res) {
34+
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, req, res, {
35+
dataFetcherRouteName: '/_app',
36+
requestedRouteName: context.ctx.pathname,
37+
dataFetchingMethodName: 'getInitialProps',
38+
});
39+
3640
const appGetInitialProps: {
3741
pageProps: {
3842
_sentryTraceData?: string;
3943
_sentryBaggage?: string;
4044
};
41-
} = await callTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, appGetInitialPropsArguments, req, res, {
42-
dataFetcherRouteName: '/_app',
43-
requestedRouteName: context.ctx.pathname,
44-
dataFetchingMethodName: 'getInitialProps',
45-
});
45+
} = await tracedGetInitialProps.apply(this, args);
4646

4747
const requestTransaction = getTransactionFromRequest(req);
4848

@@ -64,7 +64,7 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A
6464

6565
return appGetInitialProps;
6666
} else {
67-
return errorWrappedAppGetInitialProps(...appGetInitialPropsArguments);
67+
return errorWrappedAppGetInitialProps.apply(this, args);
6868
}
6969
};
7070
}

packages/nextjs/src/config/wrappers/withSentryServerSideDocumentGetInitialProps.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hasTracingEnabled } from '@sentry/tracing';
22
import Document from 'next/document';
33

44
import { isBuild } from '../../utils/isBuild';
5-
import { callTracedServerSideDataFetcher, withErrorInstrumentation } from './wrapperUtils';
5+
import { withErrorInstrumentation, withTracedServerSideDataFetcher } from './wrapperUtils';
66

77
type DocumentGetInitialProps = typeof Document.getInitialProps;
88

@@ -18,13 +18,14 @@ export function withSentryServerSideDocumentGetInitialProps(
1818
origDocumentGetInitialProps: DocumentGetInitialProps,
1919
): DocumentGetInitialProps {
2020
return async function (
21-
...documentGetInitialPropsArguments: Parameters<DocumentGetInitialProps>
21+
this: unknown,
22+
...args: Parameters<DocumentGetInitialProps>
2223
): ReturnType<DocumentGetInitialProps> {
2324
if (isBuild()) {
24-
return origDocumentGetInitialProps(...documentGetInitialPropsArguments);
25+
return origDocumentGetInitialProps.apply(this, args);
2526
}
2627

27-
const [context] = documentGetInitialPropsArguments;
28+
const [context] = args;
2829
const { req, res } = context;
2930

3031
const errorWrappedGetInitialProps = withErrorInstrumentation(origDocumentGetInitialProps);
@@ -34,13 +35,15 @@ export function withSentryServerSideDocumentGetInitialProps(
3435
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3536
// span with each other when there are no req or res objects, we simply do not trace them at all here.
3637
if (hasTracingEnabled() && req && res) {
37-
return callTracedServerSideDataFetcher(errorWrappedGetInitialProps, documentGetInitialPropsArguments, req, res, {
38+
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
3839
dataFetcherRouteName: '/_document',
3940
requestedRouteName: context.pathname,
4041
dataFetchingMethodName: 'getInitialProps',
4142
});
43+
44+
return await tracedGetInitialProps.apply(this, args);
4245
} else {
43-
return errorWrappedGetInitialProps(...documentGetInitialPropsArguments);
46+
return errorWrappedGetInitialProps.apply(this, args);
4447
}
4548
};
4649
}

packages/nextjs/src/config/wrappers/withSentryServerSideErrorGetInitialProps.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { NextPageContext } from 'next';
44
import { ErrorProps } from 'next/error';
55

66
import { isBuild } from '../../utils/isBuild';
7-
import { callTracedServerSideDataFetcher, getTransactionFromRequest, withErrorInstrumentation } from './wrapperUtils';
7+
import { getTransactionFromRequest, withErrorInstrumentation, withTracedServerSideDataFetcher } from './wrapperUtils';
88

99
type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;
1010

@@ -19,14 +19,12 @@ type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;
1919
export function withSentryServerSideErrorGetInitialProps(
2020
origErrorGetInitialProps: ErrorGetInitialProps,
2121
): ErrorGetInitialProps {
22-
return async function (
23-
...errorGetInitialPropsArguments: Parameters<ErrorGetInitialProps>
24-
): ReturnType<ErrorGetInitialProps> {
22+
return async function (this: unknown, ...args: Parameters<ErrorGetInitialProps>): ReturnType<ErrorGetInitialProps> {
2523
if (isBuild()) {
26-
return origErrorGetInitialProps(...errorGetInitialPropsArguments);
24+
return origErrorGetInitialProps.apply(this, args);
2725
}
2826

29-
const [context] = errorGetInitialPropsArguments;
27+
const [context] = args;
3028
const { req, res } = context;
3129

3230
const errorWrappedGetInitialProps = withErrorInstrumentation(origErrorGetInitialProps);
@@ -36,15 +34,17 @@ export function withSentryServerSideErrorGetInitialProps(
3634
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3735
// span with each other when there are no req or res objects, we simply do not trace them at all here.
3836
if (hasTracingEnabled() && req && res) {
39-
const errorGetInitialProps: ErrorProps & {
40-
_sentryTraceData?: string;
41-
_sentryBaggage?: string;
42-
} = await callTracedServerSideDataFetcher(errorWrappedGetInitialProps, errorGetInitialPropsArguments, req, res, {
37+
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
4338
dataFetcherRouteName: '/_error',
4439
requestedRouteName: context.pathname,
4540
dataFetchingMethodName: 'getInitialProps',
4641
});
4742

43+
const errorGetInitialProps: ErrorProps & {
44+
_sentryTraceData?: string;
45+
_sentryBaggage?: string;
46+
} = await tracedGetInitialProps.apply(this, args);
47+
4848
const requestTransaction = getTransactionFromRequest(req);
4949
if (requestTransaction) {
5050
errorGetInitialProps._sentryTraceData = requestTransaction.toTraceparent();
@@ -55,7 +55,7 @@ export function withSentryServerSideErrorGetInitialProps(
5555

5656
return errorGetInitialProps;
5757
} else {
58-
return errorWrappedGetInitialProps(...errorGetInitialPropsArguments);
58+
return errorWrappedGetInitialProps.apply(this, args);
5959
}
6060
};
6161
}

packages/nextjs/src/config/wrappers/withSentryServerSideGetInitialProps.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import { NextPage } from 'next';
44

55
import { isBuild } from '../../utils/isBuild';
6-
import { callTracedServerSideDataFetcher, getTransactionFromRequest, withErrorInstrumentation } from './wrapperUtils';
6+
import { getTransactionFromRequest, withErrorInstrumentation, withTracedServerSideDataFetcher } from './wrapperUtils';
77

88
type GetInitialProps = Required<NextPage>['getInitialProps'];
99

@@ -15,14 +15,12 @@ type GetInitialProps = Required<NextPage>['getInitialProps'];
1515
* @returns A wrapped version of the function
1616
*/
1717
export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInitialProps): GetInitialProps {
18-
return async function (
19-
...getInitialPropsArguments: Parameters<GetInitialProps>
20-
): Promise<ReturnType<GetInitialProps>> {
18+
return async function (this: unknown, ...args: Parameters<GetInitialProps>): Promise<ReturnType<GetInitialProps>> {
2119
if (isBuild()) {
22-
return origGetInitialProps(...getInitialPropsArguments);
20+
return origGetInitialProps.apply(this, args);
2321
}
2422

25-
const [context] = getInitialPropsArguments;
23+
const [context] = args;
2624
const { req, res } = context;
2725

2826
const errorWrappedGetInitialProps = withErrorInstrumentation(origGetInitialProps);
@@ -32,15 +30,17 @@ export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInit
3230
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3331
// span with each other when there are no req or res objects, we simply do not trace them at all here.
3432
if (hasTracingEnabled() && req && res) {
35-
const initialProps: {
36-
_sentryTraceData?: string;
37-
_sentryBaggage?: string;
38-
} = await callTracedServerSideDataFetcher(errorWrappedGetInitialProps, getInitialPropsArguments, req, res, {
33+
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
3934
dataFetcherRouteName: context.pathname,
4035
requestedRouteName: context.pathname,
4136
dataFetchingMethodName: 'getInitialProps',
4237
});
4338

39+
const initialProps: {
40+
_sentryTraceData?: string;
41+
_sentryBaggage?: string;
42+
} = await tracedGetInitialProps.apply(this, args);
43+
4444
const requestTransaction = getTransactionFromRequest(req);
4545
if (requestTransaction) {
4646
initialProps._sentryTraceData = requestTransaction.toTraceparent();
@@ -51,7 +51,7 @@ export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInit
5151

5252
return initialProps;
5353
} else {
54-
return errorWrappedGetInitialProps(...getInitialPropsArguments);
54+
return errorWrappedGetInitialProps.apply(this, args);
5555
}
5656
};
5757
}

0 commit comments

Comments
 (0)