However, the incorporation of SEO in NextJS is very easy mainly because NextJS is first server-side rendering and second, pre-rendering. Here are some strategies for effective SEO in a NextJS project.
You can use the next/head component for managing HTML head elements that are also meant to optimize for SEO, like title, description and other meta tags:
import Head from 'next/head';
export default function MyPage() {
return (
<>
<Head>
<title>My Page Title</title>
<meta name="description" content="This is a brief description of the page for SEO." />
<meta name="keywords" content="keyword1, keyword2, keyword3" />
<meta property="og:title" content="My Page Title" />
<meta property="og:description" content="This is a brief description of the page for social media." />
<meta property="og:image" content="/path-to-image.jpg" />
<meta property="og:url" content="https://mywebsite.com/mypage" />
<meta name="twitter:card" content="summary_large_image" />
</Head>
<main>
{/* Page content here */}
</main>
</>
);
}
Create dynamic meta tags by using the getStaticProps or the getServerSideProps method.
export async function getStaticProps() {
// Fetch your data here (e.g., page title, description)
const pageData = await fetchData();
return {
props: {
pageData,
},
};
}
function MyPage({ pageData }) {
return (
<>
<Head>
<title>{pageData.title}</title>
<meta name="description" content={pageData.description} />
</Head>
{/* Page content */}
</>
);
}
Use JSON-LD to add structured data (schema.org) to the content improving its understanding by a search engine:
import Head from 'next/head';
export default function MyPage() {
const schemaData = {
"@context": "https://schema.org",
"@type": "Organization",
name: "My Website",
url: "https://oneclickitsolution.com",
logo: "https://oneclickitsolution.com/logo.png",
};
return (
<>
<Head>
<script type="application/ld+json">
{JSON.stringify(schemaData)}
</script>
</Head>
{/* Page content */}
</>
);
}
You can create a sitemap using the package next-sitemap:
npm install next-sitemap
Then, implement this in a next-sitemap.config.js file:
module.exports = {
siteUrl: 'https://oneclickitsolution.com',
generateRobotsTxt: true, // Optionally will create a robots.txt file
};
Use the following command to generate the sitemap:
npx next-sitemap
Control which page is crawled with the help of a robots.txt file. You'll add a robots.txt to your public directory:
User-agent: *
Disallow: /api/
All these steps, when applied to NextJS , make the website be enhanced for SEO, and all that entails your contents are accessed better and rank much higher up within the search engine. Ready to transform your business with our technology solutions? Contact Us today to Leverage Our ReactJS Expertise.