Skip to content

Commit 623a5da

Browse files
test: characterize initial-load and back-navigation scroll restore timing (#655)
Reloads into pending route data, a lazy route component and an unresolved lazy subtree; across hydration, a streamed pending boundary and a still- loading route module; and a back navigation whose route has async data. Recorded against next. Co-authored-by: Cursor <[email protected]>
1 parent 4426516 commit 623a5da

3 files changed

Lines changed: 221 additions & 10 deletions

File tree

‎test/fixtures/hydration-lazy-route.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const manifest = {
1212
"src/routes/destination.tsx": { file: "routes/destination.js" }
1313
};
1414

15-
export function createApp() {
15+
export function createApp(options: { scrollRestoration?: boolean } = {}) {
1616
let releaseModules!: () => void;
1717
const modulesLoaded = isServer
1818
? Promise.resolve()
@@ -64,7 +64,7 @@ export function createApp() {
6464
component: route("src/routes/destination.tsx", "/routes/destination.js")
6565
}
6666
],
67-
scrollRestoration: false
67+
scrollRestoration: options.scrollRestoration ?? false
6868
});
6969

7070
function App() {

‎test/navigation-pending.spec.tsx‎

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
createRenderEffect,
2727
Errored,
2828
flush,
29+
lazy,
2930
Loading,
31+
NotReadyError,
3032
untrack,
3133
type ParentProps
3234
} from "solid-js";
@@ -64,6 +66,10 @@ type Options = {
6466
errored?: boolean;
6567
/** log the intent render and user effects over the location see when they run */
6668
landingProbe?: boolean;
69+
/** log what the page shows (a route, the loading fallback) at each `scrollTo` */
70+
scrollProbe?: boolean;
71+
/** wrap the router in `<Loading>`, as an app resolving a lazy subtree on arrival does */
72+
outerLoading?: boolean;
6773
};
6874

6975
function harness(initial = "/", options: Options = {}) {
@@ -95,7 +101,7 @@ function harness(initial = "/", options: Options = {}) {
95101
Object.defineProperty(window, "scrollY", { configurable: true, get: () => y });
96102
window.scrollTo = ((_x: number, newY: number) => {
97103
y = newY;
98-
log.push(`scrollTo:${newY}`);
104+
log.push(`scrollTo:${newY}` + (options.scrollProbe ? ` shows=${shows()}` : ""));
99105
window.dispatchEvent(new Event("scroll"));
100106
}) as any;
101107

@@ -229,24 +235,47 @@ function harness(initial = "/", options: Options = {}) {
229235
{ path: "/fresh/:id", component: Fresh },
230236
{ path: "/hop/:n", component: Hop },
231237
{ path: "/guarded", component: Guarded },
232-
{ path: "/watched", component: Watched }
238+
{ path: "/watched", component: Watched },
239+
{
240+
path: "/lazy",
241+
component: lazy(() => gate("lazy").promise.then(() => ({ default: page("lazy") })))
242+
},
243+
{
244+
path: "/tree",
245+
children: (() =>
246+
gate("tree").promise.then(() => ({
247+
default: [{ path: "/", component: page("tree") }]
248+
}))) as any
249+
}
233250
] as const,
234251
links: pendingLinks
235252
});
236253
const div = document.createElement("div");
254+
const shows = () =>
255+
div.querySelector("[data-route]")?.getAttribute("data-route") ??
256+
(div.querySelector("[data-loading]") ? "loading" : "-");
237257
document.body.appendChild(div);
258+
const app = () => <Router>{(props: ParentProps) => <Layout {...props} />}</Router>;
238259
const dispose = render(
239-
() => <Router>{(props: ParentProps) => <Layout {...props} />}</Router>,
260+
options.outerLoading ? () => <Loading fallback={<div data-loading />}>{app()}</Loading> : app,
240261
div
241262
);
242263

