Server-Side Rendering (SSR) is a technique used in web development where the server generates HTML for each request and sends it to the client. This approach contrasts with Client-Side Rendering (CSR), where the initial HTML is empty, and JavaScript on the client-side is responsible for rendering the UI.
In this section, we will explore how Server-Side Rendering works in React.js, its benefits, and how to implement it using Next.js's modern App Router, which supports SSR out of the box with React Server Components and async components.
Server-Side Rendering (SSR) involves rendering the initial HTML on the server. This means that when a user requests a page, the server processes the request and sends back fully rendered HTML to the client. The browser then displays this HTML without needing to execute any JavaScript.
Next.js's modern App Router simplifies the process of setting up SSR by leveraging React Server Components and async components. This approach abstracts much of the complexity involved and provides a more efficient way to handle data fetching and rendering.
To get started with the Next.js App Router, you need to have Node.js installed. You can create a new Next.js application using the following commands:
npx create-next-app@latest my-ssr-app --typescript
cd my-ssr-app
npm run dev
This will set up a basic Next.js project with TypeScript support and start a development server.
In the App Router, you define pages in the app/ directory. To create a server-side rendered page, you can use React Server Components and async functions to fetch data.
Here's an example of a simple SSR page:
// app/page.tsx
import React from 'react';
async function getData() {
// Fetch data from an API or database
const res = await fetch('https://api.example.com/data');
if (!res.ok) {
throw new Error('Network response was not ok');
}
return res.json();
}
export default async function HomePage() {
const data = await getData();
return (
<div>
<h1>Welcome to My SSR App</h1>
<ul>
{data.map((item: any) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
);
}
fetch API to make a request and returns the JSON response.HomePage component is a React Server Component that awaits the data fetched by getData and renders it.stale-while-revalidate or revalidate-on-demand to reduce server load and improve performance.// app/page.tsx
import React from 'react';
async function getData() {
try {
const res = await fetch('https://api.example.com/data');
if (!res.ok) {
throw new Error('Network response was not ok');
}
return res.json();
} catch (error) {
console.error('Error fetching data:', error);
return [];
}
}
export default async function HomePage() {
const data = await getData();
if (data.length === 0) {
return <div>No data available</div>;
}
return (
<div>
<h1>Welcome to My SSR App</h1>
<ul>
{data.map((item: any) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
);
}
Server-Side Rendering (SSR) is a powerful technique that enhances the performance, SEO, and user experience of React applications. Next.js's modern App Router provides an excellent framework for implementing SSR with minimal effort. By following best practices such as data fetching optimization and error handling, you can build robust and efficient server-side rendered applications.
In this tutorial, we covered the basics of SSR, its benefits and challenges, and how to implement it using Next.js's App Router. We also explored real-world code examples and best practices to help you get started with SSR in your React projects.