Skip to content
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/fix-spa-vite-preview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

fix(vite): Skip SSR middleware in preview server for SPA mode
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- amsal
- Andarist
- andreasottosson-polestar
- andreiborza
- andreiduca
- antonmontrezor
- appden
Expand Down
70 changes: 70 additions & 0 deletions integration/vite-preview-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,74 @@ test.describe("Vite preview", () => {
"Product 123",
);
});

test("serves SPA mode app with vite preview", async ({
vitePreview,
page,
}) => {
const files: Files = async ({ port }) => ({
"react-router.config.ts": reactRouterConfig({
ssr: false,
v8_viteEnvironmentApi: true,
}),
"vite.config.ts": await viteConfig.basic({
port,
templateName: "vite-6-template",
}),
"app/root.tsx": tsx`
import { Links, Meta, Outlet, Scripts } from "react-router";

export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<div id="content">
<h1>SPA Mode</h1>
<Outlet />
</div>
<Scripts />
</body>
</html>
);
}
`,
"app/routes/_index.tsx": tsx`
export default function IndexRoute() {
return (
<div id="index">
<h2 data-title>Index</h2>
<p data-spa-mode>SPA Mode Enabled</p>
</div>
);
}
`,
"app/routes/about.tsx": tsx`
export default function AboutRoute() {
return (
<div id="about">
<h2 data-title>About</h2>
<p>About page in SPA mode</p>
</div>
);
}
`,
});

const { port } = await vitePreview(files, "vite-6-template");
await page.goto(`http://localhost:${port}/`, {
waitUntil: "networkidle",
});

// Ensure no errors on page load (this would fail without the fix)
expect(page.errors).toEqual([]);

await expect(page.locator("#index [data-title]")).toHaveText("Index");
await expect(page.locator("#index [data-spa-mode]")).toHaveText(
"SPA Mode Enabled",
);
});
});
5 changes: 5 additions & 0 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,11 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
},
configurePreviewServer(previewServer) {
return () => {
// Skip SSR handling in SPA mode
if (!ctx.reactRouterConfig.ssr) {
return;
}

// Handle SSR requests in preview mode using the built server bundle
previewServer.middlewares.use(async (req, res, next) => {
try {
Expand Down