Meet React Flight and Become a RSC Expert

Rate this content
Bookmark
SlidesGithub

Maybe you don't know what React Flight is, but you probably heard something about React Server Components instead.
Do you want to understand how RSCs work? My advice is to start from the new communication protocol behind it, whose name is, guess what, React Flight.

In this talk I will show, step by step, how this protocol enables some of the most interesting RSC features (SSG, SSR, streaming content, server and client components, etc.), at the same time revealing some unexpected surprises: for example, did you know that you can use RSC without a server?

This talk has been presented at React Day Berlin 2024, check out the latest edition of this React Conference.

FAQ

While RSC is primarily JavaScript-based, there are experimental libraries like rscx for Rust that attempt to implement RSC-like functionality in other languages.

Server components are stateless and not interactive, meaning they cannot use state-managing hooks or perform client-side interactions like alerts.

RSC allows asynchronous server components to stream content progressively, using features like suspense to delay rendering of certain parts until data is available.

ReactFlight is a protocol in React 19 used to serialize content so it can be sent from a server to a client, enhancing the sharing of rendering jobs between them.

Mauro explored React Server Components during a special Friday at Doubleloop by reading articles, blogs, and attending a workshop by Kent Seagal.

On Fridays, Doubleloop employees do not work for customers or perform usual tasks. Instead, they explore new topics and engage in activities different from their regular work.

React Server Components allow developers to balance rendering tasks between the server and client, enabling efficient server-side rendering and streaming of content.

Yes, client components can be included in server-rendered pages, but server components cannot be directly included in client components.

RSC offers a new way to perform SSR by using ReactFlight payloads instead of direct HTML, allowing for more efficient rendering and streaming of content.

As of now, the ReactFlight protocol is not well-documented, and its understanding is mostly based on reverse engineering and community examples.

Mauro Bartolomeoli
Mauro Bartolomeoli
31 min
13 Dec, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
My name is Mauro and I work for a company called Doubleloop in Italy. Today, I'm going to share my learning journey through puzzles and challenges related to React Server Components (RSC). RSC allows for server-side rendering and streaming of components. React flight, a protocol used to serialize and share rendering jobs, is a key feature of RSC. RSC can be used without a server, known as React Serverless Components. Using RSC, different types of content can be switched using the useState hook. RSC can be effectively used on a server by starting Node.js with a specific flag. Client-only components recognized by RSC can be executed on the client. React Streaming Components introduces the streaming concept and allows for continuous updates of the page. Advanced features of React Streaming Components include asynchronous rendering, suspense, and recursion. The React Strange Constraints challenge focuses on optimizing rendering by sending the initial page as HTML. The use of a proxy and RSE payload allows for client hydration of RSC. The possibility of using languages other than JavaScript, such as Rust, for server components is explored. RSC has the potential to become a language-agnostic protocol. The meaning of client-server roles in RSC can be extended. RSC offers streaming capabilities and the ability to balance server and client work. Infinite streaming is possible with server sockets. RSC hydration is believed to be faster than regular hydration. The Talk concludes by encouraging questions during the Q&A session.

1. Introduction to RSC

Short description:

My name is Mauro and I work for a company called Doubleloop in Italy. Today, I'm going to share my learning journey through puzzles and challenges related to React Server Components (RSC). I started by studying and reading articles, but I wanted to go deeper. So, I created puzzles to solve and that's how the idea for this talk was born. The title of the talk is 'RSC: Exploring React Server Components'.

My name is Mauro and I work for a company that is called Doubleloop in Italy. And I mentioned this company because it's been very important for me being here today. First of all because they are my main sponsor for being here doing this talk, but also because this company has some traditions. For example, we have special Fridays. On Friday we don't work for our customers, we don't do any usual work. I would say that we try to not work at all usually, but we do something different. For example, we explore some new topics.

And it was during one of those Fridays that I decided to explore RSC, React Server Components, because everybody was talking about them, but I didn't know anything, so I was very curious to learn something. How I did it? Well, I started by studying a little bit, so reading articles, blogs, I could find on the internet, and I finally found the workshop by Kent Seagal that I can suggest to you, that is about creating a framework based on React Server Components from scratch. I tried to follow it, it was very good, but it was not enough for me because to learn and understand really something I need to go deep, to write some code with my own hands. So I decided to do an additional step to create some puzzles that I had to solve one by one. And from those puzzles basically also the idea for the talk was born.

