React Server Components. React Server Components is a recent feature introduced with the React team and launched with Next.js 12. Today, I will be talking about the rendering strategies, which is mainly client-side rendering and server-side rendering. Then I'll talk about the React Server Components, what are React Server Components, how it is different than server-side rendering, and how we can use them in our application. We will also see the performance benefits of using React Server Components.
Then we'll do everything coding, a live coding, and see everything up and running. Finally, we'll also talk about some challenges that you might come across when you try to use React Server Components in your existing application.
A bit about myself, my name is Asima. I'm a Senior Front-End Engineer working in Amsterdam. In my free time, I like to create some Front-End videos. You can reach me out on LinkedIn, Twitter.
So finally talking about the rendering strategies, so we have client-side rendering and server-side rendering. In client-side rendering, when a user requests a website, server sends a blank HTML with a script tag with source bundle.js. Browser then downloads the JS file, React gets loaded, and a request is sent to fetch the data from server. After the data is received from the server, the content becomes visible. This approach works great for building high user-interactive websites, as you can reload the components which have been changed instead of reloading the entire page. However, the problem comes as you ship more and more code to our JS bundle. The bundle size increases and browser takes time to download the bundle, because of which we get delayed FCP. Also with client-side rendering, there are SEO problems and you're not able to run them on non-JavaScript devices.
We have another strategy, which is server-side rendering. In server-side rendering, when user requests a website, this time server sends a fully rendered HTML, but this HTML is static. So what browser does is that parallelly it also downloads the JS file, then the comparison is done between the page, which is generated by the browser, and the one which is sent from the server. This is what we call hydration. After hydration, react takes control and content becomes interactive. One of the huge benefit we get from SSR is improved FCP because browser does not have to wait for client to generate an HTML and show content to the user. Also, the issue problem is solved, but one of the problem with SSR is that you can only use server-side rendering on first upload. After that, after hydration the app behaves like a normal client-side rendered application. Now some of you might say that in Next.js, you can create multiple routes and on each route you can use server-side rendering. But what happens is that as you switch between those routes, the state of components is not maintained. This is one of the major difference between server-side rendering and React Server components.
Comments