Prompt to Add a Sitemap and robots.txt
AI agent prompt to add a dynamically generated sitemap.xml and a correct robots.txt to a Next.js or Astro project for better search indexing.
CursorClaude CodeCodexWindsurf Next.jsAstroTypeScript
Give this prompt to your agent to add a standards-compliant sitemap.xml and
robots.txt — handling dynamic routes, priority values, and lastmod dates
without using a paid third-party service.
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.Implementation Notes
sitemap.tsandrobots.tsin theapp/directory are Next.js 13.3+ file conventions; they must export a default function that returns the typed metadata object — not a Response or string.- If
NEXT_PUBLIC_SITE_URLis undefined at build time, the sitemap will contain relative URLs which are invalid per the Sitemap protocol — validate with a startup check. lastModifiedshould be aDateobject, not a string; Next.js serializes it to ISO 8601.
Expected File Changes
src/app/sitemap.ts (new)src/app/robots.ts (new)src/lib/blog.ts (new or edited — getAllPosts helper).env.example (edited)Acceptance Criteria
GET /sitemap.xmlreturns valid XML with all static and dynamic URLs as absolute URLs.GET /robots.txtincludesDisallow: /api/andSitemap: https://example.com/sitemap.xml.- Adding a new blog post MDX file causes its URL to appear in the sitemap after
bun run build. - The sitemap validates at https://www.xml-sitemaps.com/validate-xml-sitemap.html.
Test Commands
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 typecheckCommon AI Mistakes
- Creating a static
public/sitemap.xmlfile instead of the dynamicsrc/app/sitemap.tsconvention. - Using relative URLs in the sitemap (e.g.,
/blog/my-post) — sitemaps require absolute URLs. - Installing
next-sitemapwhen the prompt explicitly disallows it. - Setting
robots.txtto disallow all crawlers (Disallow: /), which de-indexes the entire site.
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.