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

未关闭
#1,539 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
18/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
react, typescript
领域
full-stack

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
TypeScript
星标
1.2k
派生
287
平均合并
1 天 17 小时
30 天内合并 PR
19

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vitejs/vite-plugin-react 的其他 Issue

查看 vitejs/vite-plugin-react 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。