-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwithSentryServerSideDocumentGetInitialProps.ts
52 lines (46 loc) · 2 KB
/
withSentryServerSideDocumentGetInitialProps.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { hasTracingEnabled } from '@sentry/tracing';
import Document from 'next/document';
import { isBuild } from '../../utils/isBuild';
import { callTracedServerSideDataFetcher, withErrorInstrumentation } from './wrapperUtils';
type DocumentGetInitialProps = typeof Document.getInitialProps;
/**
* Create a wrapped version of the user's exported `getInitialProps` function in
* a custom document ("_document.js").
*
* @param origDocumentGetInitialProps The user's `getInitialProps` function
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryServerSideDocumentGetInitialProps(
origDocumentGetInitialProps: DocumentGetInitialProps,
): DocumentGetInitialProps {
return async function (
...documentGetInitialPropsArguments: Parameters<DocumentGetInitialProps>
): ReturnType<DocumentGetInitialProps> {
if (isBuild()) {
return origDocumentGetInitialProps(...documentGetInitialPropsArguments);
}
const [context] = documentGetInitialPropsArguments;
const { req, res } = context;
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',
},
);
} else {
return errorWrappedGetInitialProps(...documentGetInitialPropsArguments);
}
};
}