Amp performance with React Server-side rendering - LogRocket Blog (2024)

Editor’s note: This article was updated on 4 May 2022 to include updated information for Create React App, as well as details about additional SSR frameworks.

Amp performance with React Server-side rendering - LogRocket Blog (1)

In this article, we‘ll investigate different types of rendering for web applications. We’ll take a close look at server-side rendering in React, and we’ll examine the benefits of server-side rendering vs. traditional client-side rendering.

Jump ahead:

  • Rendering with Create React App
  • What is server-side rendering?
  • What are single-page applications?
  • What are static-generated applications?
  • Why move to React server-side rendering?</a
  • Get started with server-side rendering in React
  • Example: server-side rendering in React
  • Do you always need SSR?

Rendering with Create React App

Many of you probably use React CLI, better known as Create React App (CRA), to get your apps up and running. There are many advantages to this approach; however, building with CRA also has a few disadvantages.

For example, when you view source of a webpage from a web app initialized with CRA, you will notice that it’s an almost empty page with just the <head> section but hardly anything within <body>.

Here’s an example:

Amp performance with React Server-side rendering - LogRocket Blog (2)

This is because CRA renders your app on the client side, meaning the built .js file is first downloaded to the user’s browser before the rest of the page starts loading. This increases the initial load time, and some web crawlers are unable to index the site.

Is there a better way to render your app? Yes!

This is where server-side rendering for React comes in.

In this article, I want to introduce you to server-side rending (SSR) with React, reasons to use it, and some popular frameworks for rendering React on the server side. I would also like to address when SSR React does not make sense. This article is aimed at developers who are already working with React on the client side.

Before diving further into server-side rendering with React, let’s review some different types of web applications so that we can better appreciate the benefits of SSR applications.

What is server-side rendering?

Server-side rendering is when content on your webpage is rendered on the server and not on your browser using JavaScript. For example, with a typical PHP or WordPress site, the page is loaded from content that is coming via HTTP, which was rendered on the server and comes as fully rendered HTML.

This is in contrast to a React app built with CRA, which just sends a .js file to the client, and the clients’ browser JavaScript engine creates the markup after the .js file is loaded. Examples of traditional SSR languages/frameworks are PHP, Java, ASP.NET, and Node.js.

Traditional SSR apps were predominant in the early web until the influx of client-side libraries. Now, server-side rendered React apps use Node for the server, which is a key difference from traditional server-rendered apps (we’ll see how later on in this post).

Over 200k developers use LogRocket to create better digital experiencesLearn more →

SSR apps offer faster initial load times and better SEO performance compared to client-side rendered apps, there are some downsides. First, every request leads to a new page being re-rendered from the server to the browser. This means all the scripts, styles, and templates will be reloaded on the browser each time a request is sent to the server, resulting in a poor user experience.

What are single-page applications?

Single-page applications (SPAs), or client-side rendered (CSR) applications, render content in the browser using JavaScript rather than refreshing pages with each request sent to the server. The server sends raw HTML documents while the content is rendered to the HTML document via the browser’s JavaScript.

What are static-generated applications?

Static-generated apps are pre-generated using a static site generator (such as Gatsby) and are stored on the hosting server as static HTML pages. You don’t require Node or other server-side support to deploy these static files to a static hosting server. As a result, when the app is first loaded in the browser, you’ll always get the whole content right away, and the app will then behave like a regular SPA.

Static-generated apps do not support for real-time rendering. Therefore, this type of rendering is not a good option when building real-time web applications, like forums or chat apps.

Why move to React server-side rendering?

As I said before, server-side rendering initially means every page is rendered and loaded from the server. However, with the introduction of server-side (universal) React, things are slightly different.

The initial page is rendered from the server, meaning the subsequent pages load directly from the client. So you have the best of both worlds — the power of the initial server-side content plus the speedy subsequent loads, which requests just the content that is needed for future requests.

In addition to the above benefit, here are some other advantages you get when you move to SSR:

Increased performance

Arunoda Susiripala, formerly an engineer from Vercel, talks about performance being the main reason for moving to server-side rendering. SSR means there is no need for loaders or spinners for the initial load. This means that, generally speaking, SSR will outperform CSR.

