[plugin-rsc] No way to wait for, or preload, the client modules an RSC payload references during SSR

Open
#1,539 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
18/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
react, typescript
Domain
full-stack

Research direction

The SSR client-module import happens in packages/plugin-rsc/src/ssr/shared.ts (the load path, with setOnClientReference) behind the memoized __vite_rsc_client_require__ in src/core/ssr.ts. Reproduce with the linked starter and watch the first curl for the Suspense fallback. Done means a public hook that exposes each pending client-module import so a framework can await it before sending the shell, which is a design decision for maintainers first.

Written by the indexing model from the issue text.

Description

Claude Claude Opus 5.5 (high effort) wrote this; Claude Opus 5.5 (medium effort) reviewed it.

During SSR, plugin-rsc imports a client component's module the first time a payload references it, and React renders the closest <Suspense> fallback until that import resolves. So the first response after a server start differs from later ones, and a framework has no way to wait for, or preload, those imports.

How to reproduce

The repro is plugin-rsc 0.5.38's starter with <ClientCounter /> wrapped in <Suspense fallback={<p>loading...</p>}>, and ClientCounter importing its label from a new file, src/label.ts: https://github.com/nitedani-agent/plugin-rsc-repro-ssr-client-load

  1. pnpm i && pnpm dev
  2. curl -s http://localhost:5173 | grep -o '<p>loading...</p>' prints <p>loading...</p> on the first request after the server starts.
  3. Run it again, and it prints nothing, because the button is now in the shell. pnpm build && pnpm preview (port 4173) does the same after each start.
<!-- first response (trimmed) -->
<div class="card"><!--$?--><template id="B:0"></template><p>loading...</p><!--/$--></div>
...
<div hidden id="S:0"><button>Client Counter<!-- -->: <!-- -->0</button></div><script>$RB=[];...

<!-- second response -->
<div class="card"><!--$--><button>Client Counter<!-- -->: <!-- -->0</button><!--/$--></div>

Streaming the fallback and then the content is valid React, but plugin-rsc gives a framework no API to make the first response match the later ones. vike-react-rsc's <Head> content missed the <head> on a fresh dev server's first request for this reason (nitedani/vike-react-rsc#32). Awaiting allReady or dropping the <Suspense> would fix the shell but stop the page from streaming its server-side data, so what I'm after is waiting only for the module loads.

It happens on the first render of each client module after a start, under a <Suspense>, whenever that module's first import takes longer than the shell. With a second client component added to the repro and rendered only on /?b, the first /?b request shows its fallback too, after earlier requests to /. Frameworks on plugin-rsc show it too once the import is slow enough. Each ran from its official RSC starter with the same page, and the counts are server starts:

App Request 1 after a start Request 2
This repro, dev cold, dev warm, and preview fallback (2 of 2, 1 of 1, 2 of 2) component
This repro without the <Suspense> component, the shell waits for it component
Waku 1.0.0-rc.4, React Router 8.5.1 RSC, TanStack Start 1.169.4 RSC component, in every dev and prod start component
The same three, the client component importing a chain of 200 small local modules fallback in dev (each: cold 2 of 2, warm 1 of 1); in Waku's and TanStack Start's prod builds, the component (2 of 2) component
Waku, with a 300 ms wait added before each client module's first import (dev) fallback (cold 1 of 1, warm 1 of 1) component
Next.js 16.4.3, webpack and Turbopack dev, with the 200-module chain too component component
@vitejs/plugin-rsc 0.5.38, @vitejs/plugin-react 6.1.2, vite 8.3.5
react, react-dom, react-server-dom-webpack (vendored) 19.3.1
Node 26.12.1, Linux, pnpm 12.15.0
The three frameworks ran on plugin-rsc 0.5.38 and vite 8.3.5 (React Router's prod on 0.5.35, remix-run/react-router#15671)

plugin-rsc's SSR load imports the module on first use (ssr/shared.ts#L46-L76) behind a memoized __vite_rsc_client_require__ (core/ssr.ts#L13-L16), and React suspends on the pending import (requireAsyncModule). setOnClientReference reports a reference's assets, but never its pending import, so a framework still can't wait for it. Next.js wraps its client-module require and, in dev and in prerenders with cacheComponents, tracks each pending import (app-render.tsx#L2538-L2558). I think that's why even the 200-module chain doesn't show the fallback there.

I'd like an API that hands the framework each client-module import an SSR render starts (or preloads the modules a payload references), so it can wait for them before it sends the shell. vike-react-rsc wraps the internal __vite_rsc_client_require__ for this today (clientDeps.tsx).

#1476 is about the same SSR require cache (it never evicts), not about waiting on it.

Dominant language
TypeScript
Stars
1.2k
Forks
287
Avg merge
1d 17h
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vitejs/vite-plugin-react

All issues in vitejs/vite-plugin-react

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.