@@ -85,6 +85,10 @@ interface DataFunction {
85
85
( args : DataFunctionArgs ) : Promise < Response > | Response | Promise < AppData > | AppData ;
86
86
}
87
87
88
+ interface ReactRouterDomPkg {
89
+ matchRoutes : ( routes : ServerRoute [ ] , pathname : string ) => RouteMatch < ServerRoute > [ ] | null ;
90
+ }
91
+
88
92
// Taken from Remix Implementation
89
93
// https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/routeMatching.ts#L6-L10
90
94
export interface RouteMatch < Route > {
@@ -272,38 +276,40 @@ function createRoutes(manifest: ServerRouteManifest, parentId?: string): ServerR
272
276
} ) ) ;
273
277
}
274
278
275
- function wrapRequestHandler ( origRequestHandler : RequestHandler , build : ServerBuild ) : RequestHandler {
276
- const routes = createRoutes ( build . routes ) ;
277
- const pkg = loadModule < {
278
- matchRoutes : ( routes : ServerRoute [ ] , pathname : string ) => RouteMatch < ServerRoute > [ ] | null ;
279
- } > ( 'react-router-dom' ) ;
280
- // https://github.com/remix-run/remix/blob/38e127b1d97485900b9c220d93503de0deb1fc81/packages/remix-server-runtime/routeMatching.ts#L12-L24
281
- function matchServerRoutes ( routes : ServerRoute [ ] , pathname : string ) : RouteMatch < ServerRoute > [ ] | null {
282
- if ( ! pkg ) {
283
- return null ;
284
- }
285
-
286
- const matches = pkg . matchRoutes ( routes , pathname ) ;
287
- if ( ! matches ) {
288
- return null ;
289
- }
279
+ // Remix Implementation:
280
+ // https://github.com/remix-run/remix/blob/38e127b1d97485900b9c220d93503de0deb1fc81/packages/remix-server-runtime/routeMatching.ts#L12-L24
281
+ //
282
+ // Changed so that `matchRoutes` function is passed in.
283
+ function matchServerRoutes (
284
+ routes : ServerRoute [ ] ,
285
+ pathname : string ,
286
+ pkg ?: ReactRouterDomPkg ,
287
+ ) : RouteMatch < ServerRoute > [ ] | null {
288
+ if ( ! pkg ) {
289
+ return null ;
290
+ }
290
291
291
- return matches . map ( match => ( {
292
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
293
- params : match . params ,
294
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
295
- pathname : match . pathname ,
296
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
297
- route : match . route as unknown as ServerRoute ,
298
- } ) ) ;
292
+ const matches = pkg . matchRoutes ( routes , pathname ) ;
293
+ if ( ! matches ) {
294
+ return null ;
299
295
}
300
296
297
+ return matches . map ( match => ( {
298
+ params : match . params ,
299
+ pathname : match . pathname ,
300
+ route : match . route ,
301
+ } ) ) ;
302
+ }
303
+
304
+ function wrapRequestHandler ( origRequestHandler : RequestHandler , build : ServerBuild ) : RequestHandler {
305
+ const routes = createRoutes ( build . routes ) ;
306
+ const pkg = loadModule < ReactRouterDomPkg > ( 'react-router-dom' ) ;
301
307
return async function ( this : unknown , request : Request , loadContext ?: unknown ) : Promise < Response > {
302
308
const hub = getCurrentHub ( ) ;
303
309
const currentScope = hub . getScope ( ) ;
304
310
305
311
const url = new URL ( request . url ) ;
306
- const matches = matchServerRoutes ( routes , url . pathname ) ;
312
+ const matches = matchServerRoutes ( routes , url . pathname , pkg ) ;
307
313
308
314
const match = matches && getRequestMatch ( url , matches ) ;
309
315
const name = match === null ? url . pathname : match . route . id ;
@@ -385,9 +391,11 @@ function makeWrappedCreateRequestHandler(
385
391
routes [ id ] = wrappedRoute ;
386
392
}
387
393
388
- const requestHandler = origCreateRequestHandler . call ( this , { ...build , routes, entry : wrappedEntry } , mode ) ;
394
+ const newBuild = { ...build , routes, entry : wrappedEntry } ;
395
+
396
+ const requestHandler = origCreateRequestHandler . call ( this , newBuild , mode ) ;
389
397
390
- return wrapRequestHandler ( requestHandler , build ) ;
398
+ return wrapRequestHandler ( requestHandler , newBuild ) ;
391
399
} ;
392
400
}
393
401
0 commit comments