SSR apps break JavaScript and CSS into chunks, optimize assets, and render pages on the server before serving to the client browser, resulting in a faster initial load time.

Faster load times lead to a better experience for the end user. This is one of the reasons many large companies are taking the SSR approach for their sites.

Enhanced SEO

By now, you’ve probably heard that Google now crawls web apps built with JavaScript, so you’re better off having server-side rendered content ready for Google and other search engines to crawl your site. Per 10up:

Note that as of now, Google and Bing can index synchronous JavaScript applications — synchronous being the key word. If your app starts with a loading spinner, then fetches content via Ajax, the crawler will only wait a few seconds for loading to complete. This means if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary.

With SSR search engine crawlers can explore the page to improve your app’s SEO performance. This is because all pages are rendered on the server with the relevant metadata, paragraphs, and headings before being served to the client, enabling you to get the benefits of a traditional website’s SEO.

Improved user experience

After the initial load, Universal SSR apps behave like typical SPAs in that the transition between pages and routes is seamless. Only data is sent back and forth, with the HTML content holders not being re-rendered.

Enhanced social sharing

The other advantage of SSR is that you get an elaborate snippet and featured image when sharing your webpage’s content via social media. This will not be possible when you have just client-side rendered apps.

For example, here is what a server-side rendered React app looks like when shared on LinkedIn:

Amp performance with React Server-side rendering - LogRocket Blog (5)

Get started with server-side rendering in React

Getting started without frameworks is possible, but I wouldn’t recommend this approach since there are many considerations and moving parts in a React SSR app. For example, you have to handle bundling, minification, and hot reload (and more) all on your own.

However, if you want to go this route, I’d recommend reading this tutorial by Roger Jin.

React SSR frameworks

I recommend picking up a framework if you want to render React on the server side. Here are some frameworks to consider:

Next.js

Next.js is a great framework with a robust community. Next.js offers a lot of inbuilt features and you don’t have to worry about bundling, minification, or hot reloading. You are able to create pages as React components within files.

You may be used to this if you worked with PHP. In addition to the community and support, there are many large companies using Next.js in production including npm, Netflix, and Auth0.

Razzle

Razzle, a project by Jared Palmer, has been gaining a lot of traction lately. From its GitHub page:

“Razzle is a tool that abstracts all complex configuration needed for SSR into a single dependency — giving you the awesome developer experience of create-react-app, but then leaving the rest of your app’s architectural decisions about frameworks, routing, and data fetching up to you.”

It’s easy to get started with Razzle, and it uses React Router 4 by default (unlike Next.js, which does not have an inbuilt router).

Remix

Remix is a React framework with server-side rendering, easy data fetching and mutations, and resilient developer experience which makes it easy to build web applications with great user experience.

Remix provides quick page loads and fluid transitions by utilizing distributed systems and native browser features rather than clumsy static builds. Because it uses the Web Fetch API rather than Node, it can run everywhere.

Additional alternatives

React is not a silver bullet. Perhaps your team is more familiar with Vue or another JavaScript framework. Maybe a static site will best suit your use case. If you don’t want to use React or if you would like to use a Static Site Generator, there are several alternatives.

Nuxt.js

Nuxt.js is a server-side rendering framework for Vue.js and is popular in the Vue.js community. If you are looking for alternatives Next.js or Razzle in the Vue.js world, do give this a try.

Angular Universal

Angular also provides support for server-side rendering and prerendering solution with Angular Universal. If you are looking for alternatives Next.js, remix or Razzle in the Angular world, do give this a try.

SvelteKit

SvelteKit is an open-source framework based on Svelte for creating high-performance web apps with a quick development time. It also includes server-side rendering that can be configured per-app or per-page, allowing you to turn off SSR when it’s not needed.

Gatsby

You would have seen almost all popular JavaScript developers talk about Gatsby. It is a React-based Static Site Generator that has won the hearts of many with its exceptional UX (user experience) and DX (developer experience).

To be precise, it doesn’t do SSR at runtime. Rather, Gatsby does server-side rendering with Node.js at build time, where it creates static HTML, CSS, and JS when deploying the site.

This leads to blazing-fast load times and has further optimizations such as route-based code splitting and prefetching.

