Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit f02e16e

Browse files
committed
Fix matchRoutes can't match index page
1 parent 1e322a7 commit f02e16e

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/route.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ export function matchRoutes(
5252
}
5353
}
5454
}
55+
if (matches.length === 0) {
56+
// find index route
57+
for (const [p, m] of routes) {
58+
if (m.pattern.pathname.endsWith("/index")) {
59+
const ret = p.exec({ host: url.host, pathname: pathname + "/index" });
60+
if (ret) {
61+
matches.push([ret, m]);
62+
break;
63+
}
64+
}
65+
}
66+
}
5567
if (matches.length > 0) {
5668
const directMatch = matches[matches.length - 1][1];
5769
const parts = util.splitPath(pathname);
@@ -71,7 +83,7 @@ export function matchRoutes(
7183
}
7284

7385
if (directMatch.nesting) {
74-
// find the nesting index of the route
86+
// find index route
7587
for (const [p, m] of routes) {
7688
if (m.pattern.pathname === directMatch.pattern.pathname + "/index") {
7789
const ret = p.exec({ host: url.host, pathname: pathname + "/index" });

tests/lib_route_test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ Deno.test("lib/helpers.ts: matchRoutes", async () => {
1515
"./routes/docs/index.mdx",
1616
"./routes/index.tsx",
1717
"./routes/utils.ts",
18-
"./routes/post/[date]/[...slug].tsx",
1918
"./routes/works.tsx",
2019
"./routes/works/$id.tsx",
2120
"./routes/works/$id/$page+.tsx",
2221
"./routes/works/$id/index.tsx",
2322
"./routes/works/$id/order.tsx",
2423
"./routes/works/index.tsx",
2524
"./routes/works/new.tsx",
25+
"./routes/users/index.tsx",
26+
"./routes/users/$uid.tsx",
27+
"./routes/users/$uid/index.tsx",
28+
"./routes/users/$uid/settings/$page.tsx",
29+
"./routes/post/[date]/[...slug].tsx",
2630
];
2731
await Promise.all(files.map((file) => Deno.mkdir(join(tmpDir, dirname(file)), { recursive: true })));
2832
await Promise.all(files.map((file) => Deno.writeTextFile(join(tmpDir, file), "")));
2933
const routes = await initRoutes("./routes/**/*.{tsx,mdx}", tmpDir);
3034
assertEquals(routes.routes.length, files.length - 1);
31-
assertEquals(routes.routes.filter(([_, meta]) => meta.nesting).length, 4);
35+
assertEquals(routes.routes.filter(([_, meta]) => meta.nesting).length, 5);
3236

3337
let matches = matchRoutes(new URL("/", "http://localhost:3000"), routes);
3438
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/"]);
@@ -119,4 +123,30 @@ Deno.test("lib/helpers.ts: matchRoutes", async () => {
119123
"./routes/works/$id.tsx",
120124
"./routes/works/$id/$page+.tsx",
121125
]);
126+
127+
matches = matchRoutes(new URL("/users", "http://localhost:3000"), routes);
128+
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/index"]);
129+
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}]);
130+
assertEquals(matches.map(([_, meta]) => meta.filename), [
131+
"./routes/_app.tsx",
132+
"./routes/users/index.tsx",
133+
]);
134+
135+
matches = matchRoutes(new URL("/users/ije", "http://localhost:3000"), routes);
136+
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/ije", "/users/ije/index"]);
137+
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { uid: "ije" }, { uid: "ije" }]);
138+
assertEquals(matches.map(([_, meta]) => meta.filename), [
139+
"./routes/_app.tsx",
140+
"./routes/users/$uid.tsx",
141+
"./routes/users/$uid/index.tsx",
142+
]);
143+
144+
matches = matchRoutes(new URL("/users/ije/settings/profile", "http://localhost:3000"), routes);
145+
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/ije", "/users/ije/settings/profile"]);
146+
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { uid: "ije" }, { uid: "ije", page: "profile" }]);
147+
assertEquals(matches.map(([_, meta]) => meta.filename), [
148+
"./routes/_app.tsx",
149+
"./routes/users/$uid.tsx",
150+
"./routes/users/$uid/settings/$page.tsx",
151+
]);
122152
});

0 commit comments

Comments
 (0)