Navigate back to the homepage
Browse Repo

What are server components and will you need to use them in the future?

Michiel Mulders
February 3rd, 2021 · 5 min read

Around Christmas, the React team introduced React Server Components. It’s a complete surprise for the React ecosystem as React has always focused on the client-side with a small focus on server-side rendering. Server-side rendering can, for instance, be accomplished using Next.js.

However, with React Server Components (RSC), it’s now possible to write server-rendered code using React only. Note that RSC is not a final feature as of now. Currently, React Server Components are undergoing beta-testing.

In this article, you’ll learn the following concepts about React Server Components:

  1. What is a React Server Component?
  2. Why do we need React Server Components? What problems do they solve?
  3. What does a React Server Component look like, and what are the benefits?
  4. What’s the difference between React Server Components and Server Side Rendering (SSR)?
  5. Is there any future for React Server Components?

What is a React Server Component?

Let’s make this clear first: React always renders React Server Components on the server. It allows frontend developers to create components that span both server and client.

Why is this useful? This property allows developers to quickly fetch data from external sources Therefore, you don’t have to send an expensive request from your client to your database (for example) to fetch data. As you render your components on the server that also hosts your backend, you can make a local call to your API to fetch data. In other words, locally fetched data is super fast (cheap).

To summarize, React Server Components allow you to access the speed of fetching data on the server-side while maintaining its rich interactivity of client-side apps. That’s a fantastic improvement for the React ecosystem to build faster applications.

Why do we need React Server Components?

UI development needs to offer the following properties:

  1. It should offer a good user experience
  2. It should be cheap to maintain
  3. It should offer fast performance

However, these properties are at odds with each other. To illustrate this problem with the current state of React, let’s imagine a Spotify artist page that has the following components.

