So diving right in, React Server Components, probably the biggest talk of 2023 after AI. If you've been on Twitter or X, you've probably been seeing people talking about it every single day. Some people love them. Some people hate them. And most people are just confused. We have questions like, what is RSC, as in React Server Components? Isn't it the same as SSR? Why is React turning back to PHP? How does this all fit with Next.js? And can I use it without Next.js? Right now, that's a bit of a mess. I'll do my best to untangle that mess for you.
So before that, a little self-introduction. I'm Adrian, the founder of JavaScript Mastery. We've posted a lot of different educational YouTube videos on topics such as JavaScript, React, and over the last year, a lot of Next.js. And also, I've been recognized as a GitHub star, kind of position offered by GitHub for people that really try to give back to the audience and to people focusing on education. So I give my best to create over hundreds of project-based videos where we teach developers how to build real applications start to finish. And I've been working with the new Next.js a lot, ever since it came out, almost using it in all of our YouTube videos. We had it a lot, building real applications, teaching it in our Next.js course as well. And it's completely new Next.js. All from 13 to 14 and even further, there's a lot of new stuff happening with Next.js. We also created the Next.js course right here that almost 3,000 people have joined. And in this workshop, I'll explain what I've learned about React server components and how they connect with Next.js in a way that's easy to understand.
So let's begin with a warm-up on pre-React server component era. Things were simple and straightforward. React was guaranteed to run on the client's browser, nothing else. Simply ship big chunks of JavaScript bundles with minimal single HTML file and tell the client's browser to handle everything else. The process, as we know it, the client makes a request to the server, which looks something like this. The server responds to HTML and JavaScript bundle. The client renders HTML and downloads everything mentioned in the JavaScript bundle. The client executes that JavaScript and then paints the content on the screen. This was surprisingly amazing web invention. It still is, but like all things, it still has drawbacks.
Yes, even React has drawbacks, the most important ones being performance. The more JavaScript you ship, the longer it will take for your site to load initially. And in today's world, the first impression is crucial to keep your users engaged on our websites. We also must not forget about the low-end devices, where we presume that all users have top-notch devices, networks, and browsers and trusting that their browser could handle it. Yet, not everyone owns the latest devices, and we can't overlook the fact that over time, we're not incorporating increasingly advanced features into websites. If you've experimented with Next.js during development, you might have observed that it doesn't perform optimally on MacBooks with Intel chips. They're powerful machines, but new technology is demanding. As new technology evolves, so do challenges for users on less advanced setups that were powerful some day. And of course, we can't talk about React's drawbacks without mentioning SEO. Yes, yes, you heard that multiple times, React doesn't play well with SEO. It would have been amazing if web crawlers understood JavaScript, but unfortunately, that's not the case. And with minimal HTML from React, our website doesn't get to properly understand SEO. So, to address this problem, Next.js implemented SSR. SSR, server-side rendering, this method involves rendering React on a server, generating HTML, and then sending it to the client. The client is then responsible for rendering those HTML nodes and elements and incorporating the necessary JavaScript to enable interactive content. It's important to understand that as of now, SSR can only render React components that are suitable for rendering on the server. Components requiring interactivity, such as those with event listeners or dependencies on web APIs, aren't executed by SSR. You can think of SSR as a method to create a blueprint of our website. It generates and displays how your webpage will initially appear.
For example, consider the following scenario on the page. We have a home.js file, or we have a typical counter that you've seen hundreds of times. I believe it was also featured in React.js docs as one of the first examples of using states when hooks came out. How many of you were here for React 15, React 16, when we transitioned from class-based components to just hooks? Feel free to let me know in the chat. Not a lot of new generations of developers that are coding right now have been through that time. Have you been here for that transition? Yeah, it's crazy to think that some of us have started with class components and some of us, or some of you maybe, have never used class-based components, which is a pretty interesting thing to think about. We have this example right here, and the result of the SSR output would be like this, where we simply have welcome to my new Next.js app. Where we have the button and the counter. That's the SSR. An HTML page is produced here, comprising a list of elements and nodes, representing the components of that page. But what about interactivity? All the states and event listeners are dispatched to the client and as JavaScript bundles, which the client browser loads and initiates the construction of the virtual DOM. The client compares the static DOM we received initially with the virtual DOM it will create using the provided JavaScript code, and then determines if they properly match. If not, it will throw an error, saying that many Next.js developers are frustrated about, which is going to look something like this. A hydration error. This entire process of reconciling the virtual DOM, which the client believes represents the page, with the actual static DOM that the server delivered, is called hydration. Everyone's favorite, Dan Abramov, put it in this way, through his mental models. Before, so without the SSR, we have the React tree, where we render components to HTML. We have the main.js and then we bundle the code for interactions like this. And then after SSR, it's going to look something like this. Where we have the server tree, with the file system, databases, internal services, and a lot of other stuff. And we pass props to the React tree, like this. Basically, a virtual DOM, or tree, created by the client, which looks like this. Server, client, and now we have everything happening on the client as well. Yeah, I can see in the messages as well that Davie sent the builder.io React hydration overlay. So, that's a pretty cool thing to note. We can also definitely share it in the Discord, so whenever we have anything, we have a forum and we can keep our discussion there. It's quite useful to know that we can have better and improved error messages when it comes to hydration. Thank you for sharing that. But that's enough about SSR for now. You get it. Since we have this solid SSR approach, why should we bother with React server components? Well, the conventional hydration we discussed earlier, before React server components and Next 13, involved a full page hydration. This meant that when the page loaded, the entire React component 3, including all components, whether initially visible or interacted with by the user, were hydrated on the client side. What does this mean for performance? Although you can see something on the page, it won't be interactive until the client loads everything related to that page, specifically the necessary JavaScript bundle to hydrate the static tree. Sure, we achieve a faster initial load at this stage, but then there's a delay we can interact with, and because we're waiting for the JavaScript to hydrate, there's a gap. For more extensive codebases, it can lead to larger initial page loads and processing of JavaScript, even for components that may not be initially required. So here's where the React server components step in. React server component is a fresh component type. It's assured to run exclusively on the server. They never get hydrated on the client side. So as a result, no extra JavaScript is sent, resulting in a smaller JavaScript bundle size, faster page loading, selective hydration, and an enhanced experience for both UI and developer experience. But before that, let me explain how does React server component work and how we can use them. It's important to note that React server components are exclusively a React feature. There's been a lot of debate on the internet now.
Comments