@@ -37,7 +37,7 @@ const DATA_FETCHING_FUNCTIONS = {
37
37
38
38
type LoaderOptions = {
39
39
projectDir : string ;
40
- pageRegex : RegExp ;
40
+ pagesDir : string ;
41
41
} ;
42
42
43
43
/**
@@ -109,7 +109,7 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
109
109
}
110
110
111
111
// We know one or the other will be defined, depending on the version of webpack being used
112
- const { projectDir, pageRegex } = 'getOptions' in this ? this . getOptions ( ) : this . query ;
112
+ const { projectDir, pagesDir } = 'getOptions' in this ? this . getOptions ( ) : this . query ;
113
113
114
114
// In the following branch we will proxy the user's file. This means we return code (basically an entirely new file)
115
115
// that re - exports all the user file's originial export, but with a "sentry-proxy-loader" query in the module
@@ -171,22 +171,26 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
171
171
path . relative ( projectDir , this . resourcePath ) ,
172
172
) ;
173
173
174
- // Get the route name from this page's filepath
175
- let route = this . resourcePath . match ( pageRegex ) ?. [ 2 ] ;
176
- if ( ! route ) {
177
- logger . warn ( `Unable to wrap code from page ${ this . resourcePath } , because the route regex has no matches.` ) ;
178
- return userCode ;
179
- }
180
-
181
174
// Fill in template placeholders
182
175
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 ) ;
183
190
for ( const { placeholder, alias } of Object . values ( DATA_FETCHING_FUNCTIONS ) ) {
184
191
injectedCode = injectedCode . replace ( new RegExp ( placeholder , 'g' ) , alias ) ;
185
192
}
186
193
187
- // Any route ending in '/index' will correspond to the root of that directory, '/'.
188
- route = route . replace ( / i n d e x $ / , '' ) ;
189
- injectedCode = injectedCode . replace ( '__FILEPATH__' , route ) ;
190
194
191
195
return `${ modifiedUserCode } \n${ injectedCode } ` ;
192
196
}
0 commit comments