{
  "id": "add-a-sitemap-and-robots",
  "type": "playbooks",
  "category": "playbooks",
  "locale": "en",
  "url": "/playbooks/add-a-sitemap-and-robots",
  "title": "Prompt-to-PR: Add a Sitemap and robots.txt",
  "description": "SOP for adding a dynamic XML sitemap and robots.txt to a Next.js or Astro project — correct lastmod, priority, and crawl rules for production SEO.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "Astro",
    "TypeScript"
  ],
  "tags": [
    "seo",
    "nextjs",
    "astro",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "Sitemaps and `robots.txt` are the first SEO primitives an agent touches, and they are frequently wrong — wrong `lastmod` format, missing `Sitemap:` directive in robots, or blocked pages inadvertently included. This playbook gets them right.\n\n## 1. Requirement\n\nProduce an XML sitemap covering all public routes (static + dynamic content from the database) and a `robots.txt` that blocks admin/API paths and references the sitemap. Works for both Next.js App Router and Astro; choose the correct approach for your framework.\n\n## 2. First Prompt\n\n```txt title=\"First Prompt\"\nAdd a sitemap.xml and robots.txt to this project. Use the correct approach\nfor the framework detected below.\n\n### If Next.js 14+:\n1. Create `src/app/sitemap.ts` using the Next.js `MetadataRoute.Sitemap`\n   return type. Include:\n   - All static routes: /, /pricing, /blog, /about (hardcoded is fine).\n   - All dynamic blog posts: fetch slugs from the DB using the existing\n     query helper, return lastModified from the post's updatedAt field.\n   - Use `process.env.NEXT_PUBLIC_APP_URL` as the base URL.\n   - Correct W3C datetime format for lastModified (ISO 8601).\n2. Create `src/app/robots.ts` using MetadataRoute.Robots.\n   - Allow: all routes.\n   - Disallow: /admin, /api, /dashboard.\n   - Add `sitemap: process.env.NEXT_PUBLIC_APP_URL + \"/sitemap.xml\"`.\n\n### If Astro:\n1. Add `@astrojs/sitemap` integration. In astro.config.ts, add\n   `sitemap({ filter: (page) => !page.includes(\"/admin\") })` and set\n   `site: process.env.SITE_URL`.\n2. Create `public/robots.txt`:\n   User-agent: *\n   Disallow: /admin\n   Disallow: /api\n   Sitemap: <SITE_URL>/sitemap-index.xml\n\nDo not create a custom sitemap endpoint if the integration handles it.\nDo not block / or any public content pages.\n```\n\n## 3. Expected File Changes\n\n```txt\n### Next.js\nsrc/app/sitemap.ts             (new — dynamic MetadataRoute.Sitemap)\nsrc/app/robots.ts              (new — MetadataRoute.Robots)\n\n### Astro\nastro.config.ts                (add sitemap integration + filter)\npublic/robots.txt              (new)\n.env.example                   (SITE_URL added if missing)\n```\n\n## 4. Review Checklist\n\n- Base URL comes from an env var — not hardcoded as `http://localhost:3000`.\n- `lastModified` is a JavaScript `Date` object (Next.js converts to ISO 8601) or already a valid ISO string — not `\"undefined\"` or missing.\n- `/admin`, `/api`, and `/dashboard` are in the Disallow list.\n- The `Sitemap:` directive in `robots.txt` uses an absolute URL.\n- Dynamic routes (blog posts) are included via a DB query, not just static routes.\n- The sitemap does not include 404, redirect, or noindex pages.\n- `bun run build` then `curl /sitemap.xml` returns valid XML (check with `xmllint`).\n\n## 5. Test Commands\n\n```bash\nbun run build && bun run start\n# or for Astro:\nbun run build && bun run preview\n\n# Validate sitemap XML\ncurl -s http://localhost:3000/sitemap.xml | xmllint --format - | head -40\n\n# Confirm robots.txt\ncurl http://localhost:3000/robots.txt\n\n# Confirm admin is disallowed and sitemap directive is present\ngrep -E \"Disallow|Sitemap\" <(curl -s http://localhost:3000/robots.txt)\n\n# Google Rich Results / URL Inspection simulation\ncurl -A \"Googlebot\" http://localhost:3000/sitemap.xml -I\n```\n\n## 6. Common Failures\n\n- **`lastModified` is `\"undefined\"`** — post's `updatedAt` field is null. Guard: `lastModified: post.updatedAt ?? post.createdAt ?? new Date()`.\n- **Sitemap returns 404** — `src/app/sitemap.ts` is missing or placed outside the `app` directory.\n- **All routes disallowed** — agent adds `Disallow: /` by mistake. Confirm only admin/API paths are blocked.\n- **`Sitemap:` directive has relative URL** — Google ignores it. Must be absolute: `https://example.com/sitemap.xml`.\n- **Static sitemap only** — agent hardcodes blog slugs instead of querying the DB. Confirm the sitemap function is `async` and fetches real data.\n\n## 7. Fix Prompt\n\n```txt title=\"Fix Prompt\"\nThe sitemap.xml at /sitemap.xml includes every blog post with\nlastModified \"undefined\" (rendered as the string).\n\nFix in src/app/sitemap.ts:\n  const posts = await getBlogPosts();\n  return posts.map((post) => ({\n    url: `${BASE_URL}/blog/${post.slug}`,\n    lastModified: post.updatedAt ?? post.createdAt ?? new Date(),\n    changeFrequency: \"weekly\",\n    priority: 0.8,\n  }));\n\nEnsure getBlogPosts() returns rows that include updatedAt and createdAt.\n```\n\n## 8. PR Description\n\n```md title=\"PR description\"\n## SEO: Add dynamic sitemap.xml and robots.txt\n\n**Next.js**: `src/app/sitemap.ts` + `src/app/robots.ts` using built-in\n`MetadataRoute` types. Sitemap includes static routes + all published blog\nposts with correct `lastModified` timestamps from the DB.\n\n**robots.txt** disallows `/admin`, `/api`, `/dashboard`; includes absolute\n`Sitemap:` directive pointing to the generated `/sitemap.xml`.\n\nBase URL read from `NEXT_PUBLIC_APP_URL` — no localhost URLs in production.\n```"
}