Zero-Bundle-Size React Server Components—An Overview
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (172).NET Core  (29).NET MAUI  (186)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (209)BoldSign  (12)DocIO  (24)Essential JS 2  (106)Essential Studio  (199)File Formats  (63)Flutter  (129)JavaScript  (218)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (871)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (49)Windows Forms  (61)WinUI  (68)WPF  (155)Xamarin  (160)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (145)Chart  (123)Cloud  (14)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (608)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (479)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (39)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (29)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (361)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (30)Visual Studio Code  (17)Web  (572)What's new  (311)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Zero-Bundle-Size React Server Components—An Overview

Zero-Bundle-Size React Server Components—An Overview

React is one of the most popular JavaScript front-end libraries for developing web applications. Lately, React has introduced a handful of breaking changes for the React ecosystem. Overall, it strives to provide better performance right out of the box for modern web applications. React Server Components are one such feature that has become a hot topic since they were introduced. It is an experimental feature, but still, you can play around with it to grasp where React is going.

In this article, I will discuss React Server Components, show you how to implement them, and consider their pros and cons.

Introduction to React Server Components

A React Server Component (RSC) is a basic component type that retrieves data from the server and renders its content on the server side. This implies that it will be able to fetch data for rapid rendering. There is no client-side interaction in these server components. Only the rendered content will be sent to the client for display purposes. So, developers can create apps that span the server and client.

In other words, React Server Components are a combination of the robust interactivity of client-side applications with the enhanced performance of conventional server rendering.

With a server-driven mental model, the React Server Components feature aims to enhance the user experience and the performance of React applications. Furthermore, client-side JavaScript bundles may be significantly smaller as Server Components will not be included in the client bundle.

Syncfusion React UI components are the developers’ choice to build user-friendly web applications. You deserve them too.

React Server Components vs. Server-Side Rendering

Conventional server-side rendering (SSR) cannot be replaced with zero-bundle-size React Server Components. SSR is an approach to rendering apps on the server side that involves delivering the HTML to the client so the browser will render it.

On the other hand, React Server Components work with SSR by using an intermediary structure to enable rendering without delivering any bundles to the client side. They are rendered on the server side in a unique format, not HTML, and streamed into the client. When combined with static site generators (SSG) like Next.js, Server Components enable scalability by adding more components, allowing the server tree and client-side tree to be combined without losing state.

SSR works on the basis of sending prebuilt HTML markup to the client, followed by hydration with JS. These pages can’t be rerendered while maintaining the client state. This prevents users from interacting with your website until the JS has loaded. In contrast, React Server Components can be called and rendered multiple times without losing the client state and have their own network data transfer protocol.

Server Components have access to back-end resources from anywhere in the component tree. When using SSR (e.g., Next.js), you have to use getServerProps() to reach the back end. However, this function can only be used on top-level pages and cannot be accomplished using random npm components.

How to Implement Server Components

Before React Server Components were introduced, there were only client components. But now, since there are two different types of components, you can differentiate between them by using server.js and client.js file suffixes (or else server.jsx and client.jsx). An absence of either identifies a component that can serve as both a server and client component.

A basic example of a React Server Component will look like the following.

import db from "database";

const Note= (props) => {
  const { id } = props;
  const note = db.notes.get(id);
  return (
    <div><h1>{note.title}</h1><p>{note.text}</p></div>
  );
};

As you can see, React Server Components have the same visual appearance as a conventional React component that accepts props and has a render method. But there are some unique features of React Server Components you need to know:

  • They directly access server data sources like databases, microservices, and functions. With this flexibility, we can build internal APIs that operate with different data sources and access the server from the component without exposing it explicitly to an API.
    import db from "database";
    
    const Note= (props) => {
      const { id } = props;
      const note = db.notes.get(id);
    
      return (
       <NoteContent note={note} />
      );
    
    };
    
  • React Server Components do not impact the client bundle’s size. Even more, if a third-party library is solely utilized by Server Components but not client components, it will not be added to the client bundle, since it is not required.

A to Z about Syncfusion’s versatile React components and their feature set.

Consider a client component named NoteContent that uses the third-party libraries marked and sanitize-html. The dependencies will have a direct impact on increasing the client bundle size.

// NoteContent.js
// Client Component

import marked from 'marked'; // 35.9K (11.2K gzipped)
import sanitizeHtml from 'sanitize-html'; // 206K (63.3K gzipped)

function NoteContent({content}) {
  const html = sanitizeHtml(marked(content));
  return (/* render */);
}

But the Server Components will have no impact on the bundle size.

// NoteContent.server.js 
// Server Component

import marked from 'marked'; // zero bundle size
import sanitizeHtml from 'sanitize-html'; // zero bundle size

function NoteContent({content}) {
  // same as before
}

While being rendered on the server side, a React Server Component behaves quite differently. The framework streams it as soon as the distilled UI state is ready. This way, the most important UI components can be easily recognized and rendered. Suspense can be utilized in the meantime to manage interactive client-side components effectively.

React Server Components can also render client components, native HTML elements, and server components. 

For example, if we have a client component named EditNoteButton, we can just import it into our server component and use it, as shown in the following.

import db from "database";
import EditNoteButton from 'EditNoteButton.client';

const Note= (props) => {
  const { id } = props;
  const note = db.notes.get(id);
  return (
    <div><h1>{note.title}</h1><section>{note.body}</section>
      <EditNoteButton/></div>
  );
};

On the other hand, client components cannot import server components. If we try to do so, it will import many dependencies into the browser bundles.

See the possibilities for yourself with live demos of Syncfusion React components.

Advantages and Disadvantages

React Server Components have many advantages, but there are some limitations, too.

Advantages

  • Separation of concerns between client and server logic.
  • Server-only code shrinks bundle sizes and has no impact on the client bundle.
  • Direct access to private and customized server-side data sources.
  • Seamless integration with client components.
  • Progressive hydration and streaming rendering.
  • Updates at the subtree and component levels preserve the client state. Thus, client state, focus, and even active animations are not affected or reset when a Server Component tree is refetched.
  • Server and client code sharing when it is appropriate.

Disadvantages

  • Client-only functionality, such as state, cannot be accessed by Server Components. As a result, React Hooks used for handling state data, such as useState and useReducer, are not supported.
  • React Hooks for rendering lifecycle methods like useEffect and useLayoutEffect cannot be used by React Server Components.
  • Browser-only APIs cannot be used.
  • Since the feature is still under development, there is no documentation. Instead, you must examine the source code of the demo application.

Github reference

Also, refer to the Zero bundle size in React Server Components demo on GitHub.

Explore the endless possibilities with Syncfusion’s outstanding React UI components.

Wrap Up

Thank you for reading! As developers, it is important to know the latest features introduced to deliver the best result. React Server Components give us a new way of combining the best of both client-side and server-side rendered components in React applications.

Despite being an experimental feature, it has a lot of potential and beautiful capabilities that will certainly enhance the way modern apps work with a faster and more enhanced user experience.

The Syncfusion Essential Studio for React suite offers over 70 high-performance, lightweight, modular, and responsive UI components in a single package. It’s the only suite you’ll ever need to construct a complete app.

If you have questions, contact us through our support forumsupport portal, or feedback portal. We are always happy to assist you!

Recommended resources

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top