pageWrapperTemplate.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as Sentry from '@sentry/nextjs';
  2. import * as serverComponentModule from '__SENTRY_WRAPPING_TARGET_FILE__';
  3. export * from '__SENTRY_WRAPPING_TARGET_FILE__';
  4. /*
  5. * This file is a template for the code which will be substituted when our webpack loader handles non-API files in the
  6. * `pages/` directory.
  7. *
  8. * We use `__SENTRY_WRAPPING_TARGET_FILE__` as a placeholder for the path to the file being wrapped. Because it's not a real package,
  9. * this causes both TS and ESLint to complain, hence the pragma comments below.
  10. */
  11. const userPageModule = serverComponentModule ;
  12. const pageComponent = userPageModule ? userPageModule.default : undefined;
  13. const origGetInitialProps = pageComponent ? pageComponent.getInitialProps : undefined;
  14. const origGetStaticProps = userPageModule ? userPageModule.getStaticProps : undefined;
  15. const origGetServerSideProps = userPageModule ? userPageModule.getServerSideProps : undefined;
  16. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  17. const getInitialPropsWrappers = {
  18. '/_app': Sentry.wrapAppGetInitialPropsWithSentry,
  19. '/_document': Sentry.wrapDocumentGetInitialPropsWithSentry,
  20. '/_error': Sentry.wrapErrorGetInitialPropsWithSentry,
  21. };
  22. const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.wrapGetInitialPropsWithSentry;
  23. if (pageComponent && typeof origGetInitialProps === 'function') {
  24. pageComponent.getInitialProps = getInitialPropsWrapper(origGetInitialProps) ;
  25. }
  26. const getStaticProps =
  27. typeof origGetStaticProps === 'function'
  28. ? Sentry.wrapGetStaticPropsWithSentry(origGetStaticProps, '__ROUTE__')
  29. : undefined;
  30. const getServerSideProps =
  31. typeof origGetServerSideProps === 'function'
  32. ? Sentry.wrapGetServerSidePropsWithSentry(origGetServerSideProps, '__ROUTE__')
  33. : undefined;
  34. const pageWrapperTemplate = pageComponent ? Sentry.wrapPageComponentWithSentry(pageComponent ) : pageComponent;
  35. export { pageWrapperTemplate as default, getServerSideProps, getStaticProps };