Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/server/helpers/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,28 @@ export function findMatchingRoute(
...nextServer.getAppPathRoutes(),
};

let matchedRoute = undefined;
for (const [routePath, [filePath]] of Object.entries(appPathRoutes)) {
if (!routePath || !filePath) continue;
const sortedRoutes = Object.entries(appPathRoutes)
.filter(([routePath, file]) => routePath && file)
.sort(([a], [b]) => scoreRoute(b) - scoreRoute(a));

for (const [routePath, [filePath]] of sortedRoutes) {
const realPath = `${basePath}${routePath}`;
const routeParams = getRouteParams(realPath, requestPathname);
if (routeParams) matchedRoute = { filename: filePath, params: routeParams };
if (routeParams) {
return { filename: filePath, params: routeParams };
}
}
return matchedRoute;
}

function scoreRoute(routePath: string) {
const parts = routePath.split('/').filter(Boolean);

let score = 0;
for (const part of parts) {
if (part.startsWith('[...')) score += 0;
else if (part.startsWith('[')) score += 2;
else score += 3;
}

return score + parts.length;
}