Skip to content

Commit 32665f9

Browse files
committed
Updates
1 parent cb3cfec commit 32665f9

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

integration/vite-spa-mode-test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,9 @@ test.describe("SPA Mode", () => {
10681068
});
10691069
});
10701070

1071-
test("only imports top-level route modules when SSRing index.html", async ({ page }) => {
1071+
test("only imports the root route in the server build when SSRing index.html", async ({
1072+
page,
1073+
}) => {
10721074
let fixture = await createFixture({
10731075
spaMode: true,
10741076
files: {
@@ -1123,6 +1125,9 @@ test.describe("SPA Mode", () => {
11231125
import { logImport } from "../routeImportTracker";
11241126
logImport("app/routes/_index.tsx");
11251127
1128+
// This should not cause an error on SSr because the module is not loaded
1129+
console.log(window);
1130+
11261131
export default function Component() {
11271132
return "index";
11281133
}
@@ -1132,6 +1137,9 @@ test.describe("SPA Mode", () => {
11321137
import { logImport } from "../routeImportTracker";
11331138
logImport("app/routes/about.tsx");
11341139
1140+
// This should not cause an error on SSr because the module is not loaded
1141+
console.log(window);
1142+
11351143
export default function Component() {
11361144
const [mounted, setMounted] = React.useState(false);
11371145
React.useEffect(() => setMounted(true), []);
@@ -1146,11 +1154,18 @@ test.describe("SPA Mode", () => {
11461154
},
11471155
});
11481156

1149-
let importedRoutes = (await fs.promises.readFile(path.join(fixture.projectDir, "ssr-route-imports.txt"), "utf-8")).trim().split("\n");
1157+
let importedRoutes = (
1158+
await fs.promises.readFile(
1159+
path.join(fixture.projectDir, "ssr-route-imports.txt"),
1160+
"utf-8"
1161+
)
1162+
)
1163+
.trim()
1164+
.split("\n");
11501165
expect(importedRoutes).toStrictEqual([
11511166
"app/root.tsx",
1152-
"app/routes/_index.tsx"
1153-
// we should NOT have imported app/routes/about.tsx
1167+
// we should not have imported app/routes/_index.tsx
1168+
// we should not have imported app/routes/about.tsx
11541169
]);
11551170

11561171
appFixture = await createAppFixture(fixture);
@@ -1160,7 +1175,7 @@ test.describe("SPA Mode", () => {
11601175
// @ts-expect-error
11611176
expect(await page.evaluate(() => window.csrRouteImports)).toStrictEqual([
11621177
"app/root.tsx",
1163-
"app/routes/about.tsx"
1178+
"app/routes/about.tsx",
11641179
]);
11651180
});
11661181
});

packages/react-router-dev/vite/plugin.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
597597
routes
598598
);
599599

600-
let isSpaMode =
601-
!ctx.reactRouterConfig.ssr && ctx.reactRouterConfig.prerender == null;
602-
603-
let routeIdsToImport = new Set(Object.keys(routes));
604-
if (isSpaMode) {
605-
// In SPA mode, we only pre-render the top-level index route; for all
606-
// other routes we stub out their imports, as they (and their deps) may
607-
// not be compatible with server-side rendering. This also helps keep
608-
// the build fast
609-
routeIdsToImport = getRootRouteIds(routes);
610-
}
600+
let isSpaMode = isSpaModeEnabled(ctx.reactRouterConfig);
611601

612602
return `
613603
import * as entryServer from ${JSON.stringify(
@@ -616,17 +606,19 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
616606
${Object.keys(routes)
617607
.map((key, index) => {
618608
let route = routes[key]!;
619-
if (routeIdsToImport.has(key)) {
609+
if (isSpaMode && key !== "root") {
610+
// In SPA mode, we only pre-render to the root route and it's `HydrateFallback`.
611+
// Therefore, we can stub all other routes with an empty module as they
612+
// (and their deps) may not be compatible with server-side rendering.
613+
// This also helps keep the build fast.
614+
return `const route${index} = { default: () => null };`;
615+
} else {
620616
return `import * as route${index} from ${JSON.stringify(
621617
resolveFileUrl(
622618
ctx,
623619
resolveRelativeRouteFilePath(route, ctx.reactRouterConfig)
624620
)
625621
)};`;
626-
} else {
627-
// we're not importing the route since we won't be rendering
628-
// it via SSR; just stub it out
629-
return `const route${index} = { default: () => null };`;
630622
}
631623
})
632624
.join("\n")}
@@ -2605,17 +2597,6 @@ function groupRoutesByParentId(manifest: GenericRouteManifest) {
26052597
return routes;
26062598
}
26072599

2608-
/**
2609-
* Return the route ids associated with the top-level index route
2610-
*
2611-
* i.e. "root", the top-level index route's id, and (if applicable) the ids of
2612-
* any top-level layout/path-less routes in between
2613-
*/
2614-
function getRootRouteIds(manifest: GenericRouteManifest): Set<string> {
2615-
const matches = matchRoutes(createPrerenderRoutes(manifest), "/");
2616-
return new Set(matches?.filter(Boolean).map((m) => m.route.id) || []);
2617-
}
2618-
26192600
// Create a skeleton route tree of paths
26202601
function createPrerenderRoutes(
26212602
manifest: GenericRouteManifest,

0 commit comments

Comments
 (0)