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: 4 additions & 1 deletion src/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,18 @@ export type ClientGlobals = {
export interface AppProps {
children: React.ReactNode;
htmlProps?: React.HTMLAttributes<HTMLHtmlElement>;
/** Server scripts occupy a reserved slot so hydration keeps useId paths stable. */
hydrationScripts?: React.ReactNode;
}

export function App({ children, htmlProps }: AppProps) {
export function App({ children, htmlProps, hydrationScripts }: AppProps) {
return (
<html lang="en" suppressHydrationWarning {...htmlProps}>
<head>
</head>
<body>
{children}
{hydrationScripts}
</body>
</html>
);
Expand Down
12 changes: 8 additions & 4 deletions src/_server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -493,17 +493,21 @@ async function renderDocument(

renderStream = await renderToReadableStream(
<StrictMode>
<App htmlProps={htmlProps}>
<App
htmlProps={htmlProps}
hydrationScripts={
<Suspense fallback={null}>
<HydrationScript prepare={prepare} nonce={nonce} />
</Suspense>
}
>
<JuniperContextProvider context={requestContext}>
<StaticRouterProvider
router={router}
context={context}
hydrate={false}
/>
</JuniperContextProvider>
<Suspense fallback={null}>
<HydrationScript prepare={prepare} nonce={nonce} />
</Suspense>
</App>
</StrictMode>,
{
Expand Down
67 changes: 66 additions & 1 deletion src/client.hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { it } from "@std/testing/bdd";
import { stub } from "@std/testing/mock";
import { waitFor } from "@testing-library/react";
import globalJsdom from "global-jsdom";
import { act } from "react";
import { act, useId, useLayoutEffect } from "react";
import { renderToString } from "react-dom/server";
import { unstable_IdlePriority, unstable_scheduleCallback } from "scheduler";

import { Client } from "@udibo/juniper/client";
import { createServer } from "@udibo/juniper/server";
import type { RootRouteModule } from "@udibo/juniper";

import { App, registerRouter } from "./_client.tsx";
import { deserializeHydrationData } from "./_serialization.ts";
import { simulateBrowser } from "./utils/testing.internal.ts";

function reactWorkSettled(): Promise<void> {
Expand Down Expand Up @@ -195,3 +197,66 @@ for (const failure of ["caught", "uncaught", "hook"] as const) {
}),
);
}

it("preserves useId values when hydrating an actual server document", async () => {
let hydratedId: string | undefined;
function NameField() {
const id = useId();
useLayoutEffect(() => {
hydratedId = id;
}, [id]);
return (
<>
<label htmlFor={id}>Name</label>
<input id={id} defaultValue="Ada" />
</>
);
}
const client = new Client({ path: "/", main: { default: NameField } });
const server = createServer(import.meta.url, client, { path: "/" });
const response = await server.request("http://localhost:8000/");
assertEquals(response.status, 200);
const html = await response.text();
const embedded = html.match(
/__juniperHydrationData = (.*?); await client\.hydrate\(\)/,
);
assertExists(embedded);
const hydrationData = deserializeHydrationData(JSON.parse(embedded[1]));
await simulateBrowser(hydrationData, async () => {
const cleanup = globalJsdom(html, { url: "http://localhost:8000/" });
using errors = stub(console, "error", () => {});
let scheduled: IdleRequestCallback | undefined;
const previousIdle = globalThis.requestIdleCallback;
globalThis.requestIdleCallback = (callback) => {
scheduled = callback;
return 1;
};
try {
const input = document.querySelector("input")!;
const serverId = input.id;
assert(serverId.length > 0);
await client.hydrate();
assertExists(scheduled);
await act(() =>
scheduled!({ didTimeout: false, timeRemaining: () => 50 })
);
await waitFor(() => assertExists(hydratedId));
assertEquals(hydratedId, serverId);
assert(document.querySelector("input") === input);
assertEquals(document.querySelector("label")!.htmlFor, hydratedId);
assertEquals(
errors.calls.filter(({ args }) =>
/hydrat|server rendered/i.test(String(args[0]))
).length,
0,
);
} finally {
await reactWorkSettled();
if (previousIdle) globalThis.requestIdleCallback = previousIdle;
else Reflect.deleteProperty(globalThis, "requestIdleCallback");
registerRouter(undefined);
document.defaultView!.close();
cleanup();
}
})();
});
Loading