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

Đang mở
#1,539 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
18/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react, typescript
Lĩnh vực
full-stack

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
1.2k
Fork
287
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
19

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của vitejs/vite-plugin-react

Tất cả issue của vitejs/vite-plugin-react

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.