@sentry/nextjs
Version:
Official Sentry SDK for Next.js
69 lines (56 loc) • 2.83 kB
JavaScript
import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
import { addTracingExtensions, spanToTraceHeader, getDynamicSamplingContextFromSpan, getClient, getCurrentScope } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
import { isBuild } from './utils/isBuild.js';
import { getTransactionFromRequest, withErrorInstrumentation, withTracedServerSideDataFetcher } from './utils/wrapperUtils.js';
/**
* Create a wrapped version of the user's exported `getServerSideProps` function
*
* @param origGetServerSideProps The user's `getServerSideProps` function
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
function wrapGetServerSidePropsWithSentry(
origGetServerSideProps,
parameterizedRoute,
) {
return new Proxy(origGetServerSideProps, {
apply: async (wrappingTarget, thisArg, args) => {
if (isBuild()) {
return wrappingTarget.apply(thisArg, args);
}
addTracingExtensions();
const [context] = args;
const { req, res } = context;
const errorWrappedGetServerSideProps = withErrorInstrumentation(wrappingTarget);
const options = _optionalChain([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
if (_optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
const tracedGetServerSideProps = withTracedServerSideDataFetcher(errorWrappedGetServerSideProps, req, res, {
dataFetcherRouteName: parameterizedRoute,
requestedRouteName: parameterizedRoute,
dataFetchingMethodName: 'getServerSideProps',
});
const serverSideProps = await (tracedGetServerSideProps.apply(thisArg, args)
);
if (serverSideProps && 'props' in serverSideProps) {
// eslint-disable-next-line deprecation/deprecation
const requestTransaction = _nullishCoalesce(getTransactionFromRequest(req), () => ( getCurrentScope().getTransaction()));
if (requestTransaction) {
serverSideProps.props._sentryTraceData = spanToTraceHeader(requestTransaction);
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestTransaction);
serverSideProps.props._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
}
}
return serverSideProps;
} else {
return errorWrappedGetServerSideProps.apply(thisArg, args);
}
},
});
}
/**
* @deprecated Use `withSentryGetServerSideProps` instead.
*/
const withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry;
export { withSentryGetServerSideProps, wrapGetServerSidePropsWithSentry };
//# sourceMappingURL=wrapGetServerSidePropsWithSentry.js.map