@@ -111,6 +111,21 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
111
111
// We know one or the other will be defined, depending on the version of webpack being used
112
112
const { projectDir, pagesDir } = 'getOptions' in this ? this . getOptions ( ) : this . query ;
113
113
114
+ // Get the parameterized route name from this page's filepath
115
+ const parameterizedRouteName = path
116
+ // Get the path of the file insde of the pages directory
117
+ . relative ( pagesDir , this . resourcePath )
118
+ // Add a slash at the beginning
119
+ . replace ( / ( .* ) / , '/$1' )
120
+ // Pull off the file extension
121
+ . replace ( / \. ( j s x ? | t s x ? ) / , '' )
122
+ // Any page file named `index` corresponds to root of the directory its in, URL-wise, so turn `/xyz/index` into
123
+ // just `/xyz`
124
+ . replace ( / \/ i n d e x $ / , '' )
125
+ // In case all of the above have left us with an empty string (which will happen if we're dealing with the
126
+ // homepage), sub back in the root route
127
+ . replace ( / ^ $ / , '/' ) ;
128
+
114
129
// In the following branch we will proxy the user's file. This means we return code (basically an entirely new file)
115
130
// that re - exports all the user file's originial export, but with a "sentry-proxy-loader" query in the module
116
131
// string.
@@ -136,13 +151,26 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
136
151
if ( hasDefaultExport ( ast ) ) {
137
152
outputFileContent += `
138
153
import { default as _sentry_default } from "${ this . resourcePath } ?sentry-proxy-loader";
139
- import { withSentryGetInitialProps } from "@sentry/nextjs";
140
-
141
- if (typeof _sentry_default.getInitialProps === 'function') {
142
- _sentry_default.getInitialProps = withSentryGetInitialProps(_sentry_default.getInitialProps);
143
- }
144
-
145
- export default _sentry_default;` ;
154
+ import { withSentryGetInitialProps } from "@sentry/nextjs";` ;
155
+
156
+ if ( parameterizedRouteName === '/_app' ) {
157
+ // getInitialProps signature is a bit different in _app.js so we need a different wrapper
158
+ // Currently a no-op
159
+ } else if ( parameterizedRouteName === '/_error' ) {
160
+ // getInitialProps behaviour is a bit different in _error.js so we probably want different wrapper
161
+ // Currently a no-op
162
+ } else if ( parameterizedRouteName === '/_document' ) {
163
+ // getInitialProps signature is a bit different in _document.js so we need a different wrapper
164
+ // Currently a no-op
165
+ } else {
166
+ // We enter this branch for any "normal" Next.js page
167
+ outputFileContent += `
168
+ if (typeof _sentry_default.getInitialProps === 'function') {
169
+ _sentry_default.getInitialProps = withSentryGetInitialProps(_sentry_default.getInitialProps, '${ parameterizedRouteName } ');
170
+ }` ;
171
+ }
172
+
173
+ outputFileContent += 'export default _sentry_default;' ;
146
174
}
147
175
148
176
return outputFileContent ;
@@ -173,20 +201,8 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
173
201
174
202
// Fill in template placeholders
175
203
let injectedCode = modifiedTemplateCode ;
176
- const route = path
177
- // Get the path of the file insde of the pages directory
178
- . relative ( pagesDir , this . resourcePath )
179
- // Add a slash at the beginning
180
- . replace ( / ( .* ) / , '/$1' )
181
- // Pull off the file extension
182
- . replace ( / \. ( j s x ? | t s x ? ) / , '' )
183
- // Any page file named `index` corresponds to root of the directory its in, URL-wise, so turn `/xyz/index` into
184
- // just `/xyz`
185
- . replace ( / \/ i n d e x $ / , '' )
186
- // In case all of the above have left us with an empty string (which will happen if we're dealing with the
187
- // homepage), sub back in the root route
188
- . replace ( / ^ $ / , '/' ) ;
189
- injectedCode = injectedCode . replace ( '__FILEPATH__' , route ) ;
204
+
205
+ injectedCode = injectedCode . replace ( '__FILEPATH__' , parameterizedRouteName ) ;
190
206
for ( const { placeholder, alias } of Object . values ( DATA_FETCHING_FUNCTIONS ) ) {
191
207
injectedCode = injectedCode . replace ( new RegExp ( placeholder , 'g' ) , alias ) ;
192
208
}
0 commit comments