How to Add a Sitemap & Robots.txt in Next.js 15
A complete guide to implementing dynamic sitemaps and robots.txt in Next.js 15 using the App Router — with TypeScript, dynamic routes, and best practices for SEO.

Search engines need a roadmap to your site. Without a proper sitemap.xml, Google and Bing may miss pages, index slowly, or rank you lower than competitors who have one. Next.js 15 makes this dead simple with built-in conventions — no plugins, no XML templates, just TypeScript.
(01) ChapterWhy sitemaps matter for SEO
A sitemap tells search engines which pages exist, when they were last updated, and how important they are relative to each other. For sites with dynamic routes — like blog posts, product pages, or user profiles — a sitemap is critical because crawlers can't always discover these pages through links alone.
- 01Faster indexing of new pages — Google discovers them within hours instead of days.
- 02Priority signals — tell crawlers which pages matter most.
- 03Change frequency hints — help crawlers know when to re-visit.
- 04Required for Google News, Google Discover, and large-scale sites.
(02) ChapterCreating sitemap.ts in the App Router
In Next.js 15, you create a sitemap.ts file directly inside your app/ directory. Next.js automatically serves it at /sitemap.xml — no configuration needed.
import { MetadataRoute } from "next";
export default function sitemap(): MetadataRoute.Sitemap {
const baseUrl = "https://yourdomain.com";
return [
{
url: baseUrl,
lastModified: new Date(),
changeFrequency: "daily",
priority: 1,
},
{
url: `${baseUrl}/blog`,
lastModified: new Date(),
changeFrequency: "weekly",
priority: 0.8,
},
];
}(03) ChapterAdding dynamic routes to the sitemap
The real power comes when you pull dynamic data into the sitemap. If you have blog posts, products, or any data-driven pages, you can map over them and generate URLs automatically.
import { blogs } from "@/lib/blogs";
export default function sitemap(): MetadataRoute.Sitemap {
const baseUrl = "https://yourdomain.com";
const blogUrls = blogs.map((blog) => ({
url: `${baseUrl}/blog/${blog.slug}`,
lastModified: new Date(blog.date),
changeFrequency: "weekly" as const,
priority: 0.7,
}));
return [
{ url: baseUrl, lastModified: new Date(), changeFrequency: "daily", priority: 1 },
...blogUrls,
];
}(04) ChapterSetting up robots.ts
The robots.txt file controls which parts of your site search engines are allowed to crawl. In Next.js 15, you create a robots.ts file in your app/ directory.
import { MetadataRoute } from "next";
export default function robots(): MetadataRoute.Robots {
return {
rules: [
{ userAgent: "*", allow: "/", disallow: ["/_next/", "/api/"] },
{ userAgent: "GPTBot", allow: "/" },
],
sitemap: "https://yourdomain.com/sitemap.xml",
};
}With these two files in place, your Next.js site is fully equipped for modern SEO. No third-party packages needed — just clean TypeScript that Next.js serves automatically.
End of entry — DoabStudios Studio


