Skip to content

Commit 666da4b

Browse files
committed
test(router): e2e for useLocationState inside a late-hydrating Suspense boundary (#992)
1 parent 2570e9c commit 666da4b

10 files changed

Lines changed: 214 additions & 0 deletions

File tree

‎packages/rangojs-router/e2e/location-state.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,3 +1443,42 @@ test.describe("location-state.action-ls (production)", () => {
14431443
test.setTimeout(120000);
14441444
actionLocationStateSuite(f);
14451445
});
1446+
1447+
const HYDRATION_ERROR =
1448+
/hydrat|did not match|server rendered|Minified React error #(418|419|422|423|425)/i;
1449+
1450+
function lateSuspenseSuite(f: ReturnType<typeof useFixture>) {
1451+
test("reader inside a late-hydrating Suspense boundary hydrates without a mismatch", async ({
1452+
page,
1453+
}) => {
1454+
const errors: string[] = [];
1455+
page.on("console", (msg) => {
1456+
if (msg.type() === "error") errors.push(msg.text());
1457+
});
1458+
page.on("pageerror", (err) => errors.push(err.message));
1459+
1460+
await page.goto(f.url("/location-state/late-suspense"));
1461+
await waitForHydration(page);
1462+
await page.locator('[data-testid="late-ls-write"]').click();
1463+
1464+
await page.reload();
1465+
await waitForHydration(page);
1466+
1467+
// The boundary resolves ~500ms after the root hydrated.
1468+
await expect(page.locator('[data-testid="late-ls-value"]')).toHaveText(
1469+
"stored-value",
1470+
{ timeout: 10000 },
1471+
);
1472+
expect(errors.filter((e) => HYDRATION_ERROR.test(e))).toEqual([]);
1473+
});
1474+
}
1475+
1476+
test.describe("location-state.late-suspense", () => {
1477+
const f = useFixture({ root: "./e2e/test-app", mode: "dev" });
1478+
lateSuspenseSuite(f);
1479+
});
1480+
1481+
test.describe("location-state.late-suspense (production)", () => {
1482+
const f = useFixture({ root: "./e2e/test-app", mode: "build" });
1483+
lateSuspenseSuite(f);
1484+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
3+
import { useLocationState } from "@rangojs/router/client";
4+
import { LateSuspenseState } from "../location-states.js";
5+
6+
export function LateSuspenseWriter() {
7+
return (
8+
<button
9+
type="button"
10+
data-testid="late-ls-write"
11+
onClick={() => LateSuspenseState.write({ label: "stored-value" })}
12+
>
13+
Write state
14+
</button>
15+
);
16+
}
17+
18+
/**
19+
* Rendered inside a <Suspense> whose server content resolves after the root
20+
* has hydrated. The hydrating render must be `undefined` (what SSR produced);
21+
* the stored value appears on the next render.
22+
*/
23+
export function LateSuspenseReader() {
24+
const state = useLocationState(LateSuspenseState);
25+
return (
26+
<div data-testid="late-ls-reader">
27+
{state ? (
28+
<span data-testid="late-ls-value">{state.label}</span>
29+
) : (
30+
<span data-testid="late-ls-empty">empty</span>
31+
)}
32+
</div>
33+
);
34+
}

‎packages/rangojs-router/e2e/test-app/src/location-states.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,11 @@ export interface TxWhenStateShape {
8181
}
8282

8383
export const TxWhenState = createLocationState<TxWhenStateShape>();
84+
85+
export interface LateSuspenseStateShape {
86+
label: string;
87+
}
88+
89+
// Persistent slot read by a useLocationState reader inside a Suspense boundary
90+
// that hydrates after the root (#992).
91+
export const LateSuspenseState = createLocationState<LateSuspenseStateShape>();

‎packages/rangojs-router/e2e/test-app/src/router.named-routes.gen.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export const NamedRoutes = {
259259
"locale.list": "/:locale/blog",
260260
"locationState.actionLs": "/location-state/action-ls",
261261
"locationState.index": "/location-state",
262+
"locationState.lateSuspense": "/location-state/late-suspense",
262263
"locationState.linkState": "/location-state/link-state",
263264
"locationState.linkStatePlainTarget": "/location-state/link-state/plain-target",
264265
"locationState.linkStateTarget": "/location-state/link-state/target",

‎packages/rangojs-router/e2e/test-app/src/urls/location-state.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import { Suspense } from "react";
12
import { urls, redirect } from "@rangojs/router";
3+
import {
4+
LateSuspenseReader,
5+
LateSuspenseWriter,
6+
} from "../components/LateSuspenseReader.js";
27
import { Link } from "@rangojs/router/client";
38
import {
49
FlashMessage,
@@ -34,6 +39,11 @@ import {
3439
* Location state test routes - tests for redirect() with state,
3540
* ctx.setLocationState(), useLocationState(), and useLocationState()
3641
*/
42+
async function LateSuspenseContent() {
43+
await new Promise((resolve) => setTimeout(resolve, 500));
44+
return <LateSuspenseReader />;
45+
}
46+
3747
export const locationStatePatterns = urls(({ path, middleware }) => [
3848
// Index page with links to trigger different scenarios
3949
path(
@@ -258,6 +268,22 @@ export const locationStatePatterns = urls(({ path, middleware }) => [
258268
{ name: "linkStatePlainTarget" },
259269
),
260270

271+
// #992: a persistent useLocationState reader inside a Suspense boundary that
272+
// resolves after the root hydrates.
273+
path(
274+
"/late-suspense",
275+
() => (
276+
<div data-testid="ls-late-suspense">
277+
<h1>Late Suspense</h1>
278+
<LateSuspenseWriter />
279+
<Suspense fallback={<div data-testid="late-ls-fallback">loading</div>}>
280+
<LateSuspenseContent />
281+
</Suspense>
282+
</div>
283+
),
284+
{ name: "lateSuspense" },
285+
),
286+
261287
// Static write/delete demo: drives LocationState.write() and .delete()
262288
// from the client and exposes both .read() and useLocationState() readers.
263289
path(

‎tests/cloudflare-basic/e2e/location-state.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,41 @@ function describeLocationState(mode: "dev" | "build") {
253253
});
254254
}
255255

256+
const HYDRATION_ERROR =
257+
/hydrat|did not match|server rendered|Minified React error #(418|419|422|423|425)/i;
258+
259+
function describeLateSuspense(mode: "dev" | "build") {
260+
const label = mode === "build" ? "production" : "dev";
261+
test.describe(`location-state.late-suspense (${label})`, () => {
262+
const f = useFixture({ root: ".", mode });
263+
264+
test("reader inside a late-hydrating Suspense boundary hydrates without a mismatch", async ({
265+
page,
266+
}) => {
267+
const errors: string[] = [];
268+
page.on("console", (msg) => {
269+
if (msg.type() === "error") errors.push(msg.text());
270+
});
271+
page.on("pageerror", (err) => errors.push(err.message));
272+
273+
await page.goto(f.url("/location-state-late-suspense"));
274+
await waitForHydration(page);
275+
await page.locator('[data-testid="late-ls-write"]').click();
276+
277+
await page.reload();
278+
await waitForHydration(page);
279+
280+
// The boundary resolves ~500ms after the root hydrated.
281+
await expect(page.locator('[data-testid="late-ls-value"]')).toHaveText(
282+
"stored-value",
283+
{ timeout: 10000 },
284+
);
285+
expect(errors.filter((e) => HYDRATION_ERROR.test(e))).toEqual([]);
286+
});
287+
});
288+
}
289+
256290
describeLocationState("dev");
257291
describeLocationState("build");
292+
describeLateSuspense("dev");
293+
describeLateSuspense("build");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
3+
import { useLocationState } from "@rangojs/router/client";
4+
import { LateSuspenseState } from "../location-states.js";
5+
6+
export function LateSuspenseWriter() {
7+
return (
8+
<button
9+
type="button"
10+
data-testid="late-ls-write"
11+
onClick={() => LateSuspenseState.write({ label: "stored-value" })}
12+
>
13+
Write state
14+
</button>
15+
);
16+
}
17+
18+
/**
19+
* Rendered inside a <Suspense> whose server content resolves after the root
20+
* has hydrated. The hydrating render must be `undefined` (what SSR produced);
21+
* the stored value appears on the next render.
22+
*/
23+
export function LateSuspenseReader() {
24+
const state = useLocationState(LateSuspenseState);
25+
return (
26+
<div data-testid="late-ls-reader">
27+
{state ? (
28+
<span data-testid="late-ls-value">{state.label}</span>
29+
) : (
30+
<span data-testid="late-ls-empty">empty</span>
31+
)}
32+
</div>
33+
);
34+
}

‎tests/cloudflare-basic/src/location-states.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ export interface PprExecMarkShape {
8383
* transition({ when }) reads it from `to.state`, a PPR replay HIT included.
8484
*/
8585
export const PprExecMark = createLocationState<PprExecMarkShape>();
86+
87+
export interface LateSuspenseStateShape {
88+
label: string;
89+
}
90+
91+
// Persistent slot read by a useLocationState reader inside a Suspense boundary
92+
// that hydrates after the root (#992).
93+
export const LateSuspenseState = createLocationState<LateSuspenseStateShape>();

‎tests/cloudflare-basic/src/router.named-routes.gen.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const NamedRoutes = {
7474
loaderCacheTag: "/loader-cache-tag",
7575
loaderCtxItem: "/loader-ctx/:id",
7676
localeInfo: "/:locale(en|fr)/info",
77+
locationStateLateSuspense: "/location-state-late-suspense",
7778
markerAfter: "/marker-after",
7879
markerBefore: "/marker-before",
7980
"mixedClient.detail": "/mixed-client-routes/:slug",

‎tests/cloudflare-basic/src/urls.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import {
1414
import { CFCacheStore, MemorySegmentCacheStore } from "@rangojs/router/cache";
1515
import { Suspense, type ReactNode } from "react";
1616
import { Link, Outlet } from "@rangojs/router/client";
17+
import {
18+
LateSuspenseReader,
19+
LateSuspenseWriter,
20+
} from "./components/LateSuspenseReader.js";
1721
import { StreamTest } from "./components/StreamTest.js";
1822
import { NavLayout } from "./components/NavLayout.js";
1923
import { RootLayout } from "./components/SlowRootLayout.js";
@@ -403,6 +407,11 @@ async function LoaderKeyCraftedPage(ctx: HandlerContext): Promise<ReactNode> {
403407
/**
404408
* Main URL patterns - Django-style routing API
405409
*/
410+
async function LateSuspenseContent() {
411+
await new Promise((resolve) => setTimeout(resolve, 500));
412+
return <LateSuspenseReader />;
413+
}
414+
406415
export const urlpatterns = urls(
407416
({
408417
path,
@@ -2150,6 +2159,24 @@ export const urlpatterns = urls(
21502159
() => [middleware(setOverlayCookie), loader(CookieOverlayLoader)],
21512160
),
21522161

2162+
// #992: persistent useLocationState reader inside a Suspense boundary
2163+
// that resolves after the root hydrates.
2164+
path(
2165+
"/location-state-late-suspense",
2166+
() => (
2167+
<div data-testid="ls-late-suspense">
2168+
<h1>Late Suspense</h1>
2169+
<LateSuspenseWriter />
2170+
<Suspense
2171+
fallback={<div data-testid="late-ls-fallback">loading</div>}
2172+
>
2173+
<LateSuspenseContent />
2174+
</Suspense>
2175+
</div>
2176+
),
2177+
{ name: "locationStateLateSuspense" },
2178+
),
2179+
21532180
// Action location state test route (non-redirect flow)
21542181
path("/action-location-state", ActionLocationStatePage, {
21552182
name: "actionLocationState",

0 commit comments

Comments
 (0)