Skip to content

Fix validation of split route modules for root route #13238

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

Merged
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/cuddly-dots-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

When `future.unstable_splitRouteModules` is set to `"enforce"`, allow both splittable and unsplittable root route exports since it's always in a single chunk.
53 changes: 53 additions & 0 deletions integration/split-route-modules-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,59 @@ test.describe("Split route modules", async () => {
});
});

test.describe("splittable routes with splittable root route exports", () => {
test.beforeAll(async () => {
port = await getPort();
cwd = await createProject({
"react-router.config.ts": reactRouterConfig({ splitRouteModules }),
"vite.config.js": await viteConfig.basic({ port }),
"app/root.tsx": js`
import { Outlet } from "react-router";
export const clientLoader = () => null;
export const clientAction = () => null;
export default function() {
return <Outlet />;
}
`,
// Make unsplittable routes valid so the build can pass
"app/routes/unsplittable.tsx": "export default function(){}",
"app/routes/mixed.tsx": "export default function(){}",
});
});

test("build passes", async () => {
let { status } = build({ cwd });
expect(status).toBe(0);
});
});

test.describe("splittable routes with unsplittable root route exports", () => {
test.beforeAll(async () => {
port = await getPort();
cwd = await createProject({
"react-router.config.ts": reactRouterConfig({ splitRouteModules }),
"vite.config.js": await viteConfig.basic({ port }),
"app/root.tsx": js`
import { Outlet } from "react-router";
const shared = null;
export const clientLoader = () => shared;
export const clientAction = () => shared;
export default function() {
return <Outlet />;
}
`,
// Make unsplittable routes valid so the build can pass
"app/routes/unsplittable.tsx": "export default function(){}",
"app/routes/mixed.tsx": "export default function(){}",
});
});

test("build passes", async () => {
let { status } = build({ cwd });
expect(status).toBe(0);
});
});

test.describe("unsplittable routes", () => {
test.beforeAll(async () => {
port = await getPort();
Expand Down
16 changes: 12 additions & 4 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3056,6 +3056,13 @@ const resolveRouteFileCode = async (
);
};

function isRootRouteModuleId(ctx: ReactRouterPluginContext, id: string) {
return (
normalizeRelativeFilePath(id, ctx.reactRouterConfig) ===
ctx.reactRouterConfig.routes.root.file
);
}

async function detectRouteChunksIfEnabled(
cache: Cache,
ctx: ReactRouterPluginContext,
Expand All @@ -3082,10 +3089,7 @@ async function detectRouteChunksIfEnabled(
// for all requests, all of its chunks would always be loaded up front during
// the initial page load. Instead of firing off multiple requests to resolve
// the root route code, we want it to be downloaded in a single request.
if (
normalizeRelativeFilePath(id, ctx.reactRouterConfig) ===
ctx.reactRouterConfig.routes.root.file
) {
if (isRootRouteModuleId(ctx, id)) {
return noRouteChunks();
}

Expand Down Expand Up @@ -3130,6 +3134,10 @@ function validateRouteChunks({
id: string;
valid: Record<Exclude<RouteChunkName, "main">, boolean>;
}): void {
if (isRootRouteModuleId(ctx, id)) {
return;
}

let invalidChunks = Object.entries(valid)
.filter(([_, isValid]) => !isValid)
.map(([chunkName]) => chunkName);
Expand Down