# Prompt zum Hinzufügen einer Sitemap und robots.txt

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

**Type:** Prompt  
**Tools:** Cursor, Claude Code, Codex, Windsurf  
**Stack:** Next.js, Astro, TypeScript  
**Difficulty:** easy  
**Updated:** 2026-06-08

---

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.

## Hauptprompt

```txt title="Main Prompt"
You are working in a Next.js 15 App Router project with TypeScript.
The site has static pages (/, /about, /pricing) and dynamic blog posts in `src/content/blog/`.

Task: add sitemap.xml and robots.txt using only Next.js built-in file conventions.

Requirements:
- Create `src/app/sitemap.ts` (NOT `.xml`) using the Next.js `MetadataRoute.Sitemap` return type.
  - Static URLs: `/`, `/about`, `/pricing` with `priority: 1.0` and `changeFrequency: 'monthly'`.
  - Dynamic URLs: read all MDX files from `src/content/blog/` using `getCollection('blog')` from
    `astro:content` — wait, this is Next.js, so use `fs` + `gray-matter` to read frontmatter.
  - For each post, return `{ url, lastModified, changeFrequency: 'weekly', priority: 0.8 }`.
  - `url` must be an absolute URL using the `NEXT_PUBLIC_SITE_URL` environment variable.
- Create `src/app/robots.ts` (NOT `.txt`) using the `MetadataRoute.Robots` return type.
  - Allow all crawlers for `/*`.
  - Disallow `/api/*` and `/admin/*` for all crawlers.
  - Set `sitemap` to the absolute sitemap URL.
- Add `NEXT_PUBLIC_SITE_URL=https://example.com` to `.env.example`.
- Do NOT install `next-sitemap` or any sitemap package.

Stop and list all files before writing code.
```

## Implementierungshinweise

- `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.
- 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.
- `lastModified` sollte ein `Date`-Objekt sein, kein String; Next.js serialisiert es zu ISO 8601.

## Erwartete Dateiänderungen

```txt
src/app/sitemap.ts              (new)
src/app/robots.ts               (new)
src/lib/blog.ts                 (new or edited — getAllPosts helper)
.env.example                    (edited)
```

## Akzeptanzkriterien

- `GET /sitemap.xml` gibt gültiges XML mit allen statischen und dynamischen URLs als absolute URLs zurück.
- `GET /robots.txt` enthält `Disallow: /api/` und `Sitemap: https://example.com/sitemap.xml`.
- Hinzufügen einer neuen Blog-Post-MDX-Datei bewirkt, dass nach `bun run build` die URL in der Sitemap erscheint.
- Die Sitemap wird unter https://www.xml-sitemaps.com/validate-xml-sitemap.html validiert.

## Testbefehle

```bash
bun run build && bun run start
curl http://localhost:3000/sitemap.xml | xmllint --format - | head -40
curl http://localhost:3000/robots.txt
# confirm /api/ is disallowed and sitemap URL is absolute
bun run typecheck
```

## Häufige KI-Fehler

- Erstellen einer statischen Datei `public/sitemap.xml` anstelle der dynamischen Konvention `src/app/sitemap.ts`.
- Verwendung relativer URLs in der Sitemap (z.B. `/blog/my-post`) – Sitemaps erfordern absolute URLs.
- Installieren von `next-sitemap`, obwohl der Prompt es ausdrücklich verbietet.
- Setzen von `robots.txt` auf `Disallow: /`, was die gesamte Website deindiziert.

## Reparatur-Prompt

```txt title="Fix Prompt"
The sitemap contains relative URLs or robots.txt disallows too much. Fix in order:
1. In `src/app/sitemap.ts`, construct every URL as:
   `const base = process.env.NEXT_PUBLIC_SITE_URL ?? 'https://example.com'; url: \`\${base}/blog/\${post.slug}\``
2. In `src/app/robots.ts`, verify the rules object:
   `{ userAgent: '*', allow: '/', disallow: ['/api/', '/admin/'] }`.
3. Confirm `sitemap.ts` exports a default async function (not a named export).
Show only the corrected diff.
```