user
React Server Components

Hamza Miloud Amar
July 2, 2024
7 min read

In our previous article, we discussed Island Architecture. Today, we shift our focus to React Server Components. This paradigm allows developers to render specific components exclusively on the server side, optimizing overall application performance and simplifying data fetching.
Web Development Group
Me , Server , Browser, CDN, Narrator
narrator
user
server
As a server, my primary function is to generate the initial HTML structure of a web page. However, this process involves two distinct component types: server components and client components
server
Server components: I render these entirely on the server, generating their HTML output. No JavaScript code is sent to the client, eliminating the need for hydration.
server
Client components: on the other hand, are rendered by me but also include JavaScript code that is sent to the browser (for application state, event handlers, and access to DOM nodes). This allows the browser to hydrate these components, enabling interactive features and dynamic updates within the web page.
browser
user
server
Okay, let's simplify it: Server Components run exclusively on the server. Let me give you an example to illustrate this...
server
Let's take a component that displays the date in the format (YYYY.MM.DD). With traditional client components, we'd send the raw date and JavaScript code to the browser to hydrate it. Server components, however, allow us to format the date directly on the server and send only the formatted result in HTML, simplifying the process and reducing client-side load.
user
server
server
Improved Performance: Fetching and caching data within server components can significantly improve performance by reducing client-side load and optimizing response times.
server
SEO-Friendly: Server-rendered HTML is more easily indexed by search engines, improving SEO.
server
Improved User Experience: Since server components do not hydrate on the client, the client's bundle size is reduced, improving the application's Time To Interact (TTI).
server
Simplified Codebase: Server Components can handle data fetching directly, simplifying the data handling logic and reducing the need for complex client-side state management.
server
Improved Security: Server components enhance security by keeping sensitive data out of reach from the client.
user
server
New paradigm: Introducing a new approach that requires a learning curve.
server
Limited Adoption: Not all packages are currently compatible with Server Components. This new paradigm requires further integration across the ecosystem.
user
server
In general, you should aim to use Server Components whenever possible. They reduce the JavaScript bundle size, improve performance, and simplify data fetching. However, there are specific scenarios where Client Components become necessary:
server
Managing State: If your component needs to maintain and update its own internal state, a Client Component is required.
server
Leveraging Effects: To perform side effects like subscribing to external events or running code when a component mounts or unmounts, you'll need a Client Component. Server Components do not have a lifecycle for managing these types of operations.
server
Interacting with Browser APIs: When your component needs to interact directly with browser APIs (e.g., accessing geolocation, or working with local storage), a Client Component is necessary. These APIs aren't available on the server.
server
manipulating the DOM: If your component needs to directly interact with the DOM (e.g., for focusing an input, measuring dimensions, applying animations, or handling click events), you'll need to use a Client Component. Server Components don't have access to the browser's DOM.
user
server
Absolutely! While server components offer many benefits, there are some potential performance considerations to keep in mind.
server
server
server
Furthermore, increased server load due to rendering components can also impact performance, especially under heavy traffic.
user
server
server
To enhance site size performance, you can implement techniques outlined in this guide: Static Site Enhancement.
server
For the issue of slow loading page sections due to database calls or other processes, we can leverage Streaming to ensure that the rest of your page content is displayed promptly without being held back by slower components.
server
Leveraging caching mechanisms on both the server and client-side can significantly reduce server load and improve overall performance.
user
server
browser
user
me
Thank you for joining us in exploring React Server Components. I hope you enjoy this series Of article and find it informative. If you have any questions or suggestions, please feel free to reach out to me on Twitter or LinkedIn. Thank you for reading, and I look forward to seeing you in the next article!