Avatar

Otabek

Developer

Server-Side Rendering (SSR) with Next.js: A Comprehensive Guide

thumbnail

Hello everyone! If you've ever wondered how to make your web applications faster, more SEO-friendly, and dynamically rendered, you're in for a treat. Today, I'll walk you through Server-Side Rendering (SSR) with Next.js, one of the framework's standout features. We'll break down what SSR is, why it's valuable, and how to implement it in your Next.js projects. Plus, I'll share some personal insights and example code to make it all crystal clear.

What is Server-Side Rendering?

Server-Side Rendering is a method where your React components are rendered on the server instead of the client. This means when a user requests a page, the server generates the complete HTML and sends it to the client. This approach can greatly enhance performance and SEO because search engines and browsers get fully-rendered pages right from the start.

Why SSR with Next.js?

SEO Benefits

With SSR, search engines can easily crawl and index your pages because they get fully-rendered HTML. This is a big plus for content-heavy sites like blogs or e-commerce platforms.

Faster Time-to-Interactive

By sending a pre-rendered HTML page, users see meaningful content faster, improving perceived performance.

Dynamic Content

SSR is perfect for applications that need to show up-to-date content, like news websites or dashboards.

Setting Up SSR in Next.js

Let's get our hands dirty with some code!

Basic Setup

First, make sure you have Next.js installed. If not, you can create a new project with:

npx create-next-app my-ssr-app
cd my-ssr-app

Using getServerSideProps Next.js makes SSR easy with the getServerSideProps function. Let's see how to fetch and render data on the server.

import React from "react";

const UserProfile = ({ user }) => {
  return (
    <div>
      <h1>{user.name}</h1>
      <p>Email: {user.email}</p>
      <p>Username: {user.username}</p>
    </div>
  );
};

export async function getServerSideProps(context) {
  const res = await fetch(`https://jsonplaceholder.typicode.com/users/1`);
  const user = await res.json();

  return {
    props: {
      user,
    },
  };
}

export default UserProfile;

Explanation:

  • getServerSideProps: This function runs on the server for every request. In this example, it fetches user data from a public API.
  • Props Passing: The fetched data is passed as props to the UserProfile component.
  • Dynamic Content: Since getServerSideProps runs for each request, it ensures the user sees the most up-to-date data.

Dynamic Routing with SSR

Let's make it more dynamic by fetching user data based on the URL.

import React from "react";

const UserProfile = ({ user }) => {
  return (
    <div>
      <h1>{user.name}</h1>
      <p>Email: {user.email}</p>
      <p>Username: {user.username}</p>
    </div>
  );
};

export async function getServerSideProps(context) {
  const { id } = context.params;
  const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
  const user = await res.json();

  return {
    props: {
      user,
    },
  };
}

export default UserProfile;

Explanation:

  • Dynamic Routes: Using [id].js in the pages directory, Next.js knows to handle dynamic routes.
  • context.params: This contains the route parameters, allowing us to fetch data based on the id.

Handling Errors Gracefully

What if the data fetch fails? Let’s handle that:

export async function getServerSideProps(context) {
  try {
    const { id } = context.params;
    const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);

    if (!res.ok) {
      throw new Error("Failed to fetch user");
    }

    const user = await res.json();

    return {
      props: {
        user,
      },
    };
  } catch (error) {
    return {
      props: {
        error: "User not found",
      },
    };
  }
}

const UserProfile = ({ user, error }) => {
  if (error) {
    return <div>{error}</div>;
  }

  return (
    <div>
      <h1>{user.name}</h1>
      <p>Email: {user.email}</p>
      <p>Username: {user.username}</p>
    </div>
  );
};

Conclusion

And there you have it! With Server-Side Rendering in Next.js, you can build applications that are both fast and SEO-friendly. Whether you're creating a simple blog or a dynamic e-commerce site, SSR ensures your users get the best experience. I hope this guide gives you a solid understanding of how to implement SSR in your projects. Feel free to experiment with these concepts and see how they can benefit your applications. Happy coding, and let me know if you have any questions or thoughts in the comments!

Copyright ⓒ —2025