Skip to content

Commit f954d10

Browse files
ryansolidClaude via Cursorcursoragent
committed
test(hydration): cover a link click before the root boundary's route module loads (#625)
With file-route style lazy routes (a moduleUrl the server manifest answers for), the root <Loading> serializes a module map and its hydration waits for the client preload, while the router outside the boundary already takes clicks. Before #641 the router's location write forced sharedConfig.done, so the boundary resumed live against the new route: the fallback missed its hydration key and the server-rendered page stayed unclaimed. On #641 without #644 the destination rendered but the navigation back hung. The new fixture renders with a manifest and stands in for the browser's import() by seeding _$HY.loading behind a gate. The harness gains two generic hooks: an optional app.manifest for renderToStream and an optional app.preload(_$HY) before hydrate(). Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent d8288bd commit f954d10

2 files changed

Lines changed: 139 additions & 2 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { createMemo, lazy, Loading } from "solid-js";
2+
import { sharedConfig } from "solid-js/internal";
3+
import { isServer } from "@solidjs/web";
4+
import { createRouter, query, useIsRouting } from "../../src/index.js";
5+
6+
// Route components are lazy with a moduleUrl, as the Vite plugin's file routes
7+
// are. The server manifest answers for them, so the root <Loading> serializes
8+
// a module map and its hydration waits for the client to preload those
9+
// modules, while the router outside the boundary already takes clicks.
10+
const manifest = {
11+
"src/routes/home.tsx": { file: "routes/home.js" },
12+
"src/routes/destination.tsx": { file: "routes/destination.js" }
13+
};
14+
15+
export function createApp() {
16+
let releaseModules!: () => void;
17+
const modulesLoaded = isServer
18+
? Promise.resolve()
19+
: new Promise<void>(resolve => (releaseModules = resolve));
20+
let releaseDestination!: () => void;
21+
const destinationReady = new Promise<void>(resolve => (releaseDestination = resolve));
22+
const home = query(async () => "Home ready", "lazy-route-home");
23+
const destination = query(
24+
() =>
25+
isServer
26+
? Promise.resolve("Destination ready")
27+
: destinationReady.then(() => "Destination ready"),
28+
"lazy-route-destination"
29+
);
30+
let isRouting!: ReturnType<typeof useIsRouting>;
31+
32+
function Home() {
33+
const data = createMemo(() => home(), { deferStream: true });
34+
return (
35+
<main>
36+
<h1>{data()}</h1>
37+
<a href="/destination">Destination</a>
38+
</main>
39+
);
40+
}
41+
42+
function Destination() {
43+
const data = createMemo(() => destination(), { deferStream: true });
44+
return (
45+
<main>
46+
<h1>{data()}</h1>
47+
<a href="/">Home</a>
48+
</main>
49+
);
50+
}
51+
52+
const modules: Record<string, { default: () => any }> = {
53+
"/routes/home.js": { default: Home },
54+
"/routes/destination.js": { default: Destination }
55+
};
56+
const route = (moduleUrl: string, file: string) =>
57+
lazy(() => modulesLoaded.then(() => modules[file]), undefined, moduleUrl);
58+
59+
const Router = createRouter({
60+
routes: [
61+
{ path: "/", component: route("src/routes/home.tsx", "/routes/home.js") },
62+
{
63+
path: "/destination",
64+
component: route("src/routes/destination.tsx", "/routes/destination.js")
65+
}
66+
],
67+
scrollRestoration: false
68+
});
69+
70+
function App() {
71+
return (
72+
<Router>
73+
{props => {
74+
isRouting = useIsRouting();
75+
return <Loading fallback={<main>Loading...</main>}>{props.children}</Loading>;
76+
}}
77+
</Router>
78+
);
79+
}
80+
81+
// Stands in for the browser's import() of each serialized module map entry:
82+
// loadModuleAssets reuses an in-flight `_$HY.loading` entry.
83+
function preload(hy: any) {
84+
hy.modules ||= {};
85+
hy.loading ||= {};
86+
for (const id in hy.r) {
87+
if (!id.endsWith("_assets")) continue;
88+
for (const key in hy.r[id]) {
89+
const mod = modules[new URL(hy.r[id][key], "http://localhost/").pathname];
90+
hy.loading[key] = modulesLoaded.then(() => {
91+
hy.modules[key] = mod;
92+
});
93+
}
94+
}
95+
}
96+
97+
return {
98+
App,
99+
manifest,
100+
preload,
101+
release: () => {},
102+
releaseModules: () => releaseModules(),
103+
releaseDestination,
104+
isRouting: () => isRouting(),
105+
isHydrationInProgress: () => sharedConfig.isHydrationInProgress!()
106+
};
107+
}

