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