{
  "id": "add-sitemap-and-robots-txt",
  "type": "prompts",
  "category": "prompts",
  "locale": "zh",
  "url": "/zh/prompts/add-sitemap-and-robots-txt",
  "title": "添加站点地图和robots.txt的提示词",
  "description": "AI代理提示词：为Next.js或Astro项目动态生成sitemap.xml和正确的robots.txt以改善搜索引擎索引。",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "Astro",
    "TypeScript"
  ],
  "tags": [
    "seo",
    "nextjs",
    "astro",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "将此提示词提供给您的代理，以添加符合标准的`sitemap.xml`和`robots.txt`——处理动态路由、优先级值和最后修改日期，无需使用付费第三方服务。\n\n## 主提示词\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## 实现注意事项\n\n- `app/`目录下的`sitemap.ts`和`robots.ts`是Next.js 13.3+的文件约定；它们必须导出一个默认函数，返回类型化的元数据对象——而不是Response或字符串。\n- 如果构建时`NEXT_PUBLIC_SITE_URL`未定义，站点地图将包含相对URL，根据站点地图协议这是无效的——请通过启动检查进行验证。\n- `lastModified`应为`Date`对象，而非字符串；Next.js会将其序列化为ISO 8601格式。\n\n## 预期文件更改\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## 验收标准\n\n- `GET /sitemap.xml` 返回有效的XML，所有静态和动态URL均为绝对URL。\n- `GET /robots.txt` 包含 `Disallow: /api/` 和 `Sitemap: https://example.com/sitemap.xml`。\n- 添加新的博客文章MDX文件后，执行 `bun run build` 其URL将出现在站点地图中。\n- 站点地图在 https://www.xml-sitemaps.com/validate-xml-sitemap.html 验证通过。\n\n## 测试命令\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## 常见的AI错误\n\n- 创建静态的`public/sitemap.xml`文件，而不是使用动态的`src/app/sitemap.ts`约定。\n- 在站点地图中使用相对URL（例如`/blog/my-post`）——站点地图需要绝对URL。\n- 在提示词明确禁止的情况下安装`next-sitemap`。\n- 将`robots.txt`设置为禁止所有爬虫（`Disallow: /`），这会导致整个网站被取消索引。\n\n## 修复提示词\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```"
}