So today what I'm going to do is to share with you my learning journey through these puzzles or challenges, as I will call it during the talk. So this was the original title that I sent with the proposal, but during the preparation I thought of a different point of view, so I decided to associate the acronym RSC to many different meanings, each one for each different challenge that we are going to see, just to make it a little bit more fun. So this is the new final title. I remember also the enjoy flight, because we are going to talk about flight a little bit.

2. React Server Components Challenges

Short description:

The first challenge involves using React Server Components (RSC) without a server, known as React Serverless Components. We fetch pre-generated content using the createFromFetch function in React 19. The content is decoded using the ReactFlight protocol, a protocol used to serialize and share rendering jobs between a server and a client. The second challenge, called Replaceable Static Content, allows us to switch between different types of content using the useState hook. React-flight allows us to compose different chunks of content together, making it a rich protocol for sharing more than just simple HTML.

The first challenge is a simple hello world, we start easily. So we try to use the React Server Components technology but without using a server. So the challenge is called React Serverless Components, this is our first meaning for the acronym. So we want to create a very simple hello world, you can see it on the right, this is the final result. And on the left you can find some very simple code that you can run on a client, you don't need a server for this. And this code what it's basically doing is something that you can do with the RSC technology, that is loading some content that is pre-generated somewhere else, maybe on a server, I don't know, but for now we don't care about the server.

We can fetch this content from somewhere, then we can use a new function that is available in React 19, in the React Server Components package, obviously, that is called createFromFetch, that takes the content fetched and decodes in some way so that it can become something that we can render from React. There is also another thing that is new in React 19, a new feature in React 19, a new hook called use, maybe they finished the names for the hooks, so they went for the most simple one, it's called simply use. You may think you can use everything with use, but it's not the case, you can basically use any kind of resource, let's call them, in particular for now, it's used for using promises and context, so a simple way to get some remote resources, promises, and use it with a synchronous syntax instead of using async await and the usual stuff.

Now that we have the decoded stuff, we can render it using the usual render method from React DOM, there is nothing new in it, but maybe you are curious to understand what's this pre-generated content we are talking about. It's basically something like this. This is a new protocol that the React team introduced with RSE, it's called ReactFlight, this is the reason I told you remember the flight word, because it's important. It's a protocol that is used to serialize stuff so that you can send it from one place to another, for example from a server to a client, so you can generate the content in some place, send it somewhere else, and use it for example to share the rendering job between a server and a client. It's similar to JSON but it's not really JSON, we'll see a little bit more how it looks later. So with this we can imagine a workflow made like this one, for now we know how to deserialize this kind of content, using the createFromPatch function, we don't know yet how to serialize something on the server side, we'll see it later.

Okay, I can say that the first challenge is completed, so let's go quickly to the second one. It's not really that different, so I call it the first challenge and a half, because it's not really very different from the first one, so it was not good to call it second. The acronym in this case is translated as replaceable static content, because we want to add an additional step, what if we want to load more different content using RSC, not just a single page, we want to switch between different kind of contents. That's absolutely possible, the example it's the one you see on the right. Don't worry about the code, because there is a GitHub repository with all the snippets of code that I'm showing, I will give you the link later.

So with this example we don't use anything new from React 19, because the only thing we need to do is use the useState hook, for example, to store our content somewhere, like we are used to do. For example, we can store the result of the createFromPatch function that we have seen we have seen from the previous example, and just pass it down as a property to a non-sting or a root component, like in this case, and use it there. So our root component is in reality a very dynamic one, because it doesn't render anything from its own, it's just getting the content from somewhere else.

