@@ -40,8 +40,10 @@ function getFinalConfigObject(
40
40
// to `constructWebpackConfigFunction` so that it can live in the returned function's closure.
41
41
const { sentry : userSentryOptions } = incomingUserNextConfigObject ;
42
42
delete incomingUserNextConfigObject . sentry ;
43
- // Remind TS that there's now no `sentry` property
44
- const userNextConfigObject = incomingUserNextConfigObject as NextConfigObject ;
43
+
44
+ if ( userSentryOptions ?. rewritesTunnel ) {
45
+ setUpRewriteRules ( incomingUserNextConfigObject , userSentryOptions . rewritesTunnel ) ;
46
+ }
45
47
46
48
// In order to prevent all of our build-time code from being bundled in people's route-handling serverless functions,
47
49
// we exclude `webpack.ts` and all of its dependencies from nextjs's `@vercel/nft` filetracing. We therefore need to
@@ -50,11 +52,52 @@ function getFinalConfigObject(
50
52
// eslint-disable-next-line @typescript-eslint/no-var-requires
51
53
const { constructWebpackConfigFunction } = require ( './webpack' ) ;
52
54
return {
53
- ...userNextConfigObject ,
54
- webpack : constructWebpackConfigFunction ( userNextConfigObject , userSentryWebpackPluginOptions , userSentryOptions ) ,
55
+ ...incomingUserNextConfigObject ,
56
+ webpack : constructWebpackConfigFunction (
57
+ incomingUserNextConfigObject ,
58
+ userSentryWebpackPluginOptions ,
59
+ userSentryOptions ,
60
+ ) ,
55
61
} ;
56
62
}
57
63
58
64
// At runtime, we just return the user's config untouched.
59
- return userNextConfigObject ;
65
+ return incomingUserNextConfigObject ;
66
+ }
67
+
68
+ /**
69
+ * TODO
70
+ */
71
+ function setUpRewriteRules ( userNextConfig : NextConfigObject , tunnelPath : string ) : void {
72
+ const originalRewrites = userNextConfig . rewrites ;
73
+
74
+ // This function doesn't take any arguments at the time of writing but we future-proof
75
+ // here in case Next.js ever decides to pass some
76
+ userNextConfig . rewrites = async ( ...args : unknown [ ] ) => {
77
+ // TODO: Consider trailing slash option
78
+
79
+ const injectedRewrites = [
80
+ {
81
+ source : `${ tunnelPath } /:orgid/:projectid` , // without slash
82
+ destination : 'https://:orgid.ingest.sentry.io/api/:projectid/envelope/' ,
83
+ } ,
84
+ {
85
+ source : `${ tunnelPath } /:orgid/:projectid/` , // with slash
86
+ destination : 'https://:orgid.ingest.sentry.io/api/:projectid/envelope/' ,
87
+ } ,
88
+ ] ;
89
+
90
+ if ( typeof originalRewrites === 'function' ) {
91
+ // @ts -ignore weird args error
92
+ const intermediaryValue = await originalRewrites ( ...args ) ;
93
+ if ( Array . isArray ( intermediaryValue ) ) {
94
+ intermediaryValue . push ( ...injectedRewrites ) ;
95
+ } else {
96
+ intermediaryValue . beforeFiles . push ( ...injectedRewrites ) ;
97
+ }
98
+ return intermediaryValue ;
99
+ } else {
100
+ return injectedRewrites ;
101
+ }
102
+ } ;
60
103
}
0 commit comments