Recipe
robots.txt + sitemap.xml writer
Generate standards-compliant robots.txt and sitemap.xml files for your Next.js App Router project.
Ingredients
- Next.js 14+ with App Router
- Dynamic route segments for sitemap entries
- Public base URL in environment variable
Steps
- 1
Create the robots.txt route
Add
app/robots.ts. Export a default function returning aRobotsobject with rules and sitemap URL. - 2
Build the sitemap generator
Add
app/sitemap.ts. Export a default function that fetches dynamic slugs and maps them to URL entries with lastModified dates. - 3
Verify output
Visit
/robots.txtand/sitemap.xml. Both should return valid plaintext and XML respectively.
Pro tip
Use generateStaticParams in your sitemap if you have a known set of routes at build time. For fully dynamic sites, lean on incremental static regeneration.