‎test/server/hydration-navigation.spec.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { vi } from "vitest";
77

88
const { JSDOM, VirtualConsole } = createRequire(import.meta.url)("jsdom");
99
const root = fileURLToPath(new URL("../../", import.meta.url));
10-
const fixtures = ["hydration-navigation", "hydration-late-boundary"] as const;
10+
const fixtures = [
11+
"hydration-navigation",
12+
"hydration-late-boundary",
13+
"hydration-lazy-route"
14+
] as const;
1115
type Fixture = (typeof fixtures)[number];
1216
const serverEntry = (fixture: Fixture) => `${root}test/fixtures/${fixture}-server.tsx`;
1317
const clientEntry = (fixture: Fixture) => `${root}test/fixtures/${fixture}-client.tsx`;
@@ -23,6 +27,7 @@ for (const fixture of fixtures) {
2327
import { hydrate } from "@solidjs/web";
2428
import { createApp } from "./${fixture}";
2529
const app = createApp(globalThis.hydrationOptions);
30+
app.preload?.(globalThis._$HY);
2631
const dispose = hydrate(() => <app.App />, document.getElementById("app"));
2732
globalThis.hydrationTest = { ...app, dispose };
2833
`
@@ -41,7 +46,7 @@ for (const fixture of fixtures) {
4146
let resolveShell, resolveEnd;
4247
const shell = new Promise(resolve => resolveShell = resolve);
4348
const end = new Promise(resolve => resolveEnd = resolve);
44-
renderToStream(() => <app.App />).pipe({
49+
renderToStream(() => <app.App />, { manifest: app.manifest }).pipe({
4550
write(chunk) { chunks.push(String(chunk)); resolveShell(); },
4651
end() { resolveEnd(); }
4752
});
@@ -233,6 +238,31 @@ describe("navigation during hydration", () => {
233238
}
234239
});
235240

241+
// #625: the server showed the page, but the root boundary is still waiting
242+
// for its route module when the click lands.
243+
test("a link click before the root boundary's route module loads claims the server DOM", async () => {
244+
const { dom, window, app, errors, finish } = await setup("hydration-lazy-route");
245+
try {
246+
expect(Object.keys(window._$HY.loading)).not.toHaveLength(0);
247+
expect(window.document.querySelector("h1").textContent).toBe("Home ready");
248+
const click = new window.MouseEvent("click", { bubbles: true, cancelable: true, button: 0 });
249+
window.document.querySelector("a").dispatchEvent(click);
250+
expect(click.defaultPrevented).toBe(true);
251+
await new Promise(resolve => setTimeout(resolve, 10));
252+
app.releaseModules();
253+
await new Promise(resolve => setTimeout(resolve, 10));
254+
app.releaseDestination();
255+
await finish();
256+
await roundTrip(window, app);
257+
expect(window.document.querySelectorAll("main")).toHaveLength(1);
258+
expect(errors).toEqual([]);
259+
} finally {
260+
await finish();
261+
app.dispose();
262+
dom.window.close();
263+
}
264+
});
265+
236266
describe("a boundary resuming after the initial hydration pass", () => {
237267
async function visitOther(window: any, app: any) {
238268
window.document.querySelector("#other").click();

0 commit comments

Comments
 (0)