|
| 1 | +/** |
| 2 | + * This file is a template for the code which will be substituted when our webpack loader handles non-API files in the |
| 3 | + * `pages/` directory. |
| 4 | + * |
| 5 | + * We use `__RESOURCE_PATH__` as a placeholder for the path to the file being wrapped. Because it's not a real package, |
| 6 | + * this causes both TS and ESLint to complain, hence the pragma comments below. |
| 7 | + */ |
| 8 | + |
| 9 | +// @ts-ignore See above |
| 10 | +// eslint-disable-next-line import/no-unresolved |
| 11 | +import * as wrapee from '__RESOURCE_PATH__'; |
| 12 | +import * as Sentry from '@sentry/nextjs'; |
| 13 | +import type { GetServerSideProps, GetStaticProps, NextPage as NextPageComponent } from 'next'; |
| 14 | + |
| 15 | +type NextPageModule = { |
| 16 | + default: { getInitialProps?: NextPageComponent['getInitialProps'] }; |
| 17 | + getStaticProps?: GetStaticProps; |
| 18 | + getServerSideProps?: GetServerSideProps; |
| 19 | +}; |
| 20 | + |
| 21 | +const userPageModule = wrapee as NextPageModule; |
| 22 | + |
| 23 | +const pageComponent = userPageModule.default; |
| 24 | + |
| 25 | +const origGetInitialProps = pageComponent.getInitialProps; |
| 26 | +const origGetStaticProps = userPageModule.getStaticProps; |
| 27 | +const origGetServerSideProps = userPageModule.getServerSideProps; |
| 28 | + |
| 29 | +if (typeof origGetInitialProps === 'function') { |
| 30 | + pageComponent.getInitialProps = Sentry.withSentryGetInitialProps( |
| 31 | + origGetInitialProps, |
| 32 | + '__ROUTE__', |
| 33 | + ) as NextPageComponent['getInitialProps']; |
| 34 | +} |
| 35 | + |
| 36 | +export const getStaticProps = |
| 37 | + typeof origGetStaticProps === 'function' |
| 38 | + ? Sentry.withSentryGetStaticProps(origGetStaticProps, '__ROUTE__') |
| 39 | + : undefined; |
| 40 | +export const getServerSideProps = |
| 41 | + typeof origGetServerSideProps === 'function' |
| 42 | + ? Sentry.withSentryGetServerSideProps(origGetServerSideProps, '__ROUTE__') |
| 43 | + : undefined; |
| 44 | + |
| 45 | +export default pageComponent; |
| 46 | + |
| 47 | +// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to |
| 48 | +// not include anything whose name matchs something we've explicitly exported above. |
| 49 | +// @ts-ignore See above |
| 50 | +// eslint-disable-next-line import/no-unresolved |
| 51 | +export * from '__RESOURCE_PATH__'; |
0 commit comments