Everything the App Router gives you for search visibility: the Metadata API, dynamic generateMetadata, canonicals, sitemap.ts, robots.ts and JSON-LD structured data — with copy-paste code.

Francis K Njenga
Senior Developer
Next.js solves the hardest historical SEO problem — JavaScript content that crawlers never see — by rendering on the server by default. But server rendering is only the foundation. Rankings are won with the details: unique titles and descriptions per page, canonical URLs, a sitemap that stays current, structured data that earns rich results, and Core Web Vitals in the green. The App Router has first-class APIs for every one of these. Here is the complete setup we apply to client sites.
Export a metadata object from app/layout.tsx. Set metadataBase first — it makes every relative OG image and canonical URL resolve correctly. Use a title template so child pages get consistent branding without repetition.
import type { Metadata } from "next";
export const metadata: Metadata = {
metadataBase: new URL("https://yourdomain.com"),
title: {
default: "Your Brand — What you do in five words",
template: "%s | Your Brand",
},
description: "One compelling sentence about the whole site.",
openGraph: {
type: "website",
siteName: "Your Brand",
images: [{ url: "/og-default.png", width: 1200, height: 630 }],
},
twitter: { card: "summary_large_image" },
robots: { index: true, follow: true },
};Child pages override only what differs — the template appends the brand.
For database-driven pages (products, articles, profiles), export an async generateMetadata function. It receives the same params as the page, so titles, descriptions, canonicals and OG images can be per-record. Two rules that matter:
alternates.canonical — especially when query strings or trailing variants exist.import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { getArticle } from "@/lib/articles";
type Props = { params: Promise<{ slug: string }> };
export async function generateMetadata({ params }: Props): Promise<Metadata> {
const { slug } = await params;
const article = await getArticle(slug);
if (!article) return { title: "Article not found" };
return {
title: article.metaTitle ?? article.title,
description: article.metaDescription ?? article.description,
alternates: { canonical: "/articles/" + slug },
openGraph: {
type: "article",
title: article.title,
description: article.description,
publishedTime: article.date,
images: [{ url: article.coverImage, width: 1200, height: 630 }],
},
};
}
export default async function ArticlePage({ params }: Props) {
const { slug } = await params;
const article = await getArticle(slug);
if (!article) notFound();
return <article>{/* ...render... */}</article>;
}This mirrors exactly how our own article pages generate their SEO tags.
Forget hand-maintained XML. Export a function from app/sitemap.ts and Next.js serves it at /sitemap.xml, regenerated with your data. Include lastmod dates — crawlers use them to prioritize recrawls. app/robots.ts does the same for robots rules; keep admin and API paths out of the index.
import type { MetadataRoute } from "next";
import { getAllPublishedArticles } from "@/lib/articles";
const BASE = "https://yourdomain.com";
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const articles = await getAllPublishedArticles();
const staticRoutes = ["", "/services", "/work", "/process", "/contact-us"]
.map((path) => ({
url: BASE + path,
lastModified: new Date(),
changeFrequency: "monthly" as const,
priority: path === "" ? 1 : 0.8,
}));
const articleRoutes = articles.map((a) => ({
url: BASE + "/articles/" + a.slug,
lastModified: new Date(a.updatedAt),
changeFrequency: "weekly" as const,
priority: 0.6,
}));
return [...staticRoutes, ...articleRoutes];
}Add app/robots.ts alongside: allow /, disallow /admin and /api, and point Sitemap: at this file.
JSON-LD tells Google what a page is — an article, a product, a local business, an FAQ. The App Router pattern is a <script type="application/ld+json"> rendered inside the page. Validate with Google's Rich Results Test before shipping. For a local business, LocalBusiness schema with address, geo and opening hours is one of the highest-ROI snippets you can add.
export function ArticleJsonLd({ article }: { article: Article }) {
const jsonLd = {
"@context": "https://schema.org",
"@type": "TechArticle",
headline: article.title,
description: article.description,
image: "https://yourdomain.com" + article.coverImage,
datePublished: article.date,
dateModified: article.updatedAt,
author: {
"@type": "Person",
name: article.authorName,
jobTitle: article.authorRole,
},
publisher: {
"@type": "Organization",
name: "Your Brand",
logo: { "@type": "ImageObject", url: "https://yourdomain.com/logo.png" },
},
};
return (
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
);
}Keep the data truthful: schema that contradicts visible content can be penalized.
<h1> per page, logical heading order, real <a href> links (not onClick divs), descriptive alt text.notFound() (a real 404), not render an empty page with a 200.next.config.mjs — link equity survives.Reality check: technical SEO makes you eligible to rank; content quality and authority decide whether you actually do. Get the machinery right once, then invest in pages worth ranking.
Every site we build ships with this entire setup wired in from day one. If your current site is invisible to search, we can audit and fix it.

Senior Developer
Francis Njenga is an experienced Lead Developer specializing in React, Next.js, and modern JavaScript frameworks, with a strong background in web development.