about how sever components rendered
Unanswered
𝓒𝓱𝓪𝓶𝓹𝓪𝓰𝓷𝓮~❤ posted this in #help-forum
I have a really confusing question for this "client component javascript instruction" that mentions in the official document.
"Next.js uses the RSC Payload and Client Component JavaScript instructions to render HTML on the server."
from what I've researched, RSC payload contain reference to client component's javascript bundle.
and javascript instruction as the doc said "The JavaScript instructions are used to "hydrate" Client Components"
and I also asked chatGPT, it says that client component javascript instructions is in the RSC payload.
my question is, do they serve different things for client component? or are they just the same thing since the instructions is in the RSC payload? thanks!
"Next.js uses the RSC Payload and Client Component JavaScript instructions to render HTML on the server."
from what I've researched, RSC payload contain reference to client component's javascript bundle.
and javascript instruction as the doc said "The JavaScript instructions are used to "hydrate" Client Components"
and I also asked chatGPT, it says that client component javascript instructions is in the RSC payload.
my question is, do they serve different things for client component? or are they just the same thing since the instructions is in the RSC payload? thanks!
11 Replies
Client Components (despite the name) render on the server first, and run again on the browser, I assume you already encountered this part in the docs.
I see JavaScript instructions as the React code that executes to build trees of components. Once the React tree is generated then it can be transformed into HTML.
So they need the JavaScript to run both on the Server and on the Client, (1) first to generate the HTML and to send as initial HTML (along with the HTML generated based on the RSC payload from Server Component), (2) and later on the browser, React runs again to Hydrate the client components, this means making React build re-run your component code to attach event handlers, refs and later run useEffects, and of course, reconcile the Virtual DOM.
I see JavaScript instructions as the React code that executes to build trees of components. Once the React tree is generated then it can be transformed into HTML.
So they need the JavaScript to run both on the Server and on the Client, (1) first to generate the HTML and to send as initial HTML (along with the HTML generated based on the RSC payload from Server Component), (2) and later on the browser, React runs again to Hydrate the client components, this means making React build re-run your component code to attach event handlers, refs and later run useEffects, and of course, reconcile the Virtual DOM.
About the title “how server components render”, they render on the server (obviously) and their output + other information is what’s called RSC payload.
To achieve this React executes the server components and serializes the result, and you can see the payload as Serialized Virtual DOM, since Server Components are executed before Client Components start rendering, they need to add references to where these Client components will be placed once the tree of components is completed, and this references are included in the RSC payload. In case your components suspend, that’s also included in the RSC payload so React on the client can reconcile the current UI with the one that will stream in
To achieve this React executes the server components and serializes the result, and you can see the payload as Serialized Virtual DOM, since Server Components are executed before Client Components start rendering, they need to add references to where these Client components will be placed once the tree of components is completed, and this references are included in the RSC payload. In case your components suspend, that’s also included in the RSC payload so React on the client can reconcile the current UI with the one that will stream in
@LuisLl About the title “how server components render”, they render on the server (obviously) and their output + other information is what’s called RSC payload.
To achieve this React executes the server components and serializes the result, and you can see the payload as *Serialized Virtual DOM*, since Server Components are executed before Client Components start rendering, they need to add *references* to where these Client components will be placed once the tree of components is completed, and this references are included in the RSC payload. In case your components suspend, that’s also included in the RSC payload so React on the client can reconcile the current UI with the one that will stream in
thanks for the reply, I know the concept, but as the doc say
"The JavaScript instructions are used to hydrate Client Components"
and RSC payload contain "Placeholders for where Client Components should be rendered and references to their JavaScript files"
chatGPT told me that js instruction is just the reference to the js bunlde for hydration and there is another js reference inside the RSC payload
so are they the same thing?
I could say react use RSC payload to hydrate client component?
what is actually happening in this part? when the client recieve the js reference, it has to fetch the js bundle from the sever before hydration?
sorry I'm preparing for an interview, and I kinda wanna know exactly what happen XD
"The JavaScript instructions are used to hydrate Client Components"
and RSC payload contain "Placeholders for where Client Components should be rendered and references to their JavaScript files"
chatGPT told me that js instruction is just the reference to the js bunlde for hydration and there is another js reference inside the RSC payload
so are they the same thing?
I could say react use RSC payload to hydrate client component?
what is actually happening in this part? when the client recieve the js reference, it has to fetch the js bundle from the sever before hydration?
sorry I'm preparing for an interview, and I kinda wanna know exactly what happen XD
I could say React use RSC payload to hydrate client component?
I don’t think so…, because RSC payload is more than just the little chunk of embedded JavaScript in it that references the bundle to hydrate the client component.
I believe the server sends the JavaScript bundle for the client components being requested on the page.
Lets say you ask for your-domain/dashboard/, the server sends back:
the Initial HTML containing both Server and Client components pre-rendered
+ the RSC payload from components on that page
+ the JavaScript bundle for the client components to hydrate
The fun part about pre-rendering client components on the server is that they know which client bundles need to be sent to the browser so you don’t have to make a request back to the server to get it and then start hydrating
Yes, the server sends the HTML ready and also the RSC payload, the HTML is the static template that’s taken by the browser to generate the DOM, which is the final form, and the RSC payload is used to later reconcile the Virtual DOM for when the UI has to change, since server components can’t re render on the client
I don’t think they’ll expect you to even know all this, but I’m like you so… I like knowing the details even though I forget about them after 2 weeks 🥲
@LuisLl Yes, the server sends the HTML ready and **also the RSC payload**, the HTML is the static template that’s taken by the browser to generate the DOM, which is the final form, and the RSC payload is used to later reconcile the Virtual DOM for when the UI has to change, since server components can’t re render on the client
hmm does sever component pre-render client component? I think they only put a placeholder for it for client side to handle the rendering?
Server Components are one thing, Client Components another.
Since both start at the server they both get pre-render into HTML that will be sent for the initial page load.
When you get the HTML for a page and it contains both Sever and Client components they all came already pre-rendered for the initial HTML
Since both start at the server they both get pre-render into HTML that will be sent for the initial page load.
When you get the HTML for a page and it contains both Sever and Client components they all came already pre-rendered for the initial HTML
Server Components output a react component tree that could easily be sent to the client for the client to pick up and do the rest, but since they’re already on the server Next.js goes ahead and pre-renders everything as HTML so you get a initial page instead of waiting for the client to build it
So when you get HTML from the server as the initial load you’re making use of the Framework SSR (server side rendering) abilities, that separate from both Server and Client components but it’s useful when you make use of it.
We have been able to SSR client components for years, that’s what Next.js 12 did all the time, there were no such things like Server Components and still you’d get initial HTML from the server
We have been able to SSR client components for years, that’s what Next.js 12 did all the time, there were no such things like Server Components and still you’d get initial HTML from the server
@𝓒𝓱𝓪𝓶𝓹𝓪𝓰𝓷𝓮~❤ hmm does sever component pre-render client component? I think they only put a placeholder for it for client side to handle the rendering?
Do something, wrap a client component in “dynamic” from Next.js and set SSR to false in the config object you pass, you’ll notice how when you send the initial HTML that client component is skipped, not pre-render on the server, and then it’s filled when it reached the browser. So yea, the placeholder tells React where to place the components when the tree is reconciling on the browser