Example: server-side rendering in React

I explored SSR React apps a few months back and created an app with Next.js and hosted it on Now — a serverless platform. N.B.Both Next and Now are from a company called Vercel, which is doing a great job at educating developers about React and serverless technologies, along with offering other fantastic products.

My app fetches data from a WooCommerce (a WordPress eCommerce plugin) REST API endpoint and displays it in a Next.js app. You can check out my app on GitHub and take a look at the demo here.

Do you always need SSR?

The short answer would be no. Not all apps need server-side rendering, especially apps with a dashboard and authentication that will not need SEO or sharing via social media. Plus, the expertise for building a server-rendered React app is higher than an app initialized using create-react-app.

Most importantly, SSR React apps cost a lot more in terms of resources since you need to keep a Node server up and running. There are times you may be better off going the serverless route when you want to choose server-side rendering for your React applications.

Conclusion

Client-side rendered React apps are great but having apps rendered on the server have noticeable benefits.

As we covered in this post, the benefits include:

  1. Performance
  2. Search engine visibility
  3. User experience
  4. Social sharing

I would highly encourage you to explore server-side rendering for your React apps and use it for your next product to see these benefits in action.

Get set up with LogRocket's modern React error tracking in minutes:

  1. Visit https://logrocket.com/signup/ to getan app ID
  2. Install LogRocket via npm or script tag. LogRocket.init() must be called client-side, notserver-side

    • npm
    • Script tag
    $ npm i --save logrocket // Code:import LogRocket from 'logrocket'; LogRocket.init('app/id'); 
    // Add to your HTML:<script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script><script>window.LogRocket && window.LogRocket.init('app/id');</script> 
  3. (Optional) Install plugins for deeper integrations with your stack:
    • Redux middleware
    • NgRx middleware
    • Vuex plugin

Get started now

Amp performance with React Server-side rendering - LogRocket Blog (2024)

FAQs

Is React good for server-side rendering? ›

Server-side rendering can benefit SEO for a React app by providing fully rendered HTML content to search engine crawlers, improving indexability, and enhancing the website's visibility in search engine results.

Does server-side rendering improve performance? ›

One of the most significant benefits of SSR is its potential to improve the performance of your website. By offloading some of the rendering tasks to the server, you can reduce the amount of work the user's browser needs to do, resulting in faster initial load times and a smoother user experience.

What is the performance of SSR in React? ›

In React, SSR pre-renders components on the server, so the user gets a usable page faster. Once JavaScript loads, React takes over for interactivity. It's a trade-off: quicker initial load vs. more work on the server.

How do I run server-side in React? ›

Implementing Server Side Rendering using Express. js
  1. Step 1: Create a new Express.js application. ...
  2. Step 2: Set up the environment configuration. ...
  3. Step 3: Set up the server. ...
  4. Step 4: Create the home page. ...
  5. Step 5: Create the products page. ...
  6. Step 6: Create the API endpoint. ...
  7. Step 7: Start the server.

What are the downsides of SSR? ›

However, SSR also comes with downsides, including: Increased server load. Delayed interactivity. A more complex development process.

Is SSR better for SEO? ›

In conclusion, server-side rendering is generally better for SEO than client-side rendering, as it allows search engines to easily crawl and index the content on a website.

Why not use server side rendering? ›

Server-Side Rendering Disadvantages

It can cause compatibility issues. Faster load times. Higher server load for bigger applications. Ideal for static websites.

What are the disadvantages of server-side processing? ›

However, it also comes with some disadvantages, including increased server load, complexity, limited interactivity, and difficulty with client-side scripts.

Is SSR faster than CSR? ›

Initial page load time

On the other hand, SSR offers faster initial page loads since the fully rendered HTML is sent to the client.

When would you use SSR? ›

So, unlike SSG where the page is already rendered in the server waiting to be served to the client, SSR renders the page on the server upon receiving a request. SSR is ideal for websites with dynamic or personalized content that changes frequently, like e-commerce websites or social media platforms.

How is SolidJS performance compared to React? ›

Benchmark tests prove that SolidJS surpasses React regarding rendering speed and memory use. Solid's reactivity model and the virtual DOM contribute to excellent performance.

