diff --git a/packages/nextjs/src/client/index.ts b/packages/nextjs/src/client/index.ts index 3ea78374290d..fbd9e1eaefba 100644 --- a/packages/nextjs/src/client/index.ts +++ b/packages/nextjs/src/client/index.ts @@ -92,3 +92,25 @@ function addClientIntegrations(options: BrowserOptions): void { options.integrations = integrations; } + +export { + // eslint-disable-next-line deprecation/deprecation + withSentryServerSideGetInitialProps, + wrapGetInitialPropsWithSentry, +} from './wrapGetInitialPropsWithSentry'; + +export { + // eslint-disable-next-line deprecation/deprecation + withSentryServerSideAppGetInitialProps, + wrapAppGetInitialPropsWithSentry, +} from './wrapAppGetInitialPropsWithSentry'; +export { + // eslint-disable-next-line deprecation/deprecation + withSentryServerSideDocumentGetInitialProps, + wrapDocumentGetInitialPropsWithSentry, +} from './wrapDocumentGetInitialPropsWithSentry'; +export { + // eslint-disable-next-line deprecation/deprecation + withSentryServerSideErrorGetInitialProps, + wrapErrorGetInitialPropsWithSentry, +} from './wrapErrorGetInitialPropsWithSentry'; diff --git a/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..205650d41dac --- /dev/null +++ b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts @@ -0,0 +1,18 @@ +import type App from 'next/app'; + +type AppGetInitialProps = typeof App['getInitialProps']; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps { + return async function (this: unknown, ...args: Parameters): ReturnType { + return await origAppGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapAppGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..c68c2a266df1 --- /dev/null +++ b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts @@ -0,0 +1,23 @@ +import type Document from 'next/document'; + +type DocumentGetInitialProps = typeof Document.getInitialProps; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapDocumentGetInitialPropsWithSentry( + origDocumentGetInitialProps: DocumentGetInitialProps, +): DocumentGetInitialProps { + return async function ( + this: unknown, + ...args: Parameters + ): ReturnType { + return await origDocumentGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..e018fb47246d --- /dev/null +++ b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts @@ -0,0 +1,21 @@ +import type { NextPageContext } from 'next'; +import type { ErrorProps } from 'next/error'; + +type ErrorGetInitialProps = (context: NextPageContext) => Promise; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapErrorGetInitialPropsWithSentry( + origErrorGetInitialProps: ErrorGetInitialProps, +): ErrorGetInitialProps { + return async function (this: unknown, ...args: Parameters): ReturnType { + return await origErrorGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..f29561a4f333 --- /dev/null +++ b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts @@ -0,0 +1,18 @@ +import type { NextPage } from 'next'; + +type GetInitialProps = Required['getInitialProps']; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps { + return async function (this: unknown, ...args: Parameters): Promise> { + return origGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/index.types.ts b/packages/nextjs/src/index.types.ts index fbc3bec2d71b..28b6314c5225 100644 --- a/packages/nextjs/src/index.types.ts +++ b/packages/nextjs/src/index.types.ts @@ -53,3 +53,71 @@ export declare function wrapApiHandlerWithSentry ) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function of a custom `_app` page with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapAppGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapAppGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideAppGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function of a custom `_document` page with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapDocumentGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideDocumentGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function of a custom `_error` page with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapErrorGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideErrorGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>;