{
  "id": "add-sitemap-and-robots-txt",
  "type": "prompts",
  "category": "prompts",
  "locale": "en",
  "url": "/prompts/add-sitemap-and-robots-txt",
  "title": "Prompt to Add a Sitemap and robots.txt",
  "description": "AI agent prompt to add a dynamically generated sitemap.xml and a correct robots.txt to a Next.js or Astro project for better search indexing.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "Astro",
    "TypeScript"
  ],
  "tags": [
    "seo",
    "nextjs",
    "astro",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "Give this prompt to your agent to add a standards-compliant `sitemap.xml` and\n`robots.txt` — handling dynamic routes, priority values, and lastmod dates\nwithout using a paid third-party service.\n\n## Main Prompt\n\n```txt title=\"Main Prompt\"\nYou are working in a Next.js 15 App Router project with TypeScript.\nThe site has static pages (/, /about, /pricing) and dynamic blog posts in `src/content/blog/`.\n\nTask: add sitemap.xml and robots.txt using only Next.js built-in file conventions.\n\nRequirements:\n- Create `src/app/sitemap.ts` (NOT `.xml`) using the Next.js `MetadataRoute.Sitemap` return type.\n  - Static URLs: `/`, `/about`, `/pricing` with `priority: 1.0` and `changeFrequency: 'monthly'`.\n  - Dynamic URLs: read all MDX files from `src/content/blog/` using `getCollection('blog')` from\n    `astro:content` — wait, this is Next.js, so use `fs` + `gray-matter` to read frontmatter.\n  - For each post, return `{ url, lastModified, changeFrequency: 'weekly', priority: 0.8 }`.\n  - `url` must be an absolute URL using the `NEXT_PUBLIC_SITE_URL` environment variable.\n- Create `src/app/robots.ts` (NOT `.txt`) using the `MetadataRoute.Robots` return type.\n  - Allow all crawlers for `/*`.\n  - Disallow `/api/*` and `/admin/*` for all crawlers.\n  - Set `sitemap` to the absolute sitemap URL.\n- Add `NEXT_PUBLIC_SITE_URL=https://example.com` to `.env.example`.\n- Do NOT install `next-sitemap` or any sitemap package.\n\nStop and list all files before writing code.\n```\n\n## Implementation Notes\n\n- `sitemap.ts` and `robots.ts` in the `app/` directory are Next.js 13.3+ file conventions; they\n  must export a default function that returns the typed metadata object — not a Response or string.\n- If `NEXT_PUBLIC_SITE_URL` is undefined at build time, the sitemap will contain relative URLs\n  which are invalid per the Sitemap protocol — validate with a startup check.\n- `lastModified` should be a `Date` object, not a string; Next.js serializes it to ISO 8601.\n\n## Expected File Changes\n\n```txt\nsrc/app/sitemap.ts              (new)\nsrc/app/robots.ts               (new)\nsrc/lib/blog.ts                 (new or edited — getAllPosts helper)\n.env.example                    (edited)\n```\n\n## Acceptance Criteria\n\n- `GET /sitemap.xml` returns valid XML with all static and dynamic URLs as absolute URLs.\n- `GET /robots.txt` includes `Disallow: /api/` and `Sitemap: https://example.com/sitemap.xml`.\n- Adding a new blog post MDX file causes its URL to appear in the sitemap after `bun run build`.\n- The sitemap validates at https://www.xml-sitemaps.com/validate-xml-sitemap.html.\n\n## Test Commands\n\n```bash\nbun run build && bun run start\ncurl http://localhost:3000/sitemap.xml | xmllint --format - | head -40\ncurl http://localhost:3000/robots.txt\n# confirm /api/ is disallowed and sitemap URL is absolute\nbun run typecheck\n```\n\n## Common AI Mistakes\n\n- Creating a static `public/sitemap.xml` file instead of the dynamic `src/app/sitemap.ts` convention.\n- Using relative URLs in the sitemap (e.g., `/blog/my-post`) — sitemaps require absolute URLs.\n- Installing `next-sitemap` when the prompt explicitly disallows it.\n- Setting `robots.txt` to disallow all crawlers (`Disallow: /`), which de-indexes the entire site.\n\n## Fix Prompt\n\n```txt title=\"Fix Prompt\"\nThe sitemap contains relative URLs or robots.txt disallows too much. Fix in order:\n1. In `src/app/sitemap.ts`, construct every URL as:\n   `const base = process.env.NEXT_PUBLIC_SITE_URL ?? 'https://example.com'; url: \\`\\${base}/blog/\\${post.slug}\\``\n2. In `src/app/robots.ts`, verify the rules object:\n   `{ userAgent: '*', allow: '/', disallow: ['/api/', '/admin/'] }`.\n3. Confirm `sitemap.ts` exports a default async function (not a named export).\nShow only the corrected diff.\n```"
}