From ecc42850b9514107cc51253a855d094a174bd3b0 Mon Sep 17 00:00:00 2001 From: William Weiss Date: Tue, 21 Apr 2026 13:20:47 -0400 Subject: [PATCH] fix: in sortRoutePaths: sort files alphabetically (#3769) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3773 --------- Co-authored-by: Bartek IwaƄczuk Co-authored-by: Claude Opus 4.6 (1M context) --- packages/fresh/src/fs_routes.ts | 11 +++--- packages/fresh/src/fs_routes_test.tsx | 49 ++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/packages/fresh/src/fs_routes.ts b/packages/fresh/src/fs_routes.ts index fafb0dd5..a50826d9 100644 --- a/packages/fresh/src/fs_routes.ts +++ b/packages/fresh/src/fs_routes.ts @@ -246,8 +246,7 @@ export function sortRoutePaths(a: string, b: string) { const scoreB = getRoutePathScore(charB, b, bIdx); if (scoreA === scoreB) { if (charA !== charB) { - // TODO: Do we need localeSort here or is this good enough? - return charA < charB ? 0 : 1; + return charA < charB ? -1 : 1; } continue; } @@ -256,8 +255,7 @@ export function sortRoutePaths(a: string, b: string) { } if (charA !== charB) { - // TODO: Do we need localeSort here or is this good enough? - return charA < charB ? 0 : 1; + return charA < charB ? -1 : 1; } // 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 ( - i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" && - s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x" + char === "i" && s[i + 1] === "n" && + s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x" && + (i + 4 === s.length - 1 || s[i + 5] === ".") ) { return 3; } diff --git a/packages/fresh/src/fs_routes_test.tsx b/packages/fresh/src/fs_routes_test.tsx index 1dc22123..f6d6c59b 100644 --- a/packages/fresh/src/fs_routes_test.tsx +++ b/packages/fresh/src/fs_routes_test.tsx @@ -1315,6 +1315,8 @@ Deno.test("fsRoutes - sortRoutePaths with groups", () => { routes = [ "/_app", + "/app", + "/app/_middleware", "/(authed)/_middleware", "/(authed)/_layout", "/_error", @@ -1325,26 +1327,71 @@ Deno.test("fsRoutes - sortRoutePaths with groups", () => { "/(authed)/(account)/account", "/(authed)/api/slug", "/hooks/github", + "/(authed)/[org]/__foo", "/(authed)/[org]/_middleware", "/(authed)/[org]/index", ]; + routes.sort(() => Math.random() > 0.5 ? -1 : 1); routes.sort(sortRoutePaths); sorted = [ "/_app", "/_error", - "/login", + "/app/_middleware", + "/app", "/auth/login", "/auth/logout", "/hooks/github", + "/login", "/(authed)/_middleware", "/(authed)/_layout", "/(authed)/index", "/(authed)/api/slug", "/(authed)/(account)/account", "/(authed)/[org]/_middleware", + "/(authed)/[org]/__foo", "/(authed)/[org]/index", ]; 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 () => {