DEV Community

Cover image for The Power of Server-Side Rendering in Next.js: Faster Loading, Better Performance by Anik routh
Anik Routh
Anik Routh

Posted on

The Power of Server-Side Rendering in Next.js: Faster Loading, Better Performance by Anik routh

Server-side rendering (SSR) is an effective approach for improving web application performance and loading speed. SSR allows you to render your React components on the server side before sending the HTML to the client in the context of Next.js, a popular React framework. This can result in faster initial page loads and improved SEO.

Here’s an example of how you can use server-side rendering in Next.js to achieve faster loading and better performance:

1. Setup:
Make sure you have Node.js and npm installed on your machine.

2. Create a New Next.js Project:
Open your terminal and run the following commands to create a new Next.js project:

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

3. Create a Server-Side Rendered Page:

In your project’s pages directory, create a new file called ssr-page.js (or any other name you prefer).

// pages/ssr-page.js
import React from 'react';

const SsrPage = ({ serverTime }) => {
  return (
    <div>
      <h1>Server-Side Rendered Page</h1>
      <p>Server time: {serverTime}</p>
    </div>
  );
};

export async function getServerSideProps() {
  // This function runs on the server side and fetches data before rendering the page.
  const serverTime = new Date().toString();
  return {
    props: {
      serverTime,
    },
  };
}

export default SsrPage;
Enter fullscreen mode Exit fullscreen mode

In this example, the getServerSideProps the function is responsible for fetching data on the server side before rendering the page. We're simply getting the current server time here.

4. Start the Development Server:
Run the following command to start the development server:

npm run dev

This will start the Next.js development server, and you should be able to access your application at http://localhost:3000.

  1. Open your web browser and navigate to http://localhost:3000/ssr-page. You should see the "Server-Side Rendered Page" along with the server time displayed on the page.

  2. The key takeaway here is that the getServerSideProps function is executed on the server side every time a request is made to the /ssr-page route. This allows you to fetch data, perform server-side computations, and customize the content of the page before it's sent to the client.

Server-side rendering can greatly improve initial page load times and provide better SEO, especially for dynamic content. However, it’s important to note that there are cases where static site generation (SSG) might be a more suitable choice, as it can provide even better performance in certain scenarios.

Remember to refer to the official Next.js documentation for more in-depth information and advanced usage: https://nextjs.org/docs/advanced-features/pages

Check out my Agency website and click here.

Top comments (0)