React Server Components(RSC) vs. Server-Side Rendering(SSR)
React Server Components (RSC) and Server-Side Rendering (SSR) are two different approaches for rendering React components on the server-side. While SSR has been around for a while, RSC is a relatively new experimental feature that is being developed by the React team.
In this blog post, we’ll explore the differences between RSC and SSR and how they work in Next.js, specifically comparing the implementation of these features in Next.js 12 and Next.js 13.
What is Server-Side Rendering (SSR)?
Server-Side Rendering (SSR) is a technique for rendering React components on the server-side and sending the generated HTML to the client. This approach is useful for improving the initial load time of a React application, as the user receives a fully rendered page that can be displayed immediately.
In Next.js 12, SSR is implemented using a function called getServerSideProps
. This function can be added to a page component and is called on the server-side to fetch data and pass it as props to the component. The HTML for the component is then generated on the server-side and sent to the client.
For example, if you have a page called blog.js
, you can create a file called getServerSideProps.js
in the same folder that looks like this:
export async function getServerSideProps(context) {
const res = await fetch('https://example.com/api/v1.com/blog')
const data = await res.json()
return {
props: { posts: data }
}
}
This function fetches blog posts from an API and passes them as props to the blog.js
component. When a user navigates to the /blog
route, Next.js will call this function on the server-side, generate the HTML for the blog.js
component with the fetched data, and send it to the client.
What are React Server Components (RSC)?
React Server Components (RSC) is a new experimental feature that enables the creation of dynamic components on the server-side. With RSC, the server can generate the HTML for a component and send it to the client, but the component’s behavior can still be updated and changed dynamically, without requiring a full-page reload.
In Next.js 13, RSC is implemented using a new API called serverComponents
. This API enables the creation of server-side components that can be reused across multiple requests, reducing server load and improving performance.
To use RSC in Next.js 13, you need to install the next
package and enable the experimental.serverComponents
feature in your next.config.js
file.
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
appDir: true,
},
}
module.exports = nextConfig
Here’s an example of how to create an RSC in Next.js 13:
import { serverComponent } from 'next/server'
export default serverComponent(function MyComponent(props) {
const [count, setCount] = useState(0)
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
)
})
This creates a component called MyComponent
that has state and a button that increments the count when clicked. When this component is rendered on the server-side, the generated HTML is sent to the client. The client can then hydrate the component, which means that the client will take over the rendering of the component and its behavior. Any subsequent changes to the component will be handled on the client-side without requiring a full-page reload.
Comparing Next.js 12 and 13 Implementation
While both SSR and RSC serve similar purposes of improving server-side rendering of React components, they have different use cases and implementation details.
SSR is best suited for pages that have static content or data that changes infrequently. It’s useful for generating fully rendered pages that can be displayed immediately to the user, improving the initial load time of the application. In Next.js 12, SSR is implemented using the getServerSideProps
function, which enables developers to fetch data and pass it as props to the page component.
On the other hand, RSC is best suited for pages that have dynamic content or data that changes frequently. It’s useful for creating interactive components that can be updated on the server-side without requiring a full-page reload. In Next.js 13, RSC is implemented using the serverComponents
API, which enables developers to create components with dynamic behavior that can be rendered on the server-side and hydrated on the client-side.
Another key difference between SSR and RSC is the performance implications. Since RSC components are cached on the server, they can be reused across multiple requests, reducing server load and improving performance. SSR, on the other hand, can result in a slower time to first byte (TTFB) due to the need to fetch data and generate HTML on the server-side for each request.
Conclusion
In summary, React Server Components (RSC) and Server-Side Rendering (SSR) are two different approaches for rendering React components on the server-side. While SSR is best suited for pages with static content or data, RSC is best suited for pages with dynamic content or data. Next.js 12 implements SSR using the getServerSideProps
function, while Next.js 13 introduces RSC using the serverComponents
API. Both approaches have their strengths and weaknesses and can be used to improve the server-side rendering of React applications.