Skip to content

Commit 195291a

Browse files
mcanshchaance
authored andcommitted
fix(remix-dev/flat-routes): use index without leading underscore for colocation (#5160)
1 parent 19c524c commit 195291a

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

.changeset/fresh-needles-join.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"remix": patch
3+
"@remix-run/dev": patch
4+
---
5+
6+
use `index` without leading underscore for colocation
7+
8+
when using flat routes with folder colocation, use `index` without leading underscore for route
9+
10+
before:
11+
```
12+
app_.projects.$id.roadmap/
13+
_index.tsx
14+
chart.tsx
15+
update-timeline.server.tsx
16+
```
17+
18+
after:
19+
```
20+
app_.projects.$id.roadmap/
21+
index.tsx
22+
chart.tsx
23+
update-timeline.server.tsx
24+
```

packages/remix-dev/__tests__/flat-routes-test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createRoutePath,
55
flatRoutesUniversal,
66
getRouteSegments,
7+
isIndexRoute,
78
} from "../config/flat-routes";
89
import type { ConfigRoute } from "../config/routes";
910

@@ -25,10 +26,10 @@ describe("flatRoutes", () => {
2526
["nested/$slug", "/nested/:slug"],
2627
["flat.$slug", "/flat/:slug"],
2728
["flat.sub", "/flat/sub"],
28-
["nested/_index", "/nested"],
29+
["nested/index", "/nested"],
2930
["flat._index", "/flat"],
3031
["_index", undefined],
31-
["_layout/_index", undefined],
32+
["_layout/index", undefined],
3233
["_layout/test", "/test"],
3334
["_layout.test", "/test"],
3435
["_layout/$slug", "/:slug"],
@@ -88,7 +89,8 @@ describe("flatRoutes", () => {
8889
for (let [input, expected] of tests) {
8990
it(`"${input}" -> "${expected}"`, () => {
9091
let routeSegments = getRouteSegments(input);
91-
expect(createRoutePath(routeSegments)).toBe(expected);
92+
let isIndex = isIndexRoute(input);
93+
expect(createRoutePath(routeSegments, isIndex)).toBe(expected);
9294
});
9395
}
9496

@@ -332,9 +334,9 @@ describe("flatRoutes", () => {
332334
},
333335
],
334336
[
335-
"routes/app_.skipall_/_index.tsx",
337+
"routes/app_.skipall_/index.tsx",
336338
{
337-
id: "routes/app_.skipall_/_index",
339+
id: "routes/app_.skipall_/index",
338340
index: true,
339341
parentId: "root",
340342
path: "app/skipall",
@@ -580,9 +582,9 @@ describe("flatRoutes", () => {
580582
},
581583
],
582584
[
583-
"routes/brand/_index.tsx",
585+
"routes/brand/index.tsx",
584586
{
585-
id: "routes/brand/_index",
587+
id: "routes/brand/index",
586588
parentId: "root",
587589
path: "brand",
588590
index: true,

packages/remix-dev/config/flat-routes.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ export function flatRoutesUniversal(
103103
return routes;
104104
}
105105

106-
function isIndexRoute(routeId: string) {
107-
return routeId.endsWith("_index");
106+
export function isIndexRoute(routeId: string) {
107+
let isFlatFile = !routeId.includes(path.posix.sep);
108+
return isFlatFile
109+
? routeId.endsWith("_index")
110+
: /\/index$/.test(routeId);
108111
}
109112

110113
type State =
@@ -262,7 +265,7 @@ function getRouteInfo(
262265
let routeIdWithoutRoutes = routeId.slice(routeDirectory.length + 1);
263266
let index = isIndexRoute(routeIdWithoutRoutes);
264267
let routeSegments = getRouteSegments(routeIdWithoutRoutes);
265-
let routePath = createRoutePath(routeSegments);
268+
let routePath = createRoutePath(routeSegments, index);
266269

267270
return {
268271
id: routeIdWithoutRoutes,
@@ -274,9 +277,13 @@ function getRouteInfo(
274277
};
275278
}
276279

277-
export function createRoutePath(routeSegments: string[]) {
280+
export function createRoutePath(routeSegments: string[], isIndex: boolean) {
278281
let result = "";
279282

283+
if (isIndex) {
284+
routeSegments = routeSegments.slice(0, -1);
285+
}
286+
280287
for (let segment of routeSegments) {
281288
// skip pathless layout segments
282289
if (segment.startsWith("_")) {

0 commit comments

Comments
 (0)