Prompt para agregar un Sitemap y robots.txt
Prompt de agente de IA para agregar un sitemap.xml generado dinámicamente y un robots.txt correcto a un proyecto de Next.js o Astro para mejorar la indexación en buscadores.
CursorClaude CodeCodexWindsurf Next.jsAstroTypeScript
Entrega este prompt a tu agente para agregar un sitemap.xml y un robots.txt que cumplan con los estándares — manejando rutas dinámicas, valores de prioridad y fechas lastmod sin usar un servicio de terceros de pago.
Prompt Principal
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.Notas de Implementación
sitemap.tsyrobots.tsen el directorioapp/son convenciones de archivo de Next.js 13.3+; deben exportar una función por defecto que devuelva el objeto de metadatos tipado — no un Response o string.- Si
NEXT_PUBLIC_SITE_URLno está definida en tiempo de compilación, el sitemap contendrá URLs relativas que son inválidas según el protocolo Sitemap — valida con una verificación al inicio. lastModifieddebe ser un objetoDate, no un string; Next.js lo serializa a ISO 8601.
Cambios Esperados en Archivos
src/app/sitemap.ts (new)src/app/robots.ts (new)src/lib/blog.ts (new or edited — getAllPosts helper).env.example (edited)Criterios de Aceptación
GET /sitemap.xmldevuelve XML válido con todas las URLs estáticas y dinámicas como URLs absolutas.GET /robots.txtincluyeDisallow: /api/ySitemap: https://example.com/sitemap.xml.- Agregar un nuevo archivo MDX de publicación de blog hace que su URL aparezca en el sitemap después de
bun run build. - El sitemap se valida en https://www.xml-sitemaps.com/validate-xml-sitemap.html.
Comandos de Prueba
bun run build && bun run startcurl http://localhost:3000/sitemap.xml | xmllint --format - | head -40curl http://localhost:3000/robots.txt# confirm /api/ is disallowed and sitemap URL is absolutebun run typecheckErrores Comunes de la IA
- Crear un archivo estático
public/sitemap.xmlen lugar de la convención dinámicasrc/app/sitemap.ts. - Usar URLs relativas en el sitemap (ej.,
/blog/my-post) — los sitemaps requieren URLs absolutas. - Instalar
next-sitemapcuando el prompt explícitamente lo prohíbe. - Configurar
robots.txtpara deshabilitar todos los rastreadores (Disallow: /), lo que desindexa todo el sitio.
Prompt de Corrección
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.