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

Fix a bug with SPA mode when the root route had no children #8747

Merged
merged 3 commits into from
Feb 14, 2024
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/proud-paws-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Fix a bug with SPA mode when the root route had no children
84 changes: 84 additions & 0 deletions integration/spa-mode-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,90 @@ test.describe("SPA Mode", () => {
"Index Loader Data"
);
});

test("works for migration apps with only a root route and no loader", async ({
page,
}) => {
fixture = await createFixture({
compiler: "vite",
spaMode: true,
files: {
"vite.config.ts": js`
import { defineConfig } from "vite";
import { vitePlugin as remix } from "@remix-run/dev";

export default defineConfig({
plugins: [remix({
// We don't want to pick up the app/routes/_index.tsx file from
// the template and instead want to use only the src/root.tsx
// file below
appDirectory: "src",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, that's a neat trick.

Copy link
Contributor Author

@brophdawg11 brophdawg11 Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol - it took me far too long to figure out why my test wasn't failing and I finally dug deep enough to find out that we copy a template first, so even though I wasn't defining an index route, my test ended up with one. And I started down the road of a flag to delete files/not copy the template before I realized I could do this instead

ssr: false,
})],
});
`,
"src/root.tsx": js`
import {
Meta,
Links,
Outlet,
Routes,
Route,
Scripts,
ScrollRestoration,
} from "@remix-run/react";

export function Layout({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}

export default function Root() {
return (
<>
<h1 data-root>Root</h1>
<Routes>
<Route path="/" element={<h2 data-index>Index</h2>} />
</Routes>
</>
);
}

export function HydrateFallback() {
return <h1 data-loading>Loading SPA...</h1>;
}
`,
},
});
appFixture = await createAppFixture(fixture);

let res = await fixture.requestDocument("/");
let html = await res.text();
expect(html).toMatch('<h1 data-loading="true">Loading SPA...</h1>');

let logs: string[] = [];
page.on("console", (msg) => logs.push(msg.text()));

let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await page.waitForSelector("[data-root]");
expect(await page.locator("[data-root]").textContent()).toBe("Root");
expect(await page.locator("[data-index]").textContent()).toBe("Index");

// Hydrates without issues
expect(logs).toEqual([]);
});
});

test.describe("normal apps", () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/remix-react/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ export function createServerRoutes(
index: route.index,
path: route.path,
handle: routeModule.handle,
// For SPA Mode, all routes are lazy except root. We don't need a full
// implementation here though - just need a `lazy` prop to tell the RR
// rendering where to stop
lazy:
isSpaMode && route.id !== "root" ? () => spaModeLazyPromise : undefined,
// For SPA Mode, all routes are lazy except root. However we tell the
// router root is also lazy here too since we don't need a full
// implementation - we just need a `lazy` prop to tell the RR rendering
// where to stop which is always at the root route in SPA mode
lazy: isSpaMode ? () => spaModeLazyPromise : undefined,
// For partial hydration rendering, we need to indicate when the route
// has a loader/clientLoader, but it won't ever be called during the static
// render, so just give it a no-op function so we can render down to the
Expand Down