DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

Server-Side Rendering (SSR) in React

Server-Side Rendering is a technique in React where the initial rendering of the application takes place on the server rather than in the browser. This can improve the perceived performance and search engine optimization (SEO) of your application.

Server-Side Rendering

SSR involves rendering the initial HTML content on the server and sending it to the browser. This can improve the time-to-interactive for users and provide better SEO by delivering fully rendered HTML to search engines.

Let's walk through an example of setting up a basic SSR React application using Next.js.

Step 1: Problem Code

Consider a typical CSR React application where the initial rendering occurs on the client side.

// App.js
import React from 'react';

const App = () => {
  return (
    <div>
      <h1>Hello, Server-Side Rendering!</h1>
    </div>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Step 2: Solution - Server-Side Rendering with Next.js

Let's use Next.js to implement SSR for our React application.

  1. Install Next.js:
   npm install next react react-dom
Enter fullscreen mode Exit fullscreen mode
  • Next.js simplifies the process of implementing server-side rendering in a React application.
    • By default, pages inside the pages directory in your project are treated as server-side rendered routes.
  1. Create pages/index.js in your project directory:
   // pages/index.js
   import React from 'react';

   const Home = () => (
     <div>
       <h1>Hello, Server-Side Rendering!</h1>
     </div>
   );

   export default Home;
Enter fullscreen mode Exit fullscreen mode
  1. Create a custom script in your package.json for running Next.js:
   "scripts": {
     "dev": "next dev"
   }
Enter fullscreen mode Exit fullscreen mode
  1. Run the application:
   npm run dev
Enter fullscreen mode Exit fullscreen mode

Now, your application is running with server-side rendering. When you access http://localhost:3000 in your browser, you'll receive the fully rendered HTML from the server.

By using Next.js for server-side rendering, you get the benefits of faster initial page loads and improved SEO. The server sends fully rendered HTML to the browser, which can be especially advantageous for content-heavy applications and public-facing websites.

Here are some popular React server-side rendering frameworks with documentation links:

  1. Next.js:

    • Documentation
    • Description: A comprehensive React framework supporting server-side rendering and more.
  2. Gatsby:

    • Documentation
    • Description: A static site generator for React with server-side rendering capabilities.
  3. Razzle:

    • Documentation
    • Description: A zero-config framework for server-rendered React applications.
  4. After.js:

    • Documentation
    • Description: A server-side rendering framework built on React Router.
  5. NestJS:

    • Documentation
    • Description: A server-side framework for building scalable applications with Node.js, capable of SSR with React.

Please check the documentation for each framework for detailed information and usage guidelines.

"Your feedback and ideas are invaluable – drop a comment, and let's make this even better!"

😍 If you enjoy the content, please 👍 like, 🔄 share, and 👣 follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

Top comments (0)