Understanding SSR and CSR in Next.js

SivaA
3 min readApr 29, 2024

Introduction

In this blog, we will discuss the concepts of Server-Side Rendering (SSR) and Client-Side Rendering (CSR) in the context of Next.js. We will explore how to implement both SSR and CSR in a Next.js application and understand the differences between them.

What is Server-Side Rendering (SSR)?

Server-Side Rendering (SSR) is a process where the server generates the complete HTML page and sends it to the client. The server retrieves the required data, renders the HTML, and then sends the rendered HTML to the client. This allows search engines to crawl and index the content, improves SEO, and provides a better initial load time for users.

In the context of a Next.js application, SSR is used for content that doesn’t change frequently. For example, flight details like flight name, height, and capacity can be rendered on the server and sent to the client. These details are unlikely to change often, so rendering them on the server and sending them to the client reduces the overhead of making additional API calls.

Implementing SSR in Next.js

To implement SSR in Next.js, we can use the getServerSideProps function. This function is a built-in Next.js function that runs on the server and fetches the required data before rendering the page. Let's see how it can be done:

export async function getServerSideProps() {
const flightDetails = await Prisma.flightDetails.findMany({
select: {
id: true,
name: true,
height: true,
capacity: true,
},
});

return {
props: {
flightDetails,
},
};
}

In the code above, we are using the Prisma client to fetch the flight details from the database. We are selecting only the required fields (id, name, height, capacity) using the select option. The fetched flight details are then passed as props to the page component.

In the Next.js component, we can access the flight details and render them as desired:

<ul>
{flightDetails.map((flight) => (
<li key={flight.id}>
<h1>{flight.name}</h1>
<p>Height: {flight.height}</p>
<p>Capacity: {flight.capacity}</p>
</li>
))}
</ul>

With this implementation, the flight details will be fetched from the server every time the page is loaded. This ensures that the data is always up-to-date.

What is Client-Side Rendering (CSR)?

Client-Side Rendering (CSR) is a process where the initial HTML page is sent to the client, and the client’s browser fetches the data and renders the page dynamically. In CSR, the server sends a minimal HTML page with JavaScript code that fetches the data and updates the page’s content. This allows for dynamic content updates without reloading the entire page.

In the context of a Next.js application, CSR is used for content that needs to be frequently updated. For example, flight coordinates, which change continuously, can be fetched and rendered on the client-side. This ensures that the latest coordinates are always displayed to the user.

Implementing CSR in Next.js

To implement CSR in Next.js, we can use the getStaticProps function. This function is a built-in Next.js function that runs at build time and fetches the required data. Let's see how it can be done:

export async function getStaticProps() {
const flightCoordinates = await Prisma.flightCoordinates.findMany({
select: {
name: true,
latitude: true,
longitude: true,
},
});

return {
props: {
flightCoordinates,
},
};
}

In the code above, we are using the Prisma client to fetch the flight coordinates from the database. We are selecting only the required fields (name, latitude, longitude) using the select option. The fetched flight coordinates are then passed as props to the page component.

In the Next.js component, we can access the flight coordinates and render them as desired:

<ul>
{flightCoordinates.map((coordinates) => (
<li key={coordinates.name}>
<h1>{coordinates.name}</h1>
<p>Latitude: {coordinates.latitude}</p>
<p>Longitude: {coordinates.longitude}</p>
</li>
))}
</ul>

With this implementation, the flight coordinates will be fetched on the client-side every time the page is loaded. This ensures that the latest coordinates are always displayed, as they may change frequently.

Conclusion

In this blog, we discussed the concepts of Server-Side Rendering (SSR) and Client-Side Rendering (CSR) in Next.js. We learned how to implement both SSR and CSR in a Next.js application and understand when to use each approach. SSR is suitable for content that doesn’t change often, while CSR is ideal for content that needs frequent updates.

By leveraging SSR and CSR in a Next.js application, we can create dynamic and optimized web pages that provide a better user experience. SSR ensures search engine visibility and faster initial page load times, while CSR allows for real-time updates without page reloads.

Thank you for reading this blog. We hope you found it informative and helpful in understanding SSR and CSR in Next.js.

--

--

SivaA

I am a Software Engineer and YouTube Content Creator, from Dallas Texas