In modern web development, ensuring your site is SEO-friendly is crucial for discoverability. Next.js 14 simplifies this with its metadata API, making it easier to define essential SEO elements like titles, descriptions, keywords, and Open Graph metadata.
This article walks you through setting up SEO metadata in your layout.tsx file and creating a dynamic sitemap.ts file for better indexing by search engines.
Setting Up Metadata in Next.js 14
Next.js 14 provides the Metadata API to configure SEO metadata for your pages. Hereโs an example of how to set this up in layout.tsx
:
// layout.tsx
import { Metadata } from 'next';
const title = 'Narrate - Notion Style Blogging Platform for SaaS Founders';
const description = 'Easily create Notion-style blogs optimized for SEO and audience engagement.';
const keywords = ['Next.js 14 SEO', 'Notion Styled Blogs', 'Notion to Blogs', 'Narrate blog', 'SEO tips', 'AI blogging'];
const image = 'https://narrate.blog/og-image.png';
export const metadata: Metadata = {
title,
description,
icons: ['https://narrate.blog/favicon.ico'],
keywords,
openGraph: {
title,
description,
images: [image],
},
twitter: {
card: 'summary_large_image',
title,
description,
images: [image],
creator: '@eersnington',
},
metadataBase: new URL('https://narrate.blog'),
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head />
<body>{children}</body>
</html>
);
}
Explanation of Key Fields:
title
: Defines the main title for your pages. It helps search engines display an accurate and compelling title in search results.description
: This meta description is displayed under the title in search engine results. A well-crafted description can boost click-through rates by explaining the pageโs purpose succinctly.icons
: Links to the favicon for your site, visible in the browser tab and bookmarks.keywords
: A list of keywords targeting your niche and competitors to improve search discoverability.openGraph
: Used by platforms like Facebook and LinkedIn to display rich link previews. Fields like title, description, and images are optimized for social sharing to boost user engagement.twitter:Configures Twitter Cards to create visually appealing link previews.Includes a custom card type (e.g., summary_large_image) and a specific creator handle for branding.
metadataBase
: Establishes a base URL for constructing absolute URLs within the metadata. Ensures URLs are correct across different environments (local, staging, production).
Dynamic Sitemap Generation
Search engines use sitemaps to understand your siteโs structure. Letโs create a sitemap.ts
file to dynamically generate a sitemap in Next.js.
import type { MetadataRoute } from 'next';
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://narrate.blog',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
},
{
url: 'https://narrate.blog/about',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
{
url: 'https://narrate.blog/features',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.7,
},
{
url: 'https://narrate.blog/blog',
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 0.6,
},
];
}
Explanation:
-
url
: Lists the main pages of your website for search engines to index. -
lastModified
: Dynamically updates to reflect the most recent changes. -
changeFrequency
: Indicates how often a page's content is expected to change. -
priority
: Specifies the importance of each page, helping search engines prioritize crawling.
You can expand this to include more pages as your site grows. Let me know if you need further customization! ๐
Narrate.blog: Take the Hassle Out of SEO
Looking for an effortless blogging platform optimized for SEO? Narrate.blog offers Notion-style simplicity with all the technical heavy lifting handled for you.
From auto-generated sitemaps to AI-powered content, itโs the perfect tool for SaaS founders and creators to grow their audience.
Check it out and let us know how you optimize your Next.js projects! ๐
Top comments (0)