243264
const mark = (label: string) =>
244265
untrack(() => {
245-
const target = pendingTarget(router)?.value;
266+
// pending state over a still-resolving lazy route subtree is not
267+
// readable from outside the graph yet
268+
let state: string;
269+
try {
270+
const target = pendingTarget(router)?.value;
271+
state = `routing=${routingState(router)()}` + (target ? ` target=${target}` : "");
272+
} catch (e) {
273+
if (!(e instanceof NotReadyError)) throw e;
274+
state = "routing=<not ready>";
275+
}
246276
const intent = router.intent?.();
247277
log.push(
248-
`| ${label} routing=${routingState(router)()}` +
249-
(target ? ` target=${target}` : "") +
278+
`| ${label} ${state}` +
250279
(intent ? ` intent=${intent}` : "") +
251280
` at=${router.location.pathname}`
252281
);
@@ -701,6 +730,36 @@ describe("navigation pending state (characterization, #655)", () => {
701730
}
702731
});
703732

733+
test("a back navigation's restore joins the traversal's transition and waits for its data", async () => {
734+
const h = harness("/", { scrollProbe: true });
735+
try {
736+
await h.wait("mounted");
737+
await h.step("navigate /fresh/p", () => h.navigate("/fresh/p"));
738+
h.gate("p#1").resolve("p");
739+
await h.wait("landed /fresh/p");
740+
window.scrollTo(0, 900);
741+
await h.step("navigate /a", () => h.navigate("/a"));
742+
h.reset();
743+
await h.traverse("back to /fresh/p", () => window.history.back());
744+
await h.wait("still pending");
745+
h.gate("p#2").resolve("p");
746+
await h.wait("resolved");
747+
expect(h.log).toMatchInlineSnapshot(`
748+
[
749+
"fresh:p#2",
750+
"routing:true",
751+
"| back to /fresh/p routing=true intent=native at=/a",
752+
"| still pending routing=true intent=native at=/a",
753+
"routing:false",
754+
"scrollTo:900 shows=fresh",
755+
"| resolved routing=false at=/fresh/p",
756+
]
757+
`);
758+
} finally {
759+
h.cleanup();
760+
}
761+
});
762+
704763
test("beforeLeave blocking a programmatic navigation and a native pop", async () => {
705764
const h = harness();
706765
try {
@@ -814,6 +873,73 @@ describe("navigation pending state (characterization, #655)", () => {
814873
}
815874
});
816875

876+
// A reload lands on an existing entry: the restore targets a page whose
877+
// initial content is still arriving — route data, a lazy route component,
878+
// a lazy route subtree.
879+
async function reloadInto(initial: string, id: string) {
880+
const positions: Record<number, number> = {};
881+
for (let d = 0; d < 100; d++) positions[d] = 400;
882+
sessionStorage.setItem("solid-router:scroll", JSON.stringify(positions));
883+
const entries = performance.getEntriesByType;
884+
performance.getEntriesByType = ((type: string) =>
885+
type === "navigation" ? [{ type: "reload" }] : []) as any;
886+
let h!: ReturnType<typeof harness>;
887+
try {
888+
h = harness(initial, { scrollProbe: true, outerLoading: initial === "/tree" });
889+
h.mark("rendered");
890+
flush();
891+
h.mark("flushed");
892+
await h.wait("settled");
893+
h.gate(id).resolve(id);
894+
await h.wait("resolved");
895+
return h.log;
896+
} finally {
897+
performance.getEntriesByType = entries;
898+
h?.cleanup();
899+
}
900+
}
901+
902+
test("scroll restoration on a reload while the route's data is pending", async () => {
903+
expect(await reloadInto("/slow/initial", "initial")).toMatchInlineSnapshot(`
904+
[
905+
"routing:false",
906+
"preload:initial:initial",
907+
"fetch:initial:-",
908+
"scrollTo:400 shows=loading",
909+
"| rendered routing=false at=/slow/initial",
910+
"| flushed routing=false at=/slow/initial",
911+
"| settled routing=false at=/slow/initial",
912+
"| resolved routing=false at=/slow/initial",
913+
]
914+
`);
915+
});
916+
917+
test("scroll restoration on a reload while a lazy route component loads", async () => {
918+
expect(await reloadInto("/lazy", "lazy")).toMatchInlineSnapshot(`
919+
[
920+
"routing:false",
921+
"scrollTo:400 shows=loading",
922+
"| rendered routing=false at=/lazy",
923+
"| flushed routing=false at=/lazy",
924+
"| settled routing=false at=/lazy",
925+
"| resolved routing=false at=/lazy",
926+
]
927+
`);
928+
});
929+
930+
test("scroll restoration on a reload into an unresolved lazy route subtree", async () => {
931+
expect(await reloadInto("/tree", "tree")).toMatchInlineSnapshot(`
932+
[
933+
"| rendered routing=<not ready> at=/tree",
934+
"| flushed routing=<not ready> at=/tree",
935+
"| settled routing=<not ready> at=/tree",
936+
"routing:false",
937+
"scrollTo:400 shows=tree",
938+
"| resolved routing=false at=/tree",
939+
]
940+
`);
941+
});
942+
817943
test("a navigation whose data rejects lands on the error boundary", async () => {
818944
const h = harness("/", { errored: true });
819945
try {

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

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe("navigation during hydration", () => {
117117

118118
async function setup(
119119
fixture: Fixture = "hydration-navigation",
120-
options: { navigationType?: string } & Record<string, unknown> = {}
120+
options: { navigationType?: string; scrollLog?: string[] } & Record<string, unknown> = {}
121121
) {
122122
const stream = apps[fixture].start();
123123
await stream.shell;
@@ -141,14 +141,27 @@ describe("navigation during hydration", () => {
141141
event => streaming && event.stopImmediatePropagation(),
142142
true
143143
);
144+
if (options.scrollLog) {
145+
const log = options.scrollLog;
146+
const positions: Record<number, number> = {};
147+
for (let d = 0; d < 100; d++) positions[d] = 400;
148+
window.sessionStorage.setItem("solid-router:scroll", JSON.stringify(positions));
149+
Object.defineProperty(window, "scrollTo", {
150+
configurable: true,
151+
value: (_x: number, y: number) =>
152+
void log.push(
153+
`scrollTo:${y} shows=${window.document.querySelector("main")?.textContent}`
154+
)
155+
});
156+
}
144157
if (options.navigationType) {
145158
const entries = [{ type: options.navigationType }];
146159
window.performance.getEntriesByType = ((type: string) =>
147160
type === "navigation" ? entries : []) as any;
148161
}
149162
Object.assign(window, {
150163
hydrationOptions: options,
151-
scrollTo: () => {},
164+
...(options.scrollLog ? {} : { scrollTo: () => {} }),
152165
// Counts the requests the page makes itself: inside a hydration
153166
// tracking run core swaps `fetch` for a stub that never settles.
154167
fetch: (url: string) => (
@@ -309,6 +322,78 @@ describe("navigation during hydration", () => {
309322
}
310323
});
311324

325+
// When the reload's restore runs against content that is still arriving:
326+
// a boundary the server streams late, a route module the client loads.
327+
test("reload scroll restoration while the server streams a pending boundary", async () => {
328+
const log: string[] = [];
329+
const { dom, window, app, errors, finish } = await setup("hydration-navigation", {
330+
scrollRestoration: true,
331+
navigationType: "reload",
332+
scrollLog: log
333+
});
334+
const mark = (label: string) =>
335+
log.push(
336+
`| ${label} shows=${window.document.querySelector("main")?.textContent} hydrating=${app.isHydrationInProgress()}`
337+
);
338+
try {
339+
mark("shell");
340+
await new Promise<void>(resolve => setImmediate(resolve));
341+
mark("shell flushed");
342+
await finish();
343+
mark("streamed");
344+
expect(log).toMatchInlineSnapshot(`
345+
[
346+
"scrollTo:400 shows=Loading...Destination",
347+
"| shell shows=Loading...Destination hydrating=true",
348+
"| shell flushed shows=Loading...Destination hydrating=true",
349+
"| streamed shows=Home readyDestination hydrating=false",
350+
]
351+
`);
352+
expect(errors).toEqual([]);
353+
} finally {
354+
await finish();
355+
app.dispose();
356+
dom.window.close();
357+
}
358+
});
359+
360+
test("reload scroll restoration while the route module is still loading", async () => {
361+
const log: string[] = [];
362+
const { dom, window, app, errors, finish } = await setup("hydration-lazy-route", {
363+
scrollRestoration: true,
364+
navigationType: "reload",
365+
scrollLog: log
366+
});
367+
const mark = (label: string) =>
368+
log.push(
369+
`| ${label} shows=${window.document.querySelector("main")?.textContent} hydrating=${app.isHydrationInProgress()}`
370+
);
371+
try {
372+
mark("shell");
373+
await new Promise(resolve => setTimeout(resolve, 10));
374+
mark("modules pending");
375+
app.releaseModules();
376+
await new Promise(resolve => setTimeout(resolve, 10));
377+
mark("modules loaded");
378+
await finish();
379+
mark("streamed");
380+
expect(log).toMatchInlineSnapshot(`
381+
[
382+
"scrollTo:400 shows=Home readyDestination",
383+
"| shell shows=Home readyDestination hydrating=true",
384+
"| modules pending shows=Home readyDestination hydrating=true",
385+
"| modules loaded shows=Home readyDestination hydrating=false",
386+
"| streamed shows=Home readyDestination hydrating=false",
387+
]
388+
`);
389+
expect(errors).toEqual([]);
390+
} finally {
391+
await finish();
392+
app.dispose();
393+
dom.window.close();
394+
}
395+
});
396+
312397
// #625: the server showed the page, but the root boundary is still waiting
313398
// for its route module when the click lands.
314399
test("a link click before the root boundary's route module loads claims the server DOM", async () => {

0 commit comments

Comments
 (0)