Does UseEffect work in SSR? ›

SSR component

UseEffect is actually a client side hook. You cannot use it on the server. You have to make that component a client component to use hooks like useEffect, useState, any click events, any form events etc. You can make a component client by writing "use client" on top of your code ie, first line.

What is the benefit of server side rendering in React? ›

Server−side rendering in React is an excellent option for rendering web pages to improve initial page load speed, distribution of content, SEO, and user experience. The best thing about server−side rendering in React is the availability of platforms and frameworks that make complex concepts easier to implement.

What is the difference between server side rendering and client-side rendering? ›

Server-Side Rendering (SSR) suits static content, ensuring faster initial loads and enhanced SEO due to pre-rendering. Client-Side Rendering (CSR) excels in dynamic, interactive content, offering a smoother user experience with real-time updates.

What is the difference between React server component and SSR? ›

SSR is an approach to rendering apps on the server side that involves delivering the HTML to the client so the browser will render it. On the other hand, React Server Components work with SSR by using an intermediary structure to enable rendering without delivering any bundles to the client side.

Is React 18 server-side rendering? ›

React 18 introduces substantial SSR performance improvements, mostly behind the scenes, with a few opt-in options, especially for non-framework users. The most interesting idea is server-side rendering to improve performance, and this article will teach you all about it.

Is React Router server-side rendering? ›

React Router is a popular library used for client-side routing in React applications. However, it can also be used for server-side rendering (SSR) to boost the efficiency of our application. SSR is a technique that allows your web pages to be rendered on the server before being sent to the client.

What is the best server-side rendering framework? ›

Based on the above pros and cons, and surveys, we can conclude that NextJS is the best serverside rendering framework for future implementations. However, if we look at the future for front end development, we can see that Vue is also doing well in the industry.

Is react native server-side rendering? ›

Server-side rendering (SSR) offers significant advantages for React Native applications, including improved performance, enhanced SEO, and optimized user experience.

Top Articles
The Truth Revealed - Is Pokemon Go Free Pokecoins Legit
Financial Modeling
Mychart Mercy Lutherville
Zitobox 5000 Free Coins 2023
Encore Atlanta Cheer Competition
Beds From Rent-A-Center
Vocabulario A Level 2 Pp 36 40 Answers Key
My Vidant Chart
Hello Alice Business Credit Card Limit Hard Pull
Iron Drop Cafe
The Blind Showtimes Near Showcase Cinemas Springdale
Costco Gas Foster City
Lax Arrivals Volaris
Ts Lillydoll
Chic Lash Boutique Highland Village
Colorado mayor, police respond to Trump's claims that Venezuelan gang is 'taking over'
065106619
Osborn-Checkliste: Ideen finden mit System
Where Is The Nearest Popeyes
Moving Sales Craigslist
Aaa Saugus Ma Appointment
Curver wasmanden kopen? | Lage prijs
Iu Spring Break 2024
Pocono Recird Obits
Certain Red Dye Nyt Crossword
Jesus Revolution Showtimes Near Regal Stonecrest
Catchvideo Chrome Extension
Annapolis Md Craigslist
Lindy Kendra Scott Obituary
Askhistorians Book List
What Is Opm1 Treas 310 Deposit
Primerica Shareholder Account
Frommer's Belgium, Holland and Luxembourg (Frommer's Complete Guides) - PDF Free Download
How to Use Craigslist (with Pictures) - wikiHow
Bay Focus
Elgin Il Building Department
Robeson County Mugshots 2022
How To Paint Dinos In Ark
Smith And Wesson Nra Instructor Discount
Sunrise Garden Beach Resort - Select Hurghada günstig buchen | billareisen.at
Gun Mayhem Watchdocumentaries
Dispensaries Open On Christmas 2022
Jamesbonchai
Tfn Powerschool
M&T Bank
Top 1,000 Girl Names for Your Baby Girl in 2024 | Pampers
VerTRIO Comfort MHR 1800 - 3 Standen Elektrische Kachel - Hoog Capaciteit Carbon... | bol
The Average Amount of Calories in a Poke Bowl | Grubby's Poke
O'reilly's Eastman Georgia
Who We Are at Curt Landry Ministries
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6081

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.