Skip to content

[2.0] lazy() caches rejected module promises: retry is impossible on the client and one failed import poisons SSR for the process lifetime #2999

Closed

Description

@brenelz

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's reset() 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 later import() of the same specifier re-fetches), so the cached rejection also diverges from platform behavior.
  • Server: p is module-scoped and shared across requests (intentionally, so _lazyAssets reuse 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:

  • wrap captures const cur = load() instead of reading closure p, 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, set cur.error/cur.errored, clear p === cur on rejection so the next request retries, and read cur (not closure p) 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

  1. ryansolid commented on Aug 17, 2026

    @ryansolid
    Member

    Fixed on next in 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 with const cur = load(). Solid 2.0's Errored reset() 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions