{
  "id": "add-sitemap-and-robots-txt",
  "type": "prompts",
  "category": "prompts",
  "locale": "fr",
  "url": "/fr/prompts/add-sitemap-and-robots-txt",
  "title": "Invite pour ajouter un sitemap et un robots.txt",
  "description": "Invite pour agent IA afin d'ajouter un sitemap.xml généré dynamiquement et un robots.txt correct à un projet Next.js ou Astro pour un meilleur référencement.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "Astro",
    "TypeScript"
  ],
  "tags": [
    "seo",
    "nextjs",
    "astro",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "Donnez cette invite à votre agent pour ajouter un `sitemap.xml` et un `robots.txt` conformes aux normes — gérant les routes dynamiques, les valeurs de priorité et les dates de dernière modification sans utiliser de service tiers payant.\n\n## Invite principale\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## Notes d'implémentation\n\n- `sitemap.ts` et `robots.ts` dans le répertoire `app/` sont des conventions de fichiers Next.js 13.3+ ; ils doivent exporter une fonction par défaut qui retourne l'objet metadata typé — pas un objet Response ou une chaîne.\n- Si `NEXT_PUBLIC_SITE_URL` n'est pas défini au moment de la construction, le sitemap contiendra des URLs relatives qui sont invalides selon le protocole Sitemap — validez avec une vérification au démarrage.\n- `lastModified` doit être un objet `Date`, pas une chaîne ; Next.js le sérialise en ISO 8601.\n\n## Modifications de fichiers attendues\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## Critères d'acceptation\n\n- `GET /sitemap.xml` retourne du XML valide avec toutes les URLs statiques et dynamiques sous forme d'URLs absolues.\n- `GET /robots.txt` inclut `Disallow: /api/` et `Sitemap: https://example.com/sitemap.xml`.\n- L'ajout d'un nouveau fichier MDX d'article de blog fait apparaître son URL dans le sitemap après `bun run build`.\n- Le sitemap est validé sur https://www.xml-sitemaps.com/validate-xml-sitemap.html.\n\n## Commandes de test\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## Erreurs courantes de l'IA\n\n- Créer un fichier statique `public/sitemap.xml` au lieu de la convention dynamique `src/app/sitemap.ts`.\n- Utiliser des URLs relatives dans le sitemap (par ex. `/blog/my-post`) — les sitemaps nécessitent des URLs absolues.\n- Installer `next-sitemap` alors que l'invite l'interdit explicitement.\n- Configurer `robots.txt` pour interdire tous les robots (`Disallow: /`), ce qui désindexe tout le site.\n\n## Invite de correction\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```"
}