Something more about react-flight, because we are probably curious, we have already seen that in react-flight we can include some representation of our DOM in a sort of JSON-like syntax. It's not really JSON but it resembles it. But the other important thing is that content is not limited to just one row of stuff. We can have many pieces that are called chunks, each one has a prefix that is a key associated to this chunk, because an important property of react-flight is the ability to compose different chunks together, and the way you compose them is to reference other chunks with this $key syntax. So also react-flight is composable, like all the react libraries aim to be. So what can we think to do with all of these things that we have seen so far? Maybe static site generation, we can generate our react-flight payload somewhere and use it from a client without having a real server available. But the same is possible with the old HTML, so why do we need RLC to do that? Probably this is not a very strong selling point for RLC, but what I can tell you for now is that the react-flight protocol is a rich protocol, it can share between server and clients a lot of things more than simple HTML, so we will see probably later if this is something that we can use properly.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Simplifying Server Components
React Advanced 2023React Advanced 2023
27 min
Simplifying Server Components
Top Content
Watch video: Simplifying Server Components
React server components simplify server-side rendering and provide a mental model of components as pure functions. Using React as a library for server components allows for building a basic RSC server and connecting it to an SSR server. RSC responses are serialized virtual DOM that offload code from the client and handle interactivity. The client manifest maps serialized placeholders to real components on the client, enabling dynamic rendering. Server components combine the best of classic web development and progressive enhancement, offering the advantage of moving logic from the client to the server.
Exploring React Server Component Fundamentals
React Day Berlin 2023React Day Berlin 2023
21 min
Exploring React Server Component Fundamentals
Top Content
Watch video: Exploring React Server Component Fundamentals
This Talk introduces React Server Components (RSC) and explores their serialization process. It compares RSC to traditional server-side rendering (SSR) and explains how RSC handles promises and integrates client components. The Talk also discusses the RSC manifest and deserialization process. The speaker then introduces the Waku framework, which supports bundling, server, routing, and SSR. The future plans for Waku include integration with client state management libraries.
And Now You Understand React Server Components
React Summit 2024React Summit 2024
27 min
And Now You Understand React Server Components
Top Content
In this Talk, Kent C. Dodds introduces React Server Components (RSCs) and demonstrates how to build them from scratch. He explains the process of integrating RSCs with the UI, switching to RSC and streaming for improved performance, and the benefits of using RSCs with async components. Dodds also discusses enhancements with streaming and server context, client support and loaders, server component rendering and module resolution, handling UI updates and rendering, handling back buttons and caching, and concludes with further resources for diving deeper into the topic.
A Practical Guide for Migrating to Server Components
React Advanced 2023React Advanced 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
Watch video: A Practical Guide for Migrating to Server Components
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.
Server Components: The Epic Tale of Rendering UX
React Summit 2023React Summit 2023
26 min
Server Components: The Epic Tale of Rendering UX
Top Content
Watch video: Server Components: The Epic Tale of Rendering UX
This Talk introduces server components in React, which provide an intermediate format for rendering and offer advantages for both client-side and server-side rendering. Server components reduce bundle size on the client and improve search engine optimization. They abstract the rendering process, allowing for faster rendering and flexibility in choosing where to render components. While server components are still in the experimental stage, Next.js is a good starting point to try them out.
RSCs In Production: 1 Year Later
React Summit 2024React Summit 2024
24 min
RSCs In Production: 1 Year Later
This Talk explores the experience of shipping server components in production and highlights the benefits and challenges of using Server Components in Next.js apps. The Talk discusses the deployment of UploadThing and the use of AppRouter for safe production usage. It delves into the implementation of different layouts, data fetching, and code centralization for improved performance. The Talk also covers the use of server components for performance optimization and latency handling. Additionally, it explores the use of Edge and Lambda for partial pre-rendering and the challenges faced with webpack performance and hydration. Overall, the Talk emphasizes the benefits and challenges of working with Server Components in Next.js applications.

Workshops on related topic

Mastering React Server Components and Server Actions in React 19
React Summit US 2024React Summit US 2024
150 min
Mastering React Server Components and Server Actions in React 19
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Calling all React developers! Join us for an immersive 4-hour workshop diving deep into React Server Components and Server Actions. Discover how these game-changing technologies are revolutionizing web development and learn how to harness their full potential to build lightning-fast, efficient applications.

Explore the world of React Server Components, seamlessly blending server-side rendering with client-side interactivity for unmatched performance and user experience. Dive into React Server Actions to see how they combine client-side interactivity with server-side logic, making it easier to develop interactive applications without traditional API constraints.

