@sentry/nextjs
Version:
Official Sentry SDK for Next.js
86 lines (69 loc) • 3.75 kB
JavaScript
var {
_nullishCoalesce,
_optionalChain
} = require('@sentry/utils');
Object.defineProperty(exports, '__esModule', { value: true });
const core = require('@sentry/core');
const utils = require('@sentry/utils');
const isBuild = require('./utils/isBuild.js');
const wrapperUtils = require('./utils/wrapperUtils.js');
/**
* 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
*/
function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps) {
return new Proxy(origAppGetInitialProps, {
apply: async (wrappingTarget, thisArg, args) => {
if (isBuild.isBuild()) {
return wrappingTarget.apply(thisArg, args);
}
core.addTracingExtensions();
const [context] = args;
const { req, res } = context.ctx;
const errorWrappedAppGetInitialProps = wrapperUtils.withErrorInstrumentation(wrappingTarget);
const options = _optionalChain([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
// 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 (req && res && _optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
const tracedGetInitialProps = wrapperUtils.withTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, req, res, {
dataFetcherRouteName: '/_app',
requestedRouteName: context.ctx.pathname,
dataFetchingMethodName: 'getInitialProps',
});
const appGetInitialProps
= await tracedGetInitialProps.apply(thisArg, args);
// eslint-disable-next-line deprecation/deprecation
const requestTransaction = _nullishCoalesce(wrapperUtils.getTransactionFromRequest(req), () => ( core.getCurrentScope().getTransaction()));
// Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call
// `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per
// https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`.
// For this reason, we just handle the case where `pageProps` doesn't exist explicitly.
if (!appGetInitialProps.pageProps) {
appGetInitialProps.pageProps = {};
}
if (requestTransaction) {
appGetInitialProps.pageProps._sentryTraceData = core.spanToTraceHeader(requestTransaction);
const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(requestTransaction);
appGetInitialProps.pageProps._sentryBaggage =
utils.dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
}
return appGetInitialProps;
} else {
return errorWrappedAppGetInitialProps.apply(thisArg, args);
}
},
});
}
/**
* @deprecated Use `wrapAppGetInitialPropsWithSentry` instead.
*/
const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry;
exports.withSentryServerSideAppGetInitialProps = withSentryServerSideAppGetInitialProps;
exports.wrapAppGetInitialPropsWithSentry = wrapAppGetInitialPropsWithSentry;
//# sourceMappingURL=wrapAppGetInitialPropsWithSentry.js.map