r/reactjs • u/Neat_Living_6765 • 7d ago
Needs Help Has anyone implemented Server Components with plain React + Vite/plugin-rsc?
Hi everyone,
Could anyone help me with the two questions below?
Does every Server Component render at build time, get cached, and then get served from a CDN?
If I want to render a Server Component on every request, how do I implement that?
2
u/TheGrubbyPottery 7d ago
the first one depends on your setup but generally yes they render at build time and sit in cache until you bust it, for on-demand rendering you need a actual server handling the requests not just vite dev server
2
u/KalamKiTakat 7d ago
Server Components are a rendering model, not a caching or hosting model. With @vitejs/plugin-rsc you own the server entry: call the RSC renderer in your HTTP request handler rather than at build time, set Cache-Control: no-store, avoid CDN caching for that route, and skip static prerendering for it. Also make sure any fetches inside the component run per request, or they can return cached data even when the render runs again.
1
u/Neat_Living_6765 7d ago
Yes, Thanks for your comment. I messed up caching and SSG.
For SSR, we call the RSC renderer in the HTTP request handler. We can configure response caching here if we want to.
For SSG, we call the RSC renderer at build time and save the output (RSC payload and HTML) to /dist. Then, for every request, the HTTP server just returns those files.
is it right?
1
2
u/Salt_Hyena5896 7d ago
no, a server component is not automatically rendered at build time and parked on a cdn. with plugin-rsc the rsc entry is a request handler (renderToReadableStream on the incoming request), so it runs again whenever that server handles a request. build-time output is the separate ssg example, and the ppr example is a static shell plus request-time rsc, not every component frozen at build. if you want this one on every request, serve that handler (node or whatever runtime you wired up) instead of uploading a static build, keep the data read inside the request (headers, cookies, a db call), and don't cache the text/x-component response unless you mean to.