-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextjs): Instrument server-side
getInitialProps
of _app
, `_d…
…ocument` and `_error` (#5604)
- Loading branch information
Showing
10 changed files
with
195 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export { withSentryGetStaticProps } from './withSentryGetStaticProps'; | ||
export { withSentryGetServerSideProps } from './withSentryGetServerSideProps'; | ||
export { withSentryServerSideGetInitialProps } from './withSentryServerSideGetInitialProps'; | ||
export { withSentryServerSideAppGetInitialProps } from './withSentryServerSideAppGetInitialProps'; | ||
export { withSentryServerSideDocumentGetInitialProps } from './withSentryServerSideDocumentGetInitialProps'; | ||
export { withSentryServerSideErrorGetInitialProps } from './withSentryServerSideErrorGetInitialProps'; | ||
export { withSentryGetServerSideProps } from './withSentryGetServerSideProps'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/nextjs/src/config/wrappers/withSentryServerSideAppGetInitialProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { hasTracingEnabled } from '@sentry/tracing'; | ||
import App from 'next/app'; | ||
|
||
import { isBuild } from '../../utils/isBuild'; | ||
import { callTracedServerSideDataFetcher, withErrorInstrumentation } from './wrapperUtils'; | ||
|
||
type AppGetInitialProps = typeof App['getInitialProps']; | ||
|
||
/** | ||
* Create a wrapped version of the user's exported `getInitialProps` function in | ||
* a custom app ("_app.js"). | ||
* | ||
* @param origAppGetInitialProps The user's `getInitialProps` function | ||
* @param parameterizedRoute The page's parameterized route | ||
* @returns A wrapped version of the function | ||
*/ | ||
export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps { | ||
return async function ( | ||
...appGetInitialPropsArguments: Parameters<AppGetInitialProps> | ||
): ReturnType<AppGetInitialProps> { | ||
if (isBuild()) { | ||
return origAppGetInitialProps(...appGetInitialPropsArguments); | ||
} | ||
|
||
const [context] = appGetInitialPropsArguments; | ||
const { req, res } = context.ctx; | ||
|
||
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 | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return callTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, appGetInitialPropsArguments, req!, res!, { | ||
dataFetcherRouteName: '/_app', | ||
requestedRouteName: context.ctx.pathname, | ||
dataFetchingMethodName: 'getInitialProps', | ||
}); | ||
} else { | ||
return errorWrappedAppGetInitialProps(...appGetInitialPropsArguments); | ||
} | ||
}; | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/nextjs/src/config/wrappers/withSentryServerSideDocumentGetInitialProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
}; | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/nextjs/src/config/wrappers/withSentryServerSideErrorGetInitialProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { hasTracingEnabled } from '@sentry/tracing'; | ||
import { NextPageContext } from 'next'; | ||
import { ErrorProps } from 'next/error'; | ||
|
||
import { isBuild } from '../../utils/isBuild'; | ||
import { callTracedServerSideDataFetcher, withErrorInstrumentation } from './wrapperUtils'; | ||
|
||
type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>; | ||
|
||
/** | ||
* Create a wrapped version of the user's exported `getInitialProps` function in | ||
* a custom error page ("_error.js"). | ||
* | ||
* @param origErrorGetInitialProps The user's `getInitialProps` function | ||
* @param parameterizedRoute The page's parameterized route | ||
* @returns A wrapped version of the function | ||
*/ | ||
export function withSentryServerSideErrorGetInitialProps( | ||
origErrorGetInitialProps: ErrorGetInitialProps, | ||
): ErrorGetInitialProps { | ||
return async function ( | ||
...errorGetInitialPropsArguments: Parameters<ErrorGetInitialProps> | ||
): ReturnType<ErrorGetInitialProps> { | ||
if (isBuild()) { | ||
return origErrorGetInitialProps(...errorGetInitialPropsArguments); | ||
} | ||
|
||
const [context] = errorGetInitialPropsArguments; | ||
const { req, res } = context; | ||
|
||
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 | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return callTracedServerSideDataFetcher(errorWrappedGetInitialProps, errorGetInitialPropsArguments, req!, res!, { | ||
dataFetcherRouteName: '/_error', | ||
requestedRouteName: context.pathname, | ||
dataFetchingMethodName: 'getInitialProps', | ||
}); | ||
} else { | ||
return errorWrappedGetInitialProps(...errorGetInitialPropsArguments); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters