Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 6 additions & 25 deletions src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,37 +400,18 @@ export function sortRoutePaths(a: string, b: string) {
let aIdx = 0;
let bIdx = 0;
for (; aIdx < aLen && bIdx < bLen; aIdx++, bIdx++) {
let charA = a.charAt(aIdx);
let charB = b.charAt(bIdx);
const charA = a.charAt(aIdx);
const charB = b.charAt(bIdx);

// When comparing a grouped route with a non-grouped one, we
// need to skip over the group name to effectively compare the
// actual route.
if (charA === "(" && charB !== "(") {
aIdx++;

while (aIdx < aLen) {
charA = a.charAt(aIdx);
if (charA === ")") {
aIdx += 2;
charA = a.charAt(aIdx);
break;
}

aIdx++;
}
if (charB == "[") return -1;
return 1;
} else if (charB === "(" && charA !== "(") {
bIdx++;
while (bIdx < bLen) {
charB = b.charAt(bIdx);
if (charB === ")") {
bIdx += 2;
charB = b.charAt(bIdx);
break;
}

bIdx++;
}
if (charA == "[") return 1;
return -1;
}

if (charA === "/" || charB === "/") {
Expand Down
48 changes: 48 additions & 0 deletions src/plugins/fs_routes/mod_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,54 @@ Deno.test("fsRoutes - sortRoutePaths", () => {
expect(routes).toEqual(sorted);
});

Deno.test("fsRoutes - sortRoutePaths with groups", () => {
let routes = [
"/(authed)/_middleware.ts",
"/(authed)/index.ts",
"/about.tsx",
];
routes.sort(sortRoutePaths);
let sorted = [
"/about.tsx",
"/(authed)/_middleware.ts",
"/(authed)/index.ts",
];
expect(routes).toEqual(sorted);

routes = [
"/_app",
"/(authed)/_middleware",
"/(authed)/_layout",
"/_error",
"/(authed)/index",
"/login",
"/auth/login",
"/auth/logout",
"/(authed)/(account)/account",
"/(authed)/api/slug",
"/hooks/github",
"/(authed)/[org]/_middleware",
"/(authed)/[org]/index",
];
routes.sort(sortRoutePaths);
sorted = [
"/_app",
"/_error",
"/login",
"/auth/login",
"/auth/logout",
"/hooks/github",
"/(authed)/_middleware",
"/(authed)/_layout",
"/(authed)/index",
"/(authed)/api/slug",
"/(authed)/(account)/account",
"/(authed)/[org]/_middleware",
"/(authed)/[org]/index",
];
expect(routes).toEqual(sorted);
});

Deno.test("fsRoutes - registers default GET route for component without GET handler", async () => {
const server = await createServer<{ value: boolean }>({
"routes/noGetHandler.tsx": {
Expand Down
Loading