Skip to content

Commit

Permalink
fix(remix-dev): normalize /route routeId
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Feb 15, 2023
1 parent 7c5b6b5 commit c74e086
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
13 changes: 5 additions & 8 deletions packages/remix-dev/__tests__/flat-routes-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from "node:path";

import {
createRoutePath,
flatRoutesUniversal,
getRouteInfo,
getRouteSegments,
isIndexRoute,
} from "../config/flat-routes";
import type { ConfigRoute } from "../config/routes";

Expand Down Expand Up @@ -88,11 +87,9 @@ describe("flatRoutes", () => {

for (let [input, expected] of tests) {
it(`"${input}" -> "${expected}"`, () => {
let isIndex = isIndexRoute(input);
let [routeSegments, rawRouteSegments] = getRouteSegments(input);
expect(createRoutePath(routeSegments, rawRouteSegments, isIndex)).toBe(
expected
);
let fullRoutePath = path.join(APP_DIR, "routes", `${input}.tsx`);
let routeInfo = getRouteInfo(APP_DIR, "routes", fullRoutePath);
expect(routeInfo.path).toBe(expected);
});
}

Expand Down Expand Up @@ -276,7 +273,7 @@ describe("flatRoutes", () => {
[
"routes/folder/route.tsx",
{
id: "routes/folder/route",
id: "routes/folder",
parentId: "root",
path: "folder",
},
Expand Down
38 changes: 23 additions & 15 deletions packages/remix-dev/config/flat-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,6 @@ export function getRouteSegments(routeId: string) {
let routeSegment = "";
let rawRouteSegment = "";
let state: State = "NORMAL";
let hasFolder = routeId.includes(path.posix.sep);

/**
* @see https://github.com/remix-run/remix/pull/5160#issuecomment-1402157424
*/
if (hasFolder && (routeId.endsWith("/index") || routeId.endsWith("/route"))) {
let last = routeId.lastIndexOf(path.posix.sep);
if (last >= 0) {
routeId = routeId.substring(0, last);
}
}

let pushRouteSegment = (segment: string, rawSegment: string) => {
if (!segment) return;
Expand Down Expand Up @@ -289,7 +278,7 @@ function findParentRouteId(
return undefined;
}

function getRouteInfo(
export function getRouteInfo(
appDirectory: string,
routeDirectory: string,
filePath: string
Expand Down Expand Up @@ -381,10 +370,29 @@ function isRouteModuleFile(filePath: string) {
return basename.endsWith(`/route`) || basename.endsWith(`/index`);
}

function createFlatRouteId(filePath: string) {
/**
* @see https://github.com/remix-run/remix/pull/5160#issuecomment-1402157424
* normalize routeId
* remove `/index` and `/route` suffixes
* they should be treated like if they weren't folders
* e.g. `/dashboard` and `/dashboard/index` should be the same route
* e.g. `/dashboard` and `/dashboard/route` should be the same route
*/
function isRouteInFolder(routeId: string) {
return (
(routeId.endsWith("/index") || routeId.endsWith("/route")) &&
routeId.includes(path.posix.sep)
);
}

export function createFlatRouteId(filePath: string) {
let routeId = createRouteId(filePath);
if (routeId.includes(path.posix.sep) && routeId.endsWith("/index")) {
routeId = routeId.split(path.posix.sep).slice(0, -1).join(path.posix.sep);

if (isRouteInFolder(routeId)) {
let last = routeId.lastIndexOf(path.posix.sep);
if (last >= 0) {
routeId = routeId.substring(0, last);
}
}
return routeId;
}

0 comments on commit c74e086

Please sign in to comment.