Repository navigation
Description
Affected version: [email protected]
Summary
lazy() caches its module promise permanently — including rejections. Once a chunk download fails, that failure is sealed in:
- Client: an error-boundary "retry" flow can never work.
Errored'sreset()remounts the boundary's children, but the lazy wrapper still holds the cached rejected promise, so every remount re-throws the original download error until a full page reload. Browsers deliberately do not cache failed dynamic imports (a laterimport()of the same specifier re-fetches), so the cached rejection also diverges from platform behavior. - Server:
pis module-scoped and shared across requests (intentionally, so_lazyAssetsreuse works). A single transient import failure — momentary FS/network hiccup, dev-server race — marks the shared promise errored and poisons every subsequent SSR request for the process lifetime. - Both: the internal
p.then(mod => { comp = () => mod.default })has no rejection handler, so every failed load additionally surfaces as an unhandled rejection.
Reproduction (client)
let fail = true;
const Page = lazy(() =>
fail ? Promise.reject(new Error("download failed")) : import("./Page")
);
<Errored fallback={(err, reset) => (
<button onClick={() => { fail = false; reset(); }}>Retry</button>
)}>
<Page />
</Errored>Clicking Retry re-throws download failed forever — fn is never called again even though the failure condition is gone. The same shape with a flaky network (chunk 503s once) leaves the route permanently broken without a hard reload.
Suggested fix
Clear the cached promise on rejection so the next preload/render retries, and route both the render path and preload through one load() so they dedupe. Client sketch (dist/solid.js shape):
function lazy(fn, moduleUrl) {
let comp;
let p;
const load = () => {
if (!p) {
const cur = p = fn();
cur.then(mod => {
comp = () => mod.default;
}, () => {
if (p === cur) p = undefined;
});
}
return p;
};
const wrap = props => {
if (sharedConfig.hydrating) comp = _lazyHydrationLookup(comp, moduleUrl);
let local = comp;
if (!local) {
const cur = load();
local = createMemo(() => cur.then(mod => mod.default));
}
// ... unchanged
};
wrap.preload = load;
wrap.moduleUrl = moduleUrl;
return wrap;
}Notes on the shape:
wrapcapturesconst cur = load()instead of reading closurep, so a rejection-triggered clear can't race a render in flight — the failing render still throws (through the memo's rejected promise) into the nearest boundary, and the next mount retries.- The added rejection handler also fixes the unhandled-rejection leak as a side effect.
- Server-side is the same pattern: capture
cur, setcur.error/cur.errored, clearp === curon rejection so the next request retries, and readcur(not closurep) in the wrap's memo/asset paths.
Context
Found porting TanStack Router's lazyRouteComponent to delegate to lazy() on the 2.0 rc line — its download-retry contract (error boundary → retry → re-import) is impossible against a cached rejection without wrapping lazy() in an external retry state machine. We're currently carrying this as a pnpm patch (with tests exercising the retry flow) in TanStack/router#8081; happy to turn it into a PR against the source if the approach looks right.
Possibly related: #2945 tracks a v1 symptom of the same missing rejection handling (renderToStream never closing when a lazy module promise rejects).
Activity
fix(solid): retry failed lazy() imports instead of caching the reject…
ryansolid commentedon Aug 17, 2026
Fixed on
nextin 3c68ab2 — thanks for the excellent write-up; the suggested shape was essentially right, with one adjustment.Both runtimes now route render and preload through a single
load()that clears the cached promise on rejection (only if still current), so a failed download retries on the next mount/preload()on the client and on the next request on the server, while success stays cached. The internal rejection handler also removes the unhandled-rejection leak.The one deviation from the sketch: on the client,
load()runs inside the render memo's compute rather than being captured withconst cur = load(). Solid 2.0'sErroredreset()doesn't remount the boundary's children — it recomputes the errored source computations in place — so a captured promise would replay the original rejection forever. With the cache consulted per compute,reset()triggers a fresh import directly. The in-flight-race concern the capture was guarding against doesn't apply here: the rejection only clears the cache, and any compute that already started holds its own promise reference.Covered by client tests (Errored
reset()retry +preload()retry/dedupe) and server tests (a transient import failure surfaces to the capturing request's boundary but doesn't poison subsequent renders).feat(solid-router): route chunks opt in to Solid's client-assets mani…
chore(solid): bump to solid-js 2.0.0-rc.1 and drop the solid-js patch
feat(solid-router): delegate lazyRouteComponent to Solid 2's lazy() (#…