How To Use Server-Side Rendering in Nextjs Apps For Better SEO
How To Use Server-Side Rendering in Nextjs Apps For Better SEO
js Apps for
Better SEO
freecodecamp.org/news/server-side-rendering-in-next-js-for-improved-seo
Joan Ayebola
Server-side rendering (SSR) is a web development technique that can help
improve your site's SEO. It does this by generating HTML content on the server
in response to a user's request.
This approach contrasts with client-side rendering (CSR), where content is delivered as a
basic HTML shell, and JavaScript fetches and displays data in the browser.
SSR offers significant SEO advantages, making it a perfect fit for Next.js, a popular React
framework. Let's discuss how using SSR with Next.js can elevate your website's search
engine visibility.
Table of Contents
1/11
This is unlike client-side rendering (CSR), where the browser downloads a basic HTML
structure and then uses JavaScript to fetch and display the content.
First, you need to install Next.js. You can do this using create-next-app, which sets up a
new Next.js project with a default configuration. Run the following command in your terminal:
This command creates a new Next.js application in a folder called my-next-app and starts
the development server.
pages/: This folder contains all the pages of your application. Each file represents a
route in your app.
public/: Static assets like images can be placed here.
styles/: Contains CSS files for styling your application.
2/11
// pages/index.js
import React from 'react';
Let's discuss this code in some detail. For the home component:
You can add error handling to the getServerSideProps function to manage any issues that
might arise during data fetching. Here's an example:
3/11
export async function getServerSideProps() {
try {
const res = await fetch('https://api.example.com/data');
if (!res.ok) {
throw new Error('Failed to fetch data');
}
const data = await res.json();
return {
props: {
data,
},
};
} catch (error) {
console.error(error);
return {
props: {
data: { message: 'Error fetching data' },
},
};
}
}
Open your browser and go to http://localhost:3000. You should see the message
fetched from the API displayed on the page.
Next.js determines which rendering method to use based on the functions you implement in
your page components (getStaticProps and getServerSideProps).
Next.js uses the pages/ directory to define routes. Each file in this directory corresponds to a
route in your application.
pages/index.js → /
4/11
pages/about.js → /about
pages/posts/[id].js → /posts/:id
// pages/index.js
import React from 'react';
getStaticProps is used for static generation. It runs at build time and allows you to fetch
data and pass it to your page as props. Use this for data that doesn't change often.
Example:
5/11
// pages/index.js
import React from 'react';
return {
props: {
posts,
},
};
}
getServerSideProps is used for server-side rendering. It runs on every request and allows
you to fetch data at request time.
Example:
6/11
// pages/index.js
import React from 'react';
return {
props: {
data,
},
};
}
Client-side rendering (CSR) can cause issues with search engines struggling to index
content properly since it is rendered in the user's browser using JavaScript.
SSR, however, renders content on the server before sending it to the user's browser,
ensuring the HTML is complete and can be easily crawled and indexed by search engines.
Use SSR for important pages: Ensure that key pages, such as landing pages, blog posts,
and product pages, are rendered on the server to facilitate better indexing.
7/11
// pages/blog/[id].js
import React from 'react';
import { useRouter } from 'next/router';
import Head from 'next/head';
return (
<div>
<Head>
<title>{post.title}</title>
<meta name="description" content={post.excerpt} />
</Head>
<h1>{post.title}</h1>
<p>{post.content}</p>
</div>
);
};
return {
props: {
post,
},
};
}
Search engines like Google use page load speed as a ranking factor. SSR can improve initial
load time because the server sends a fully rendered page to the browser, enhancing
perceived performance and user experience.
Optimize server response time: Ensure your server is optimized for quick responses. Use
caching strategies to reduce server load.
8/11
Example – cache-control header for SSR:
return {
props: {
data,
},
};
}
When sharing links on social media, platforms like Facebook and Twitter scrape the URL
content to generate previews. SSR ensures that necessary metadata is available in the initial
HTML, resulting in better previews and increased click-through rates.
Manage meta tags with next/head: Use the next/head component to add meta tags for
social media and SEO.
9/11
Page Component: This component uses next/head to add SEO meta tags, including
Open Graph tags for social media previews. This ensures that when the page is
shared, social media platforms can generate rich previews with the provided metadata.
Pre-render pages with static generation (SSG) for less dynamic content: Use SSG for
pages that don’t change often to reduce server load and improve performance.
return {
props: {
data,
},
revalidate: 10, // Revalidate at most once every 10 seconds
};
}
StaticPage Component: This component displays static content fetched from an API.
getStaticProps Function: This function fetches data at build time and revalidates it
every 10 seconds, ensuring the content is always fresh while reducing server load.
Conclusion
Using server-side rendering and Next.js together is like giving your website an extra boost for
search engines. With pre-built content for search engines and a smooth experience for
visitors, your site is set up to be seen by more people naturally.
This works great for any kind of website, from online stores to blogs. Next.js with SSR makes
it easy to build a website that search engines love and users enjoy.
10/11
That's all for this article! If you'd like to continue the conversation or have questions,
suggestions, or feedback, feel free to reach out to connect with me on LinkedIn. And if you
enjoyed this content, consider buying me a coffee to support the creation of more developer-
friendly contents.
Joan Ayebola
Hi, I am Joan, a frontend developer and technical writer who's deeply passionate
about open-source technologies.
If you read this far, thank the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than
40,000 people get jobs as developers. Get started
11/11