← Back to docs
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. 1

    Create the robots.txt route

    Add app/robots.ts. Export a default function returning a Robots object with rules and sitemap URL.

  2. 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. 3

    Verify output

    Visit /robots.txt and/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.