-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathpageWrapperTemplate.ts
60 lines (49 loc) · 2.79 KB
/
pageWrapperTemplate.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
57
58
59
60
/*
* 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 `__SENTRY_WRAPPING_TARGET_FILE__` 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.
*/
import * as Sentry from '@sentry/nextjs';
// @ts-expect-error See above
import * as wrapee from '__SENTRY_WRAPPING_TARGET_FILE__';
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 ? userPageModule.default : undefined;
const origGetInitialProps = pageComponent ? pageComponent.getInitialProps : undefined;
const origGetStaticProps = userPageModule ? userPageModule.getStaticProps : undefined;
const origGetServerSideProps = userPageModule ? userPageModule.getServerSideProps : undefined;
// Rollup will aggressively tree-shake what it perceives to be unused properties
// on objects. Because the key that's used to index into this object (__ROUTE__)
// is replaced during bundling, Rollup can't see that these properties are in fact
// used. Using `Object.freeze` signals to Rollup that it should not tree-shake
// this object.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getInitialPropsWrappers: Record<string, any> = Object.freeze({
'/_app': Sentry.wrapAppGetInitialPropsWithSentry,
'/_document': Sentry.wrapDocumentGetInitialPropsWithSentry,
'/_error': Sentry.wrapErrorGetInitialPropsWithSentry,
});
const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.wrapGetInitialPropsWithSentry;
if (pageComponent && typeof origGetInitialProps === 'function') {
pageComponent.getInitialProps = getInitialPropsWrapper(origGetInitialProps) as NextPageComponent['getInitialProps'];
}
export const getStaticProps =
typeof origGetStaticProps === 'function'
? Sentry.wrapGetStaticPropsWithSentry(origGetStaticProps, '__ROUTE__')
: undefined;
export const getServerSideProps =
typeof origGetServerSideProps === 'function'
? Sentry.wrapGetServerSidePropsWithSentry(origGetServerSideProps, '__ROUTE__')
: undefined;
export default pageComponent ? Sentry.wrapPageComponentWithSentry(pageComponent as unknown) : 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 matches something we've explicitly exported above.
// @ts-expect-error See above
export * from '__SENTRY_WRAPPING_TARGET_FILE__';