Skip to content

Commit ea1c5e0

Browse files
committed
Address PR review
1 parent 5d1097f commit ea1c5e0

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ interface DataFunction {
8585
(args: DataFunctionArgs): Promise<Response> | Response | Promise<AppData> | AppData;
8686
}
8787

88+
interface ReactRouterDomPkg {
89+
matchRoutes: (routes: ServerRoute[], pathname: string) => RouteMatch<ServerRoute>[] | null;
90+
}
91+
8892
// Taken from Remix Implementation
8993
// https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/routeMatching.ts#L6-L10
9094
export interface RouteMatch<Route> {
@@ -272,38 +276,40 @@ function createRoutes(manifest: ServerRouteManifest, parentId?: string): ServerR
272276
}));
273277
}
274278

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+
}
290291

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;
299295
}
300296

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');
301307
return async function (this: unknown, request: Request, loadContext?: unknown): Promise<Response> {
302308
const hub = getCurrentHub();
303309
const currentScope = hub.getScope();
304310

305311
const url = new URL(request.url);
306-
const matches = matchServerRoutes(routes, url.pathname);
312+
const matches = matchServerRoutes(routes, url.pathname, pkg);
307313

308314
const match = matches && getRequestMatch(url, matches);
309315
const name = match === null ? url.pathname : match.route.id;
@@ -385,9 +391,11 @@ function makeWrappedCreateRequestHandler(
385391
routes[id] = wrappedRoute;
386392
}
387393

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);
389397

390-
return wrapRequestHandler(requestHandler, build);
398+
return wrapRequestHandler(requestHandler, newBuild);
391399
};
392400
}
393401

0 commit comments

Comments
 (0)