{
  "id": "add-sitemap-and-robots-txt",
  "type": "prompts",
  "category": "prompts",
  "locale": "de",
  "url": "/de/prompts/add-sitemap-and-robots-txt",
  "title": "Prompt zum Hinzufügen einer Sitemap und robots.txt",
  "description": "KI-Agent-Prompt zum Hinzufügen einer dynamisch generierten sitemap.xml und einer korrekten robots.txt zu einem Next.js- oder Astro-Projekt für eine bessere Suchindexierung.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "Astro",
    "TypeScript"
  ],
  "tags": [
    "seo",
    "nextjs",
    "astro",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "Geben Sie diesen Prompt an Ihren Agenten, um eine standardkonforme `sitemap.xml` und `robots.txt` hinzuzufügen – inklusive dynamischer Routen, Prioritätswerten und Lastmod-Daten, ohne einen kostenpflichtigen Drittanbieterdienst zu verwenden.\n\n## Hauptprompt\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## Implementierungshinweise\n\n- `sitemap.ts` und `robots.ts` im Verzeichnis `app/` sind Next.js 13.3+-Dateikonventionen; sie müssen eine Standardfunktion exportieren, die das typisierte Metadaten-Objekt zurückgibt – keine Response oder Zeichenkette.\n- Wenn `NEXT_PUBLIC_SITE_URL` zur Build-Zeit nicht definiert ist, enthält die Sitemap relative URLs, die gemäß dem Sitemap-Protokoll ungültig sind – überprüfen Sie dies mit einem Startup-Check.\n- `lastModified` sollte ein `Date`-Objekt sein, kein String; Next.js serialisiert es zu ISO 8601.\n\n## Erwartete Dateiänderungen\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## Akzeptanzkriterien\n\n- `GET /sitemap.xml` gibt gültiges XML mit allen statischen und dynamischen URLs als absolute URLs zurück.\n- `GET /robots.txt` enthält `Disallow: /api/` und `Sitemap: https://example.com/sitemap.xml`.\n- Hinzufügen einer neuen Blog-Post-MDX-Datei bewirkt, dass nach `bun run build` die URL in der Sitemap erscheint.\n- Die Sitemap wird unter https://www.xml-sitemaps.com/validate-xml-sitemap.html validiert.\n\n## Testbefehle\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## Häufige KI-Fehler\n\n- Erstellen einer statischen Datei `public/sitemap.xml` anstelle der dynamischen Konvention `src/app/sitemap.ts`.\n- Verwendung relativer URLs in der Sitemap (z.B. `/blog/my-post`) – Sitemaps erfordern absolute URLs.\n- Installieren von `next-sitemap`, obwohl der Prompt es ausdrücklich verbietet.\n- Setzen von `robots.txt` auf `Disallow: /`, was die gesamte Website deindiziert.\n\n## Reparatur-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```"
}