How to Create a Sitemap and robots.txt in Next.js App Router

At a glance
Pick five URLs from the generated sitemap and open each on the public deployment. Check that they return successful pages, use the intended canonical hostname and do not have a noindex directive. Do the same for a recently edited article and confirm lastModified represents a real content change.
A sitemap and a robots.txt file have different jobs. A sitemap lists canonical URLs you want search engines to discover, while robots.txt gives crawlers instructions about which paths they may request. Neither file guarantees indexing or ranking, but correctly configured files help search engines understand how to crawl a site.
This tutorial uses the Next.js App Router metadata file conventions. Replace the example domain and routes with the real URLs for your own project.
Compare the sitemap with the actual indexable pages
Pick five URLs from the generated sitemap and open each on the public deployment. Check that they return successful pages, use the intended canonical hostname and do not have a noindex directive. Do the same for a recently edited article and confirm lastModified represents a real content change.
Exclude redirects, missing pages, private routes and deliberately noindexed pages. A sitemap is a discovery aid, not a command to index a page. Do not give every article today's modification date merely because the sitemap function ran today.
Open /robots.txt separately and confirm its sitemap URL uses the public domain. Blocking a route in robots.txt does not make it private and can prevent a crawler from seeing a noindex instruction. Protect private data with authentication and authorisation.
Reference: Next.js robots convention.
1. Create app/sitemap.ts
Next.js supports a special sitemap.ts file that returns a typed list of URLs. For a small site, you can begin with a simple version:
import type { MetadataRoute } from "next";
export default function sitemap(): MetadataRoute.Sitemap {
const baseUrl = "https://www.example.com";
return [
{
url: baseUrl,
lastModified: new Date(),
},
{
url: `${baseUrl}/blog`,
lastModified: new Date(),
},
{
url: `${baseUrl}/about`,
lastModified: new Date(),
},
];
}
Save this file at app/sitemap.ts. Next.js serves the generated output at /sitemap.xml. Use your actual canonical domain and only include real, indexable URLs that you want search engines to discover.
Official documentation: https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap
2. Generate blog URLs from your content
If your project stores articles as Markdown files, generate sitemap entries from the same source of truth used by your blog. This avoids maintaining a separate hand-written list that becomes stale. The exact implementation depends on your content loader and whether it can safely run in the sitemap module.
A simplified illustration:
import type { MetadataRoute } from "next";
import { getAllPostsMeta } from "@/lib/blog";
export default function sitemap(): MetadataRoute.Sitemap {
const baseUrl = "https://www.example.com";
const posts = getAllPostsMeta();
return [
{
url: baseUrl,
lastModified: new Date(),
},
...posts.map((post) => ({
url: `${baseUrl}/blog/${post.slug}`,
lastModified: post.updatedDate
? new Date(post.updatedDate)
: new Date(post.date),
})),
];
}
This example assumes getAllPostsMeta() returns valid slugs and date strings. Adapt the import alias and field names to your actual project, and validate date values before relying on them. Avoid using the current date as a fake “last modified” value for every article on every build; use a real publication or update date where possible.
3. Create app/robots.ts
Create a file at app/robots.ts:
import type { MetadataRoute } from "next";
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: "*",
allow: "/",
disallow: ["/admin/", "/private/"],
},
sitemap: "https://www.example.com/sitemap.xml",
};
}
Replace the example paths with routes that should be excluded from crawling, and replace the domain with your production sitemap URL. Next.js generates /robots.txt from this function.
Official documentation: https://nextjs.org/docs/app/api-reference/file-conventions/metadata/robots
4. Understand what robots.txt does not do
A Disallow rule is a crawl instruction, not a reliable way to remove a URL from search results. A blocked URL may still be discovered through links or other signals, and the crawler may not fetch the page to see its noindex directive. If a page should not appear in search results, choose an appropriate indexing control and make sure the crawler can access the directive.
Do not block essential CSS, JavaScript, or image resources if doing so prevents search engines from understanding your pages. Be particularly careful with broad rules such as Disallow: /, which can block crawling of the entire site for matching user agents.
5. Include the right URLs in the sitemap
A useful sitemap should generally include canonical, indexable pages that return successfully. Review these cases:
- Include public blog posts and important category pages when they should be indexed.
- Do not include private, duplicate, redirected, or intentionally
noindexURLs. - Use the preferred HTTPS hostname consistently.
- Check that URLs do not contain local development or preview deployment domains.
- Keep the sitemap current when content is published, updated, or removed.
A sitemap is not a replacement for clear navigation and internal links. Important pages should be reachable through ordinary links on your website too.
6. Verify the files in production
After deploying, open these URLs directly:
https://www.example.com/sitemap.xmlhttps://www.example.com/robots.txt
Confirm that they return the expected content, use the correct production hostname, and contain no malformed or unintended routes. For a sitemap, inspect several article URLs and open them to confirm that they resolve correctly. For robots.txt, review every Disallow path carefully.
You can then submit the sitemap through Google Search Console and Bing Webmaster Tools. Submission helps notify the search engines about the sitemap; it does not guarantee that every URL will be indexed.
Common mistakes
The sitemap contains old or broken URLs
Generate entries from current content metadata and verify that removed slugs are no longer included. If URLs have moved, use appropriate redirects and update internal links.
robots.txt blocks the whole site
Check the exact production file after deployment. A broad rule can unintentionally prevent crawling of public pages.
The sitemap lists pages marked noindex
Keep sitemap entries aligned with your indexing strategy. A page you intentionally do not want indexed generally should not be promoted as an indexable sitemap URL.
Every page has today's lastModified date
Use a real modification date. A date that changes on every build without actual content changes can make the metadata less useful.
Frequently asked questions
Does a sitemap guarantee Google or Bing indexing?
No. It helps search engines discover URLs, but indexing depends on their systems and the page's quality, accessibility, canonicalization, and other signals.
Should I add every URL to my sitemap?
No. Focus on canonical public URLs that you want search engines to consider indexing.
Do I need both sitemap.xml and robots.txt?
They serve different purposes. A sitemap lists URLs for discovery; robots.txt communicates crawl rules. Many public sites use both, configured for their own structure.
Research and references
- Next.js sitemap convention — https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap
- Next.js robots.txt convention — https://nextjs.org/docs/app/api-reference/file-conventions/metadata/robots
- Google Search Central: Build and submit a sitemap — https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap
- Google Search Central: robots.txt introduction — https://developers.google.com/search/docs/crawling-indexing/robots/intro
If the sitemap lists a URL but Search Console still excludes it, use the indexing diagnosis guide to check the URL itself.
Try our QR Code Generator
Create clean, scannable QR codes for testing websites, URLs, and client handoffs.

Ukasha Altaf
Software Developer & Digital Tools Creator
Founder of Ukasha Mart. Building free, accessible browser-based utilities and writing in-depth tutorials on modern technology, freelancing, and practical online work.
Explore Further