OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

Server Side Rendering (SSR) with React

Samaila Bala
February 25th, 2021 · 5 min read

Server Side rendering has been the popular way of rendering web applications before the advent of JavaScript frontend libraries/frameworks e.g React, Angular, Svelte, and Vue.js which brought Client-side rendering to the mainstream. In this article, we will be looking at how to build an SSR application using React but first let’s take a detour to explain what Client-side Rendering and Server-side rendering are.

What is Client-Side Rendering?

Client-side rendering is a way of rendering the content of a web application on the client-side(browser). What it means is when a user makes the initial request the server will return a blank page or a loading screen with some scripts.

1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <meta charset="utf-8" />
6 <title>Client side rendered SPA </title>
7</head>
8
9<body>
10 <noscript>You need to enable JavaScript to run this app.</noscript>
11 <div id="root"></div>
12
13 <script src="app.js"></script>
14 <script src="react-library.js"></script>
15</body>
16
17</html>

The page renders and delivers content to the user after the scripts are fully loaded and compiled. This might lead to a slow initial render time but the upside is that when another request to the server is made, only the content will need to travel from server to client. The script will be responsible for rendering the response. This in turn makes all the subsequent requests after the first one super fast. The major downside of this approach is the script tends to grow as the application grows which can make it less performant as it scales.

What is Server-Side Rendering?

SSR is a way of rendering web applications on the server and then sending the response and content back to the user. What this means is when a user opens a web application a request is sent to the server which returns a response together with the content i.e HTML, CSS, JavaScript, and other assets required to display the page to a user.

So unlike a Client-side rendered application, a page with the content is returned to the user. The downside of this approach is a request is always made to the server whenever a user clicks a link which may be slow as the server has to go through the process of processing the request and then return the HTML, CSS, and JavaScript files.

A solution to this approach is a hybrid of SSR and CSR which is called a Universal or Isomorphic app in some circles. In an Isomorphic app, we can eliminate the slow initial load time by Client-side rendered applications by rendering the initial HTML from the server and then letting the client take over rendering responsibilities thereby eliminating the frequent requests that have to be made to the server in SSR apps.

Benefits of SSR

  • Faster initial load time: because an SSR app only delivers what a user requests for when an initial request is made and also doesn’t have to wait until all the JavaScript files are loaded the Time To First Byte (which is the response time from when a user clicks a link to getting feedback) is faster.
  • Good for SEO: SSR apps are better suited for Search engines(Google, Bing, etc.) as the bots of the Search engines can crawl the entire app and index its pages, as opposed to Client-side rendered apps that load and updates just a single page.
  • Great for static sites: because the server returns a full HTML to the user, SSR can be great for building static sites.

Cons of SSR

  • Frequent server requests: every request made by a user has to be sent back to the server for processing which leads to performance issues.
  • Overall slower load time: because the server has to process each request the load time overall becomes slower compared to single-page applications that only need to fetch all the content needed at the initial load time. Also for large SSR applications, processing requests can take some time which may lead to a slow Time To First Byte.

Getting Started

Now that we have an understanding of what SSR is we will be looking at building an SSR app using a popular React framework called Next.js. According to Wikipedia

Next.js is an open-source React front-end development web framework that enables functionality such as server-side rendering and generating static websites for React based web applications.

Next.js makes creating SSR apps with React less stressful as it handles the complexities of setting everything up and also comes with some exciting features out of the box like:

  • Image Optimization
  • Internationalization
  • Next.js Analytics
  • Zero config
  • Typescript support
  • Fast refresh
  • File system routing
  • API routes
  • Built-in CSS support
  • Code-splitting and Bundling

To get started with Next.js open a terminal and run the code below

1npx create-next-app [app-name]

or

1yarn create next-app [app-name]

This code will initialize a Next.js application. Navigate to the root directory of the application and start the development server by running

1npm run dev

or if you are using yarn

1yarn dev

Pages and Routing

A page in Next.js is a React component file created in the pages directory. Next.js associates each page created to a route based on the file name. If you navigate to the pages directory you will see an index.js file that is created by default when a Next.js application is created. The index.js file is associated with / route and is by default the home page of the application.

Navigate to the pages directory and create an about.js file. Open the file and paste the code below into it and save the file

1import React from 'react'
2const About = () => {
3 return (
4 <div>
5 This is an About page.
6 </div>
7 )
8}
9export default About

Now if you navigate to http://localhost:3000/about in your browser the about page will be rendered to you. We can also create more nested routes for example http://localhost:3000/movies/tenet can be created by creating a tenet.js in the following path pages/movies.

We will be creating a sample movie app to illustrate some of the major concepts of Next.js. Create a data.js file in the root directory and paste the code below

