SSR pre-renders pages on the server, resulting in faster initial load times and better SEO. CSR, on the other hand, renders pages on the client side, which can lead to slower page loads and less optimal SEO performance.
Key takeaways:
Server-side rendering (SSR) in Next.js pre-renders pages on the server, enhancing SEO and performance.
SSR results in faster initial page loads because the HTML is generated on the server and sent to the client fully formed, hence improving user experience
SSR optimizes content delivery and reduces client-side processing, making it ideal for complex applications.
Implementing SSR in Next.js is straightforward with the getServerSideProps
function, which allows developers to fetch data and pre-render the page on each request.
Server-side rendering (SSR) is a core feature of Next.js, a powerful React framework for building high-performance web applications. SSR refers to rendering web pages on the server before sending them to the client’s browser. This process ensures faster page loads and better SEO.
In traditional client-side rendering (CSR), the server sends a basic HTML file when a user visits a website. The browser then fetches additional JavaScript files and builds the webpage on the user’s device. This process can be slow, especially on slower internet connections or if the website has complex elements.
Learn more about rendering strategies like server side rendering in Next.js with this blog: Understanding rendering in Next.js.
SSR with Next.js addresses these issues by performing most of the rendering on the server. When a webpage is requested, the server executes JavaScript code, retrieves data from APIs, and renders the page as complete HTML content. This means that when the server sends the HTML to the client, the webpage is visible immediately without additional processing.
Next.js simplifies the SSR process with its built-in support. When a page request is made, Next.js invokes the getServerSideProps
function to fetch data, perform computations, and render the HTML on the server before delivering it to the client.
export async function getServerSideProps() {// Fetch data from an API or perform any async operationconst apiData = await fetch('https://educative.io/data').then(res => res.json());// Return the fetched api data as propsreturn {props: {apiData,},};}
The getServerSideProps
function runs on the server every time a page is requested. You need to define it inside a page component file.
It can fetch data from an API or database or perform any asynchronous operation.
The returned object with the props
key contains the data that will be passed as props to the page component.
Want to build something interactive using Next.js? Try this project: Build an Interactive E-Library Using Next.js and Tailwind.
A request is made to the server from the client.
Next.js executes the necessary JavaScript code on the server.
Data is fetched from APIs or databases, and computations are performed.
The page is rendered as HTML and sent to the client’s browser.
The client’s browser displays the pre-rendered page without additional processing delays.
Let's look at this workflow in the form of an illustration:
Improved SEO: SSR enables search engines to easily crawl and index your pages, improving search engine rankings and visibility.
Faster initial page load: Pre-rendered HTML is sent from the server, resulting in shorter load times and a better user experience.
Better performance on low-end devices: SSR reduces client-side processing, making your app more accessible and responsive for users with slower devices or internet connections.
Efficient content delivery: Server-side data fetching reduces the number of API requests and optimizes bandwidth usage, enhancing the overall efficiency of your application.
getServerSideProps
Next.js provides the getServerSideProps
method to handle server-side rendering with ease. Here’s how to implement SSR in a Next.js application:
Run the following command in your terminal to create a new Next.js project:
npx create-next-app my-app
To get a hands on experince on Next.js rendering techniques, refer to the Build a Music Sharing App with Next.js and the MERN Stack project.
HomePage
componentCreate a new file named index.js
inside the pages
directory. This will serve as our SSR-enabled page component.
Learn more about routing and Next.js directory structure, read this blog, Understanding routing in Next.js with the App Router.
We will add the following code in index.js
file:
import React from 'react';const HomePage = ({ serverRenderedData }) => {return (<div><h1>Next.js SSR Example</h1><p>{serverRenderedData}</p></div>);};export async function getServerSideProps() {// Fetch data from an API or perform any async operationconst serverRenderedData = 'Server-rendered data';// Return the data as propsreturn {props: {serverRenderedData,},};}export default HomePage;
We define a HomePage
component that receives the serverRenderedData
as a prop.
We fetch the data using the getServerSideProps
method which runs on the server side.
The fetched data is then passed as a prop to the HomePage
component.
To start the Next.js development server, run the following command in our terminal or command prompt:
npm run dev
Below is a Next.js project created by implementing the above steps:
import React from 'react'; const HomePage = ({ serverRenderedData }) => { return ( <div> <h1>Next.js SSR Example</h1> <p>{serverRenderedData}</p> </div> ); }; export async function getServerSideProps() { // Fetch data from an API or perform any async operation const serverRenderedData = 'Server-rendered data'; // Return the data as props return { props: { serverRenderedData, }, }; } export default HomePage;
Line 1: Import the necessary dependencies.
Lines 3–10: Define the HomePage
component, which receives a prop called serverRenderedData
and returns a <div>
with an <h1>
heading and a <p>
paragraph displaying the value of the serverRenderedData
prop.
Lines 12–22: Define the getServerSideProps
function, which is a special Next.js function for server-side rendering. It assigns the string 'Server-rendered data'
to the serverRenderedData
variable and returns it as a prop.
Line 24: Export the HomePage
component as the default export of the module.
Explore these projects for hands-on practice of SSR in Next.js:
Haven’t found what you were looking for? Contact Us
Free Resources