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

feat(remix-dev): support root route as a folder #7256

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changeset/cyan-pumpkins-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@remix-run/dev": minor
---

Support root route as a folder.

Now your root route can be either a file (`app/root.tsx`) or a folder (`app/root/route.tsx`) just like any other route. This is handy for organizational purposes if your root route depends on a lot of components imported from other files.
12 changes: 12 additions & 0 deletions docs/file-conventions/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ app/routes/app._index.tsx
app/routes/app._index/route.tsx
```

Even your root route can be a folder. For example:

```text
app/
├── root/
| ├── footer.tsx
| ├── menu.tsx
| └── route.tsx
└── routes/
├── _index.tsx
└── about.tsx

## Scaling

Our general recommendation for scale is to make every route a folder and put the modules used exclusively by that route in the folder, then put the shared modules outside of routes folder elsewhere. This has a couple benefits:
Expand Down
5 changes: 5 additions & 0 deletions integration/helpers/create-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ async function writeTestFiles(init: FixtureInit, dir: string) {
let filePath = path.join(dir, filename);
await fse.ensureDir(path.dirname(filePath));
let file = init.files![filename];
// if we have a app/root/route.tsx then we don't want app/root.tsx to exist
if (filename.endsWith("root/route.tsx")) {
let rootTsx = path.join(dir, "app", "root.tsx");
await fse.remove(rootTsx);
}

await fse.writeFile(filePath, stripIndent(file));
})
Expand Down
75 changes: 75 additions & 0 deletions integration/root-route-folder-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { test, expect } from "@playwright/test";

import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
import {
createAppFixture,
createFixture,
js,
} from "./helpers/create-fixture.js";

let fixture: Fixture;
let appFixture: AppFixture;

test.describe("root route as a folder", () => {
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root/route.tsx": js`
import { Links, Meta, Outlet, Scripts } from "@remix-run/react";

export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<div id="content">
<h1>Root</h1>
<Outlet />
</div>
<Scripts />
</body>
</html>
);
}
`,

"app/routes/_index.tsx": js`
export default function () {
return <h2>Index</h2>;
}
`,
},
});

appFixture = await createAppFixture(fixture);
});

test.afterAll(() => {
appFixture.close();
});

test.describe("without JavaScript", () => {
test.use({ javaScriptEnabled: false });
runTests();
});

test.describe("with JavaScript", () => {
test.use({ javaScriptEnabled: true });
runTests();
});

function runTests() {
test("renders matching routes (index)", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
expect(await app.getHtml("#content")).toBe(`<div id="content">
<h1>Root</h1>
<h2>Index</h2>
</div>`);
});
}
});
8 changes: 6 additions & 2 deletions packages/remix-dev/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type { RouteManifest, DefineRoutesFunction } from "./config/routes";
import { defineRoutes } from "./config/routes";
import { ServerMode, isValidServerMode } from "./config/serverModes";
import { serverBuildVirtualModule } from "./compiler/server/virtualModules";
import { flatRoutes } from "./config/flat-routes";
import {
findRootRoute,
flatRoutes,
} from "./config/flat-routes";
import { detectPackageManager } from "./cli/detectPackageManager";
import { logger } from "./tux";

Expand Down Expand Up @@ -523,10 +526,11 @@ export async function resolveConfig(

let publicPath = addTrailingSlash(appConfig.publicPath || "/build/");

let rootRouteFile = findEntry(appDirectory, "root");
let rootRouteFile = findRootRoute(appDirectory);
if (!rootRouteFile) {
throw new Error(`Missing "root" route file in ${appDirectory}`);
}
rootRouteFile = path.relative(appDirectory, rootRouteFile);

let routes: RouteManifest = {
root: { path: "", id: "root", file: rootRouteFile },
Expand Down
17 changes: 16 additions & 1 deletion packages/remix-dev/config/flat-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ class PrefixLookupTrie {
}
}

export function findRootRoute(appDirectory: string) {
let routeFileForFolder = findRouteModuleForFolder(
appDirectory,
path.join(appDirectory, "root"),
[]
);
let routeFileForFile = findConfig(appDirectory, "root", routeModuleExts);
if (routeFileForFolder && routeFileForFile) {
throw new Error(
`Conflicting root route modules: ${routeFileForFolder}, ${routeFileForFile}`
);
}
return routeFileForFolder || routeFileForFile;
}

export function flatRoutes(
appDirectory: string,
ignoredFilePatterns: string[] = [],
Expand All @@ -82,7 +97,7 @@ export function flatRoutes(
.filter((re: any): re is RegExp => !!re);
let routesDir = path.join(appDirectory, prefix);

let rootRoute = findConfig(appDirectory, "root", routeModuleExts);
let rootRoute = findRootRoute(appDirectory);

if (!rootRoute) {
throw new Error(
Expand Down