1export default [
2 {
3 slug: 'the-social-network',
4 title: 'The Social Network',
5 description: 'The social network is a story of how Mark Zuckerberg created Facebook and the ensuing lawsuits that followed by the twins who said he stole their idea'
6 },
7 {
8 slug: 'malcolm-and-marie',
9 title: 'Malcolm and Marie',
10 description: 'A black and white romantic drama starring John David Washington and Zendaya. it tells a story of how their relationship is tested on the night of his film premiere.'
11 },
12 {
13 slug: 'tenet',
14 title: 'Tenet',
15 description: 'The latest action film thriller by Christopher Nolan follows a secret agent known as the Protagonist around the world as he tries to stop a pending World war between the future and the past.'
16 }
17]

This file contains the data we will be using for our sample movie application.

Open index.js and replace the contents of the file with the code below

1import Link from 'next/link';
2import movies from '../data';
3export async function getServerSideProps() {
4 return {
5 props: {
6 allMovies: movies,
7 },
8 };
9}
10export default function Home({ allMovies }) {
11 return (
12 <div>
13 <main>
14 <h1>Welcome to a Movie List.</h1>
15 <ul>
16 {allMovies.map((item) => (
17 <li key={item.slug}>
18 <Link href={`/movies/${item.slug}`}>
19 <a>{item.title}</a>
20 </Link>
21 </li>
22 ))}
23 </ul>
24 </main>
25 </div>
26 );
27}

We’ve talked about creating pages and routes. To navigate between pages in Next.js we use the Link component which can be imported from next/link

1<Link href={`/movies/${item.slug}`}>
2 <a>{item.title}</a>
3</Link>

Navigating between pages works by wrapping the <a> tag with the Link component and adding the href attribute to the Link component.

Data Fetching

Next.js has two ways of pre-rendering HTML they are:

  • Static Site Generation: render HTML at build time
  • Server-Side Rendering: render HTML at request time

The way data is fetched in Next.js depends on how a page is rendered. And since this article is focused on SSR we will be using a function called getServerSideProps. The getServerSideProps is a method for fetching data on each request. If the getServerSideProps is exported as an async function on a page Next.js will prerender the page on each request using the data returned by getServerSideProps

1import movies from '../data';
2export async function getServerSideProps() {
3 return {
4 props: {
5 allMovies: movies,
6 },
7 };
8}

In the code snippet above we are returning the sample data we created earlier on whenever we render our page. The props object is passed to our page component so we can access the data in the component.

Dynamic Routing

After saving the file and restarting the dev server you should be presented with a page similar to the screenshot below

null

Now if you try clicking on any of the links in the list you will be taken to a 404 page because the page doesn’t exist. We want to create the pages on the fly based on the movie data. To do that we will be creating a file called [id].js in the pages/movies directory.

1cd pages
2 mkdir movies
3 cd movies
4 touch [id].js

If a file name is wrapped with [] for example [id].js it tells Next.js that it is a dynamic route file. Open the [id].js file and paste the code below

1import { useRouter } from 'next/router';
2import movies from '../../data';
3const Movie = () => {
4 const router = useRouter();
5 const { id } = router.query;
6 const getMovieById = movies.find((item) => item.slug === id);
7 if (!getMovieById) {
8 return <h1>Movie does not exist.</h1>;
9 }
10 return (
11 <div>
12 <h1>{getMovieById.title}</h1>
13 <p>{getMovieById.description}</p>
14 </div>
15 );
16};
17export default Movie;

The useRouter is a react hook that gives us access to the Router object that contains information about the routes. What we are trying to do with the router object is to get the slug so we can use it to retrieve information about the movie. If you save and go back to the application the links should work.

Measuring front-end performance

Monitoring the performance of a web application in production may be challenging and time consuming. OpenReplay is an open-source session replay stack for developers. It helps you replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder.

OpenReplay lets you reproduce issues, aggregate JS errors and monitor your app’s performance. OpenReplay offers plugins for capturing the state of your Redux or VueX store and for inspecting Fetch requests and GraphQL queries.

OpenReplay Redux

Happy debugging, for modern frontend teams - Start monitoring your web app for free.

Conclusion

In this article, we learned how to render React server-side using Next.js. The repository of the application we built in this tutorial is available on GitHub. Next.js offers a lot more than covered in this article so please check out the docs to learn more about the framework.

More articles from OpenReplay Blog

6 React Best Practices For 2021

6 tips for writing better React in 2021

February 10th, 2021 · 5 min read

Is Redux Dead?

Should you still worry about learning Redux?

February 10th, 2021 · 8 min read
© 2021 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552