You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "How React Server Component Integrates with Bundler"
3
3
description: "A deep dive into how React state updates really work, from hooks to batching"
4
4
date: "2026-02-24"
5
5
topic: "React"
@@ -8,64 +8,105 @@ draft: true
8
8
9
9
10
10
# Intro
11
-
I have been trying to build a react server component from a scratch for a learning and it's been a fun quite not gonna lie.
12
-
In this blog i wanna share with you all my learning, why RSC integrates with a bundler and how different is React server component
13
-
from the CSR app we build or the spa app we build
14
11
15
-
this blog is not going to be about why we need a RSC or benefite of RSC rather it will be on a internal implementation detail,
16
-
if you are nerdy about these things then buckle up we are going to see through those in this blog
12
+
I have been experimenting with building a [small version](https://github.com/sankalpaacharya/drop) of React Server Components to understand how the system works internally.
17
13
14
+
This blog is not about why RSC is useful or what benefits it brings. It’s more focused on how it actually works under the hood,
15
+
especially how the server and client parts of a React app end up split during build time.
18
16
19
17
20
-
# Little History
18
+
# Interesting parts
21
19
22
-
Before we move to a react server component let's have a quick recap, For many years react has always been on
23
-
the client side only, if you needed a data from the database you would have to wait till the browser downloads all the JavaScript,
24
-
parse it compile it and react renders all components commit them and after that the fetch call will begin. This way SEO suffers,
25
-
user will just have to stare at the blank screen while all these things is being processed
20
+
React server component has a protocl called a flight protocol which genereates a flight stream. This is not real JSON, but it looks similar in structure. It is closer to a structured wire format that React can parse incrementally while streaming.
26
21
27
-
considering a same problem react team during 2021 introduced a New Suspense SSR Architecture in React 18, RSC was still in experimental
28
-
phase that time, but i guess react team prepared the new suspense SSR Architecture considering the RSC they were working that time.
29
-
tho the initial RSC accountment was already done during the 2020 ie [Introducing Zero-Bundle-Size React Server Components](https://react.dev/blog/2020/12/21/data-fetching-with-react-server-components)
22
+
One reason this format is not plain JSON is that React needs to support values that JSON normally cannot represent cleanly during serialization, such as async boundaries and server-side computations that may resolve over time.
30
23
24
+
```tsx
31
25
32
-
# Interesting parts
26
+
const promise =newPromise((resolve) => {
27
+
setTimeout(() =>resolve("Sanku"), 1000);
28
+
});
33
29
34
-
react server component introduced a new protocl called a flight protocol which genereates a flight stream which looks something
30
+
const json =JSON.stringify(promise); // => '{}'
35
31
36
-
React server component needs a tight integreation with the bundler, In a RSC paradigm we can have a both client and a server components
32
+
```
37
33
38
-
RSC gives a ability to generate a UI on server side and send that built UI on the client side as a flight protocl and none
39
-
of the JavaScript for this component needs to be shipped to a browser, since RSC sends a serialized flight stream not a HTML, client
Instead of trying to force everything into JSON semantics, React uses a custom format that can represent these cases in a streaming way.
42
35
43
-
This can be pretty much confusing to a lot of people, is RSC is just a new SSR but that's actually not the truth cause
44
-
when you are doing SSR you are sending a rendered HTML to a client side and after that react on the client side takes a full control
45
-
you can't just ask few portion of your UI to get revalidated in SSR, you would need a full page re-fresh on that.
36
+
-[You can serialize a promise in React](https://twofoldframework.com/blog/you-can-serialize-a-promise-in-react)
46
37
47
-
so what actually mean in RSC is that a new version of a server subtree is streamed to the client and reconciled into the existing React tree
48
38
49
-
React server component paradigm we can have a client component and a server component both,
39
+
Take this as an example
50
40
51
-
like this
41
+
```tsx
42
+
// Server-Component.tsx
43
+
asyncfunction Page() {
44
+
const user =awaitdb.user.findFirst()
52
45
53
-
[why cleint component need ssrd](https://github.com/reactwg/server-components/discussions/4)
46
+
return (
47
+
<div>
48
+
<h1>Dashboard</h1>
49
+
<p>{user.name}</p>
50
+
<AddToCart />
51
+
</div>
52
+
)
53
+
}
54
+
```
54
55
55
-
# Let's begin
56
+
Now during a runtime RSC renderer will generate a flight stream for this which would look something like this
57
+
58
+
```json
59
+
1:["$","div",null,{
60
+
"children":[
61
+
["$","h1",null,{
62
+
"children":"Dashboard"
63
+
}],
64
+
65
+
["$","p",null,{
66
+
"children":"Sanku"// from DB result
67
+
}],
68
+
69
+
["$","$L1",null,{}]
70
+
]
71
+
}]
72
+
```
56
73
57
-
React UI is a tree in itself, this UI tree has leaves 🍃 in this tree, some leaves is for browser(client components) and some leaves
58
-
is for the server(server component).
74
+
On the client side, this stream is reconstructed back into a React tree. This is what allows React Server Components to send UI from the server without shipping the implementation code for server components to the browser.
75
+
Because you are sending a rendered UI as a flight stream which will be constructued on client side.
59
76
60
-
when we are building RSC app we build app from two prespective from a client world and from a server world.
77
+
while client components are resolved later using the referenced modules.
78
+
79
+
```json
80
+
1:["$","div",null,{
81
+
"children":[
82
+
["$","h1",null,{
83
+
"children":"Counter"
84
+
}],
85
+
86
+
["$","$L1",null,{}]
87
+
]
88
+
}]
89
+
```
90
+
91
+
92
+
This can be pretty much confusing to a lot of people, is RSC is just a new SSR but that's actually not the truth cause
93
+
when you are doing SSR you are sending a rendered HTML to a client side and after that react on the client side takes a full control
94
+
you can't just ask few portion of your UI to get revalidated in SSR, you would need a full page re-fresh on that.
95
+
96
+
# Let's begin
61
97
62
-
when it's a client build it's it goes from your entry file and your module graph, it travers your module graph and when it
63
-
encounters a servre component it will execute
98
+
A React UI is a tree. Some parts of this tree run on the server, and some parts run in the browser.
64
99
100
+
In a React Server Components setup, we are no longer dealing with a single build target. Instead, the same codebase is processed from two perspectives: a server world and a client world.
65
101
102
+
On the server side, the application is executed starting from the entry file. React walks through the module graph and renders all Server Components. These components run in a Node.js environment using the server renderer from react-server-dom, which does not include browser APIs like useState or event handlers.
66
103
104
+
Instead of rendering the component, the server emits something like a module reference using registerClientReference. This reference does not contain implementation details. It only points to where the component exists in the client bundle.
67
105
68
106
107
+
```tsx
108
+
let out =`import { registerClientReference } from "react-server-dom-webpack/server";\n`;
0 commit comments