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
5 changes: 5 additions & 0 deletions .changeset/index-suffix-segment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"filesystem-routing": patch
---

Only strip a whole `index` segment when mapping route files to paths. Route files whose names merely end in "index", such as `routes/reindex.tsx`, were previously mapped to `/re` instead of `/reindex`.
3 changes: 2 additions & 1 deletion src/convention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function routePathFromFile(routeFile: string): string {
const routePath = routeFile
// remove the initial slash
.slice(1)
.replace(/index$/, "")
// only strip a whole `index` segment, not a name that merely ends in "index" (e.g. `reindex`)
.replace(/(^|\/)index$/, "$1")
.replace(/\[([^/]+)\]/g, (_, m) => {
if (m.length > 3 && m.startsWith("...")) {
return `*${m.slice(3)}`;
Expand Down
23 changes: 23 additions & 0 deletions test/convention.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ describe("routePathFromFile", () => {
it("retains group segments for emission adapters", () => {
expect(routePathFromFile("/(marketing)/about")).toBe("/(marketing)/about");
});

// https://github.com/solidjs/solid-start/issues/2314
it("only strips index as a whole segment, not as a filename suffix", () => {
expect(routePathFromFile("/reindex")).toBe("/reindex");
expect(routePathFromFile("/myindex")).toBe("/myindex");
expect(routePathFromFile("/appendix")).toBe("/appendix");
expect(routePathFromFile("/blog/reindex")).toBe("/blog/reindex");
expect(routePathFromFile("/reindex/index")).toBe("/reindex/");
expect(routePathFromFile("/[id]/index")).toBe("/:id/");
});
});

const temporaryDirectories: string[] = [];
Expand Down Expand Up @@ -75,6 +85,19 @@ describe("PageFileSystemRouter", () => {
expect(post.$$route?.pick).toEqual(["route"]);
});

it("keeps route files whose names end in index at their own path", async () => {
const dir = createRouteTree({
"reindex.tsx": "export default () => <h1>Reindex</h1>;",
"blog/index.tsx": "export default () => <h1>Blog</h1>;",
"blog/myindex.tsx": "export default () => <h1>My index</h1>;"
});
const router = new PageFileSystemRouter({ dir, extensions: ["tsx"] });

const paths = (await router.getRoutes()).map(route => route.path).sort();

expect(paths).toEqual(["/blog/", "/blog/myindex", "/reindex"]);
});

it("resolves a relative dir against the current working directory", async () => {
const dir = createRouteTree({
"index.tsx": "export default () => <h1>Home</h1>;",
Expand Down
Loading