Skip to content

Commit 4f14a34

Browse files
tsushanthryansolidcursoragent
authored
fix(server): handle rejection in lazy() so it reaches Errored (closes #2780) (#2781)
* fix(server): handle rejection in lazy() so it reaches Errored (closes #2780) `load()` registered a single `p.then(mod => p.v = mod.default)`. When the lazy import rejects, there's no second argument, so the rejection falls through as a process-level `unhandledRejection` and `p.v` stays `undefined` forever — the memo body keeps throwing `NotReadyError` and SSR treats the module as still loading instead of converting the rejection into a regular error that `<Errored>` can catch. Add a rejection branch that stores the error on `p.error`, then surface it from the memo via `if (p.error) throw p.error` ahead of the `!p.v` NotReadyError check. Mirror the change on the `ctx.block(p.then(...))` call site so it doesn't propagate its own unhandled rejection alongside the one captured for the render path. * test(server): cover rejected SSR lazy() reaching Errored + add changeset (#2780) Adds the regression test the PR was missing. The original attempt asserted the <Errored> fallback rendered into the server HTML, but in the streamed case (rejected lazy under a Loading boundary) the server only serializes the error at the boundary id and the client renders the fallback via the existing streamed-fragment hydration path. The test asserts the correct server-side behavior: the rejection is captured/serialized and the render completes instead of hanging on NotReadyError or leaking an unhandledRejection. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Ryan Carniato <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent c943c5c commit 4f14a34

3 files changed

Lines changed: 61 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Fix rejected SSR `lazy()` so it reaches `<Errored>` instead of stack-overflowing or leaking an unhandled rejection (#2780). `lazy()` hand-rolls its own promise tracking and previously had no rejection handler, so a failed module load left `p.v` undefined forever (the render memo kept throwing `NotReadyError`, i.e. perpetual "loading") and the orphaned rejection escaped as a process-level `unhandledRejection`. The loader now captures the rejection on the lazy and surfaces it through the render memo, and `ctx.block` swallows its duplicate rejection branch — bringing `lazy()` to parity with async memos, whose rejections already propagate to error boundaries. Once the error reaches the boundary, the existing streamed-fragment hydration path renders the fallback as usual.

‎packages/solid-web/test/server/ssr-stream.spec.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,37 @@ describe("SSR Streaming — Basic Rendering", () => {
266266
expect(html).not.toContain("tracking scope");
267267
});
268268

269+
test("rejected lazy() under Errored serializes the error instead of hanging (#2780)", async () => {
270+
const manifest = { "./Boom.tsx": { file: "assets/boom.js" } };
271+
const LazyBoom = lazy(
272+
() => new Promise<any>((_, rej) => setTimeout(() => rej(new Error("lazy failed")), 10)),
273+
"./Boom.tsx"
274+
) as any;
275+
276+
// Without the rejection capture in lazy(), the failed module load left the
277+
// render memo throwing NotReadyError forever (the stream never completes)
278+
// and leaked a process-level unhandledRejection. The render now completes,
279+
// and — because the boundary's region was already streamed (Loading
280+
// placeholder) — the error reaches `<Errored>` and is serialized at its id
281+
// for the client to render the fallback via the streamed-fragment path.
282+
const html = await renderComplete(
283+
() => (
284+
<Errored fallback={(e: any) => <span>err: {String(e()?.message || e())}</span>}>
285+
<Loading fallback={<span>Pending</span>}>
286+
<LazyBoom />
287+
</Loading>
288+
</Errored>
289+
),
290+
{ manifest }
291+
);
292+
const rKeys = [...html.matchAll(/_\$HY\.r\["([^"]+)"\]/g)].map(m => m[1]);
293+
// Error captured and serialized at the boundary id, the streamed fragment
294+
// settled (rejected), and the shell did not get stuck on the placeholder.
295+
expect(html).toContain("lazy failed");
296+
expect(rKeys).toContain("0");
297+
expect(rKeys).toContain("000_fr");
298+
});
299+
269300
test("async memo — shell contains fallback, final has resolved value", async () => {
270301
function App() {
271302
const data = createMemo(async () => {

‎packages/solid/src/server/component.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,23 @@ export function lazy<T extends Component<any>>(
8080
fn: () => Promise<{ default: T }>,
8181
moduleUrl?: string
8282
): T & { preload: () => Promise<{ default: T }>; moduleUrl?: string } {
83-
let p: Promise<{ default: T }> & { v?: T };
83+
let p: Promise<{ default: T }> & { v?: T; error?: unknown };
8484
let load = () => {
8585
if (!p) {
8686
p = fn() as any;
87-
p.then(mod => {
88-
p.v = mod.default;
89-
});
87+
p.then(
88+
mod => {
89+
p.v = mod.default;
90+
},
91+
err => {
92+
// Capture the rejection so the SSR render path can surface it to
93+
// `<Errored>` instead of leaving p.v `undefined` forever (which
94+
// would keep throwing `NotReadyError` and look like the module is
95+
// still loading) and instead of leaking the rejection as a
96+
// process-level `unhandledRejection` (#2780).
97+
p.error = err;
98+
}
99+
);
90100
}
91101
return p;
92102
};
@@ -121,13 +131,21 @@ export function lazy<T extends Component<any>>(
121131
}
122132
if (ctx?.async) {
123133
ctx.block(
124-
p.then(() => {
125-
(p as any).s = "success";
126-
})
134+
p.then(
135+
() => {
136+
(p as any).s = "success";
137+
},
138+
() => {
139+
// Rejection is captured on `p.error` by `load()` and surfaced
140+
// through the memo below; swallow the rejection of this branch
141+
// so `ctx.block` doesn't propagate a second unhandled rejection.
142+
}
143+
)
127144
);
128145
}
129146
return createMemo(
130147
() => {
148+
if (p.error) throw p.error;
131149
if (!p.v) throw new NotReadyError(p);
132150
return p.v(props);
133151
},

0 commit comments

Comments
 (0)