Skip to content

Commit ecc4285

Browse files
will-weissbartlomiejuclaude
authored
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>
1 parent 36dd637 commit ecc4285

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

packages/fresh/src/fs_routes.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ export function sortRoutePaths(a: string, b: string) {
246246
const scoreB = getRoutePathScore(charB, b, bIdx);
247247
if (scoreA === scoreB) {
248248
if (charA !== charB) {
249-
// TODO: Do we need localeSort here or is this good enough?
250-
return charA < charB ? 0 : 1;
249+
return charA < charB ? -1 : 1;
251250
}
252251
continue;
253252
}
@@ -256,8 +255,7 @@ export function sortRoutePaths(a: string, b: string) {
256255
}
257256

258257
if (charA !== charB) {
259-
// TODO: Do we need localeSort here or is this good enough?
260-
return charA < charB ? 0 : 1;
258+
return charA < charB ? -1 : 1;
261259
}
262260

263261
// 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 {
292290
}
293291

294292
if (
295-
i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" &&
296-
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x"
293+
char === "i" && s[i + 1] === "n" &&
294+
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x" &&
295+
(i + 4 === s.length - 1 || s[i + 5] === ".")
297296
) {
298297
return 3;
299298
}

packages/fresh/src/fs_routes_test.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,8 @@ Deno.test("fsRoutes - sortRoutePaths with groups", () => {
13151315

13161316
routes = [
13171317
"/_app",
1318+
"/app",
1319+
"/app/_middleware",
13181320
"/(authed)/_middleware",
13191321
"/(authed)/_layout",
13201322
"/_error",
@@ -1325,26 +1327,71 @@ Deno.test("fsRoutes - sortRoutePaths with groups", () => {
13251327
"/(authed)/(account)/account",
13261328
"/(authed)/api/slug",
13271329
"/hooks/github",
1330+
"/(authed)/[org]/__foo",
13281331
"/(authed)/[org]/_middleware",
13291332
"/(authed)/[org]/index",
13301333
];
1334+
routes.sort(() => Math.random() > 0.5 ? -1 : 1);
13311335
routes.sort(sortRoutePaths);
13321336
sorted = [
13331337
"/_app",
13341338
"/_error",
1335-
"/login",
1339+
"/app/_middleware",
1340+
"/app",
13361341
"/auth/login",
13371342
"/auth/logout",
13381343
"/hooks/github",
1344+
"/login",
13391345
"/(authed)/_middleware",
13401346
"/(authed)/_layout",
13411347
"/(authed)/index",
13421348
"/(authed)/api/slug",
13431349
"/(authed)/(account)/account",
13441350
"/(authed)/[org]/_middleware",
1351+
"/(authed)/[org]/__foo",
13451352
"/(authed)/[org]/index",
13461353
];
13471354
expect(routes).toEqual(sorted);
1355+
1356+
routes = [
1357+
"/_app.js",
1358+
"/app.tsx",
1359+
"/app/_middleware.tsx",
1360+
"/(authed)/_middleware.tsx",
1361+
"/(authed)/_layout.jsx",
1362+
"/_error.tsx",
1363+
"/(authed)/index.js",
1364+
"/login.js",
1365+
"/auth/logout.tsx",
1366+
"/auth/login.ts",
1367+
"/(authed)/(account)/account.tsx",
1368+
"/(authed)/api/slug.js",
1369+
"/hooks/github.js",
1370+
"/(authed)/[org]/__foo.js",
1371+
"/(authed)/[org]/_middleware.tsx",
1372+
"/(authed)/[org]/index.ts",
1373+
];
1374+
routes.sort(() => Math.random() > 0.5 ? -1 : 1);
1375+
routes.sort(sortRoutePaths);
1376+
sorted = [
1377+
"/_app.js",
1378+
"/_error.tsx",
1379+
"/app/_middleware.tsx",
1380+
"/app.tsx",
1381+
"/auth/login.ts",
1382+
"/auth/logout.tsx",
1383+
"/hooks/github.js",
1384+
"/login.js",
1385+
"/(authed)/_middleware.tsx",
1386+
"/(authed)/_layout.jsx",
1387+
"/(authed)/index.js",
1388+
"/(authed)/api/slug.js",
1389+
"/(authed)/(account)/account.tsx",
1390+
"/(authed)/[org]/_middleware.tsx",
1391+
"/(authed)/[org]/__foo.js",
1392+
"/(authed)/[org]/index.ts",
1393+
];
1394+
expect(routes).toEqual(sorted);
13481395
});
13491396

13501397
Deno.test("fsRoutes - registers default GET route for component without GET handler", async () => {

0 commit comments

Comments
 (0)