Get hands-on experience with practical exercises, real-world examples, and expert guidance on implementing these technologies into your projects. Learn essential topics such as the differences between Server and Client Components, optimizing data fetching, passing data effectively, and maximizing performance with new React hooks like useActionState, useFormStatus and useOptimistic.

Whether you're new to React or a seasoned pro, this workshop will equip you with the knowledge and tools to elevate your web development skills. Stay ahead of the curve and master the cutting-edge technology of React 19. Don't miss out - sign up now and unleash the full power of React!
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
The Gateway to Backend: A Frontend Developer's Guide to Full-Stack Development
React Summit US 2023React Summit US 2023
160 min
The Gateway to Backend: A Frontend Developer's Guide to Full-Stack Development
Top Content
WorkshopFree
Amy Dutton
Amy Dutton
This workshop will guide you through the product development life cycle of creating a real-world web application. You will learn about React Server Components, building a design system within Storybook, and using frontend development to approach becoming a full-stack developer. The workshop will cover increasing confidence in your application with unit tests and implementing authentication and authorization. You'll have the opportunity to work through product features and examine a real-world RedwoodJS project, gaining valuable experience in real-world product development. RedwoodJS makes it simple to approach full-stack development, and this workshop will give you the skills you need to create your own real-world web applications.
Advanced Application Deployment Patterns with React Server Components (feat. a DIY RSC Framework)
React Summit US 2023React Summit US 2023
104 min
Advanced Application Deployment Patterns with React Server Components (feat. a DIY RSC Framework)
Top Content
WorkshopFree
 Greg Brimble
Greg Brimble
The developer ecosystem is always moving fast and this year has proved no exception. React Server Components can offer a significant improvement to developer experience and to application performance. But I think it's fair to say that this new server-first paradigm can be tricky to wrap your head around!In the first half of this workshop, we'll explore React Server Components from the ground-up: building our own mini meta-framework to help us understand how RSCs work. We'll discover exactly what is produced by an RSC build and we'll connect those pieces together to form a full application.Next, we'll deploy it! Cloudflare have also had a busy year too — Smart Placement, in particular, is a new technology that we've developed which fits the RSC model perfectly. We'll explore why that makes sense for our workshop app, and we'll actually deploy it onto the Cloudflare Developer Platform.Finally, we'll build out our app a little further, using D1 (our serverless SQL database) to really show off the React Server Component's power when combined with Smart Placement.You should come away from this workshop with a greater understanding of how React Server Components work (both behind-the-scenes and also how you as a developer can use them day-to-day), as well as insight into some of the new deployment patterns that are now possible after recent innovations in the platform space.
Building Reusable Server Components in NextJS
React Summit US 2023React Summit US 2023
88 min
Building Reusable Server Components in NextJS
Workshop
Will Bishop
Mettin Parzinski
2 authors
React continues to evolve their beta capability, React Server Components, and they're continuing to further develop them in partnership with frameworks like NextJS.In this workshop, attendees will learn what React Server Components are, how to effectively build and use them in NextJS, and focus on one of the major advantages of React/NextJS: reusability through components.We will also cover related beta technologies enabled by the `app` directory, such as nested layouts and server actions (alpha/experimental capability).Join us for this hands-on, 120 minute workshop!Technologies:
React, JavaScript/Typescript, NextJS, Miro
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
React Day Berlin 2023React Day Berlin 2023
149 min
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
Workshop
Maurice de Beijer
Maurice de Beijer
Get ready to supercharge your web development skills with React Server Components! In this immersive, 3-hour workshop, we'll unlock the full potential of this revolutionary technology and explore how it's transforming the way developers build lightning-fast, efficient web applications.
Join us as we delve into the exciting world of React Server Components, which seamlessly blend server-side rendering with client-side interactivity for unparalleled performance and user experience. You'll gain hands-on experience through practical exercises, real-world examples, and expert guidance on how to harness the power of Server Components in your own projects.
Throughout the workshop, we'll cover essential topics, including:- Understanding the differences between Server and Client Components- Implementing Server Components to optimize data fetching and reduce JavaScript bundle size- Integrating Server and Client Components for a seamless user experience- Strategies for effectively passing data between components and managing state- Tips and best practices for maximizing the performance benefits of React Server Components