Skip to content

Commit f245471

Browse files
fix: route middleware order with groups (#2811)
This is a follow-up fix to #2792 The original fix skipped over route groups which lead to them being mixed with non-grouped routes. This in turn confused the middleware processing part when building the final routes.
1 parent c7c341b commit f245471

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

src/plugins/fs_routes/mod.ts

+6-25
Original file line numberDiff line numberDiff line change
@@ -400,37 +400,18 @@ export function sortRoutePaths(a: string, b: string) {
400400
let aIdx = 0;
401401
let bIdx = 0;
402402
for (; aIdx < aLen && bIdx < bLen; aIdx++, bIdx++) {
403-
let charA = a.charAt(aIdx);
404-
let charB = b.charAt(bIdx);
403+
const charA = a.charAt(aIdx);
404+
const charB = b.charAt(bIdx);
405405

406406
// When comparing a grouped route with a non-grouped one, we
407407
// need to skip over the group name to effectively compare the
408408
// actual route.
409409
if (charA === "(" && charB !== "(") {
410-
aIdx++;
411-
412-
while (aIdx < aLen) {
413-
charA = a.charAt(aIdx);
414-
if (charA === ")") {
415-
aIdx += 2;
416-
charA = a.charAt(aIdx);
417-
break;
418-
}
419-
420-
aIdx++;
421-
}
410+
if (charB == "[") return -1;
411+
return 1;
422412
} else if (charB === "(" && charA !== "(") {
423-
bIdx++;
424-
while (bIdx < bLen) {
425-
charB = b.charAt(bIdx);
426-
if (charB === ")") {
427-
bIdx += 2;
428-
charB = b.charAt(bIdx);
429-
break;
430-
}
431-
432-
bIdx++;
433-
}
413+
if (charA == "[") return 1;
414+
return -1;
434415
}
435416

436417
if (charA === "/" || charB === "/") {

src/plugins/fs_routes/mod_test.tsx

+48
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,54 @@ Deno.test("fsRoutes - sortRoutePaths", () => {
11641164
expect(routes).toEqual(sorted);
11651165
});
11661166

1167+
Deno.test("fsRoutes - sortRoutePaths with groups", () => {
1168+
let routes = [
1169+
"/(authed)/_middleware.ts",
1170+
"/(authed)/index.ts",
1171+
"/about.tsx",
1172+
];
1173+
routes.sort(sortRoutePaths);
1174+
let sorted = [
1175+
"/about.tsx",
1176+
"/(authed)/_middleware.ts",
1177+
"/(authed)/index.ts",
1178+
];
1179+
expect(routes).toEqual(sorted);
1180+
1181+
routes = [
1182+
"/_app",
1183+
"/(authed)/_middleware",
1184+
"/(authed)/_layout",
1185+
"/_error",
1186+
"/(authed)/index",
1187+
"/login",
1188+
"/auth/login",
1189+
"/auth/logout",
1190+
"/(authed)/(account)/account",
1191+
"/(authed)/api/slug",
1192+
"/hooks/github",
1193+
"/(authed)/[org]/_middleware",
1194+
"/(authed)/[org]/index",
1195+
];
1196+
routes.sort(sortRoutePaths);
1197+
sorted = [
1198+
"/_app",
1199+
"/_error",
1200+
"/login",
1201+
"/auth/login",
1202+
"/auth/logout",
1203+
"/hooks/github",
1204+
"/(authed)/_middleware",
1205+
"/(authed)/_layout",
1206+
"/(authed)/index",
1207+
"/(authed)/api/slug",
1208+
"/(authed)/(account)/account",
1209+
"/(authed)/[org]/_middleware",
1210+
"/(authed)/[org]/index",
1211+
];
1212+
expect(routes).toEqual(sorted);
1213+
});
1214+
11671215
Deno.test("fsRoutes - registers default GET route for component without GET handler", async () => {
11681216
const server = await createServer<{ value: boolean }>({
11691217
"routes/noGetHandler.tsx": {

0 commit comments

Comments
 (0)