fix: in sortRoutePaths: sort files alphabetically (#3769)

Closes #3773

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
William Weiss
2026-04-21 17:20:47 +00:00
committed by GitHub
co-authored by Bartek Iwańczuk Claude Opus 4.6
parent 36dd6375eb
commit ecc42850b9
2 changed files with 53 additions and 7 deletions
+5 -6
View File
@@ -246,8 +246,7 @@ export function sortRoutePaths(a: string, b: string) {
const scoreB = getRoutePathScore(charB, b, bIdx); const scoreB = getRoutePathScore(charB, b, bIdx);
if (scoreA === scoreB) { if (scoreA === scoreB) {
if (charA !== charB) { if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough? return charA < charB ? -1 : 1;
return charA < charB ? 0 : 1;
} }
continue; continue;
} }
@@ -256,8 +255,7 @@ export function sortRoutePaths(a: string, b: string) {
} }
if (charA !== charB) { if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough? return charA < charB ? -1 : 1;
return charA < charB ? 0 : 1;
} }
// If we're at the end of A or B, then we assume that the longer // If we're at the end of A or B, then we assume that the longer
@@ -292,8 +290,9 @@ function getRoutePathScore(char: string, s: string, i: number): number {
} }
if ( if (
i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" && char === "i" && s[i + 1] === "n" &&
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x" s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x" &&
(i + 4 === s.length - 1 || s[i + 5] === ".")
) { ) {
return 3; return 3;
} }
+48 -1
View File
@@ -1315,6 +1315,8 @@ Deno.test("fsRoutes - sortRoutePaths with groups", () => {
routes = [ routes = [
"/_app", "/_app",
"/app",
"/app/_middleware",
"/(authed)/_middleware", "/(authed)/_middleware",
"/(authed)/_layout", "/(authed)/_layout",
"/_error", "/_error",
@@ -1325,26 +1327,71 @@ Deno.test("fsRoutes - sortRoutePaths with groups", () => {
"/(authed)/(account)/account", "/(authed)/(account)/account",
"/(authed)/api/slug", "/(authed)/api/slug",
"/hooks/github", "/hooks/github",
"/(authed)/[org]/__foo",
"/(authed)/[org]/_middleware", "/(authed)/[org]/_middleware",
"/(authed)/[org]/index", "/(authed)/[org]/index",
]; ];
routes.sort(() => Math.random() > 0.5 ? -1 : 1);
routes.sort(sortRoutePaths); routes.sort(sortRoutePaths);
sorted = [ sorted = [
"/_app", "/_app",
"/_error", "/_error",
"/login", "/app/_middleware",
"/app",
"/auth/login", "/auth/login",
"/auth/logout", "/auth/logout",
"/hooks/github", "/hooks/github",
"/login",
"/(authed)/_middleware", "/(authed)/_middleware",
"/(authed)/_layout", "/(authed)/_layout",
"/(authed)/index", "/(authed)/index",
"/(authed)/api/slug", "/(authed)/api/slug",
"/(authed)/(account)/account", "/(authed)/(account)/account",
"/(authed)/[org]/_middleware", "/(authed)/[org]/_middleware",
"/(authed)/[org]/__foo",
"/(authed)/[org]/index", "/(authed)/[org]/index",
]; ];
expect(routes).toEqual(sorted); expect(routes).toEqual(sorted);
routes = [
"/_app.js",
"/app.tsx",
"/app/_middleware.tsx",
"/(authed)/_middleware.tsx",
"/(authed)/_layout.jsx",
"/_error.tsx",
"/(authed)/index.js",
"/login.js",
"/auth/logout.tsx",
"/auth/login.ts",
"/(authed)/(account)/account.tsx",
"/(authed)/api/slug.js",
"/hooks/github.js",
"/(authed)/[org]/__foo.js",
"/(authed)/[org]/_middleware.tsx",
"/(authed)/[org]/index.ts",
];
routes.sort(() => Math.random() > 0.5 ? -1 : 1);
routes.sort(sortRoutePaths);
sorted = [
"/_app.js",
"/_error.tsx",
"/app/_middleware.tsx",
"/app.tsx",
"/auth/login.ts",
"/auth/logout.tsx",
"/hooks/github.js",
"/login.js",
"/(authed)/_middleware.tsx",
"/(authed)/_layout.jsx",
"/(authed)/index.js",
"/(authed)/api/slug.js",
"/(authed)/(account)/account.tsx",
"/(authed)/[org]/_middleware.tsx",
"/(authed)/[org]/__foo.js",
"/(authed)/[org]/index.ts",
];
expect(routes).toEqual(sorted);
}); });
Deno.test("fsRoutes - registers default GET route for component without GET handler", async () => { Deno.test("fsRoutes - registers default GET route for component without GET handler", async () => {