diff --git a/packages/remix-dev/config/routesConvention.ts b/packages/remix-dev/config/routesConvention.ts index 6e566911a0c..69f751aeed6 100644 --- a/packages/remix-dev/config/routesConvention.ts +++ b/packages/remix-dev/config/routesConvention.ts @@ -49,10 +49,7 @@ export function defineConventionalRoutes( }); let routeIds = Object.keys(files).sort(byLongestFirst); - let parentRouteIds: { [routeId: string]: string | undefined } = {}; - for (let id of routeIds) { - parentRouteIds[id] = findParentRouteId(routeIds, id); - } + let parentRouteIds = getParentRouteIds(routeIds); let uniqueRoutes = new Map(); @@ -197,11 +194,16 @@ export function createRoutePath(partialRouteId: string): string | undefined { return result || undefined; } -function findParentRouteId( - routeIds: string[], - childRouteId: string -): string | undefined { - return routeIds.find((id) => childRouteId.startsWith(`${id}/`)); +function getParentRouteIds( + routeIds: string[] +): Record { + return routeIds.reduce>( + (parentRouteIds, childRouteId) => ({ + ...parentRouteIds, + [childRouteId]: routeIds.find((id) => childRouteId.startsWith(`${id}/`)), + }), + {} + ); } function byLongestFirst(a: string, b: string): number {