Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v1-route-convention): allow sibling pathless layout routes #20

Merged
merged 3 commits into from
May 24, 2023
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/sibling-pathless-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/v1-route-convention": patch
---

Allow sibling pathless layout routes (from https://github.com/remix-run/remix/pull/4421)
88 changes: 88 additions & 0 deletions packages/v1-route-convention/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,92 @@ describe("defineConventionalRoutes", () => {

expect(routes).toMatchObject(expected);
});

it("supports sibling pathless layout routes", () => {
let routes = createRoutesFromFolders(defineRoutes, {
appDirectory: path.join(
__dirname,
"fixtures",
"sibling-pathless-layout-routes"
),
});

expect(routes).toEqual({
"routes/__a": {
file: path.join("routes", "__a.tsx"),
id: "routes/__a",
parentId: "root",
},
"routes/__a/a": {
file: path.join("routes", "__a", "a.tsx"),
id: "routes/__a/a",
parentId: "routes/__a",
path: "a",
},
"routes/__a/index": {
file: path.join("routes", "__a", "index.tsx"),
id: "routes/__a/index",
index: true,
parentId: "routes/__a",
},
"routes/__b": {
file: path.join("routes", "__b.tsx"),
id: "routes/__b",
parentId: "root",
},
"routes/__b/b": {
caseSensitive: undefined,
file: path.join("routes", "__b", "b.tsx"),
id: "routes/__b/b",
index: undefined,
parentId: "routes/__b",
path: "b",
},
});
});

// See: https://github.com/remix-run/remix/discussions/3014
it("handles sibling pathless and index routes", () => {
let routes = createRoutesFromFolders(defineRoutes, {
appDirectory: path.join(__dirname, "fixtures", "blog-collision"),
});

expect(routes).toEqual({
"routes/blog/__nested": {
file: path.join("routes", "blog", "__nested.tsx"),
id: "routes/blog/__nested",
parentId: "root",
path: "blog",
},
"routes/blog/__nested/new": {
file: path.join("routes", "blog", "__nested", "new.tsx"),
id: "routes/blog/__nested/new",
parentId: "routes/blog/__nested",
path: "new",
},
"routes/blog/index": {
file: path.join("routes", "blog", "index.tsx"),
id: "routes/blog/index",
index: true,
parentId: "root",
path: "blog",
},
"routes/index": {
file: path.join("routes", "index.tsx"),
id: "routes/index",
index: true,
parentId: "root",
},
});
});

it("warns on route collisions", () => {
expect(() =>
createRoutesFromFolders(defineRoutes, {
appDirectory: path.join(__dirname, "fixtures", "route-collisions"),
})
).toThrowErrorMatchingInlineSnapshot(
'"Path \\"a\\" defined by route \\"routes/__b/a\\" conflicts with route \\"routes/__a/a\\""'
);
});
});
53 changes: 46 additions & 7 deletions packages/v1-route-convention/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,52 @@ export function createRoutesFromFolders(
let isIndexRoute = routeId.endsWith("/index");
let fullPath = createRoutePath(routeId.slice(routesDirectory.length + 1));
let uniqueRouteId = (fullPath || "") + (isIndexRoute ? "?index" : "");

if (uniqueRouteId) {
let isPathlessLayoutRoute =
routeId.split("/").pop()?.startsWith("__") === true;

/**
* We do not try to detect path collisions for pathless layout route
* files because, by definition, they create the potential for route
* collisions _at that level in the tree_.
*
* Consider example where a user may want multiple pathless layout routes
* for different subfolders
*
* routes/
* account.tsx
* account/
* __public/
* login.tsx
* perks.tsx
* __private/
* orders.tsx
* profile.tsx
* __public.tsx
* __private.tsx
*
* In order to support both a public and private layout for `/account/*`
* URLs, we are creating a mutually exclusive set of URLs beneath 2
* separate pathless layout routes. In this case, the route paths for
* both account/__public.tsx and account/__private.tsx is the same
* (/account), but we're again not expecting to match at that level.
*
* By only ignoring this check when the final portion of the filename is
* pathless, we will still detect path collisions such as:
*
* routes/parent/__pathless/foo.tsx
* routes/parent/__pathless2/foo.tsx
*
* and
*
* routes/parent/__pathless/index.tsx
* routes/parent/__pathless2/index.tsx
*/
if (uniqueRouteId && !isPathlessLayoutRoute) {
if (uniqueRoutes.has(uniqueRouteId)) {
throw new Error(
`Path ${JSON.stringify(fullPath)} defined by route ${JSON.stringify(
routeId
)} conflicts with route ${JSON.stringify(
uniqueRoutes.get(uniqueRouteId)
)}`
`Path ${JSON.stringify(fullPath || "/")} defined by route ` +
`${JSON.stringify(routeId)} conflicts with route ` +
`${JSON.stringify(uniqueRoutes.get(uniqueRouteId))}`
);
} else {
uniqueRoutes.set(uniqueRouteId, routeId);
Expand Down Expand Up @@ -268,6 +305,8 @@ export function createRoutePath(partialRouteId: string): string | undefined {

if (rawSegmentBuffer === "index" && result.endsWith("index")) {
result = result.replace(/\/?index$/, "");
} else {
result = result.replace(/\/$/, "");
}

if (rawSegmentBuffer === "index" && result.endsWith("index?")) {
Expand Down