1function ArtistPage({ artistID }) {
3 return (
4 <ArtistDetails artistId={artistId}>
5 <TopTracks artistId={artistId} />
6 <Discography artistId={artistId} />
7 </ArtistDetails>
8 );

Now, let’s fetch all data at the top of the component and pass each component’s required data. This approach’s benefit is that it’s performant as we only send one API call to retrieve all data we need.

We end up with the below code.

1function ArtistPage({ artistID }) {
2 const artistData = fetchAllArtistData();
4 return (
5 <ArtistDetails
6 details={artistData.details}
7 artistId={artistId}>
8 <TopTracks
9 topTracks={artistData.topTracks}
10 artistId={artistId} />
11 <Discography
12 discography={artistData.discography}
13 artistId={artistId} />
14 </ArtistDetails>
15 );

The API response becomes very coupled to the component. We expect the API call to return artist details, top tracks, and discography. If we want to change the component’s behavior in the future, we might need to change the behavior of the API endpoint as well.

Further, if we remove a component inside the <ArtistDetail/> component, we might forget to remove this data from the API call. Therefore, this approach affects code maintenance. But don’t worry, there’s nothing fundamentally wrong with this approach. It’s how React works.

What if we allow each component to fetch its data to decouple the API response from the component? Our code might look like this.

1function ArtistDetails({ artistId, children }) {
2 const artistData = fetchDetails(artistId)'
3 // ...
6function TopTracks({ artistId }) {
7 const topTracks = fetchTopTracks(artistId)'
8 // ...
11function Discography({ artistId }) {
12 const discography = fetchDiscography(artistId)'
13 // ...

While this code is much easier to maintain, it creates performance issues. If you remember our initial component, we’ll get a cascade of API calls that wait for each other to finish.

In other words, the <ArtistDetails/> component fetches data, then <TopTracks/> fetches its data, and finally, the <Discography/> component starts fetching the required data. They also call this a network waterfall.

Our previous example used a single API call to fetch all data to increase performance.

1function ArtistPage({ artistID }) {
3 return (
4 <ArtistDetails artistId={artistId}>
5 <TopTracks artistId={artistId} />
6 <Discography artistId={artistId} />
7 </ArtistDetails>
8 );

So, can we come up with a solution that addresses all three properties in the React ecosystem: good UX, cheap maintenance, and fast performance? The potential answer here is React Server Components (RSC) by moving our components to the server.

What does a React Server Component look like?

It’s time to explore a React Server Component. This component is an example out of the React demo for React Server Components. We have a list of notes that we can expand when we click it.

You can distinguish a server-side component from a client-side component by appending the file name with .server.js. The file below is named NoteList.server.js. You can apply the opposite for a client-side file: myFile.client.js.

1// NoteList.server.js
2import {fetch} from 'react-fetch';
4import SidebarNote from './SidebarNote';
6export default function NoteList({searchText}) {
7 const notes = fetch('http://localhost:4000/notes').json();
9 return notes.length > 0 ? (
10 <ul className="notes-list">
11 { => (
12 <li key={}>
13 <SidebarNote note={note} />
14 </li>
15 ))}
16 </ul>
17 ) : (
18 <div className="notes-empty">
19 {searchText
20 ? `Couldn't find any notes titled "${searchText}".`
21 : 'No notes created yet!'}{' '}
22 </div>
23 );

First of all, we can immediately fetch notes data from the server using http://localhost:4000/notes. This ticks off the fast performance property. On top of that, we can use interactive logic on our server, such as return notes.length > 0 ? <code> : <code>.

Next, let’s look at an example where we have an import that we only use once to format dates data-fns. Often, front-end developers have to evaluate if they want to include a dependency that they only use once or twice in the application. It will increase the final bundle size. Developers often ask the question: is it worth it?

1import {format, isToday} from 'date-fns';
3export default function SidebarNote({note}) {
4 const updatedAt = new Date(note.updated_at);
5 const lastUpdatedAt = isToday(updatedAt)
6 ? format(updatedAt, 'h:mm bb')
7 : format(updatedAt, 'M/d/yy');
9 return (
10 <div className="sidebar-note-list-item">
11 <header className="sidebar-note-header">
12 <strong>{note.title}</strong>
13 <small>{lastUpdatedAt}</small>
14 </header>
15 <button classname="sidebar-note-open" />
16 </div>
17 );

With React Server Components, we don’t have to worry about this evaluation. React won’t include a dependency that you use only in a React Server Component in the bundle size. Therefore, the user won’t have to download this dependency. The RSC will use this dependency on the server to render the required output and send this output to the client. Cool, right?

What’s the difference between React Server Components and Server Side Rendering (SSR)?

Server-side rendering focuses on reducing the initial page load. When using SSR, you send HTML to the client. The client then loads all of the React JavaScript you need to make your web page interactive. In other words, you have a traditional React app after the initial page load when React injects all JavaScript.

The most significant difference is that your components are still client-side components. All required dependencies are still downloaded. The only difference when using SSR is that your initial page load contains only HTML to improve performance.

Is there any future for React Server Components?

There’s a massive potential for React Server Components as they address many tradeoffs React developers experience. Some of the most important benefits include:

  • Fast data fetching locally on a server
  • Server-side dynamic logic
  • Ability to include any dependency for RSCs without having to worry about increasing the cost to load a web page

On the other hand, many React users might get confused at first by this domain switch. React has shown a strong focus on the client-side in the past. React Server Components suddenly span both server and client. This change requires a significant shift in the way we think about React components.

For that reason, I expect React developers to embrace this approach but also to see many people struggle to grasp this concept and make correct decisions about server/client-side components.

If you want to play around with the demo code for React Server Components, you can find this code on GitHub.

Monitor bad and slow requests in production

While RSC will let you work seemlessly with back-end and front-end requests, debugging data fetching requests and responses in production may be challenging and time consuming. OpenReplay is an open-source session replay stack for developers. It helps you replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder.

OpenReplay Frontend Monitoring

OpenReplay helps to quickly get to the root cause by reproducing issues as if they happened in your own browser. It also monitors your frontend performance by capturing key metrics such as time to interactive, page load time, memory consumption and slow network requests as well as Redux, VueX and NgRx actions/state. Start monitoring your web app for free.

More articles from OpenReplay Blog

All about React's createRef method

React refs are a useful and powerful tool that we can use to get even more control over the elements in our application.

February 1st, 2021 · 4 min read

Fetch vs Axios: Which Is the Best Library for Making HTTP Requests?

Axios and Fetch provide a seemingly equal API and set of features, so which one is better for making HTTP requests?

January 29th, 2021 · 4 min read
© 2021 OpenReplay Blog
Link to $ to $ to $