guides

Robots.txt

配置 robots.txt 以显式允许或阻止 AI 爬虫。

next-ai-ready 生成一个 robots.txt,显式声明你的 AI 爬虫策略。这对 AI 爬虫很重要,因为它们需要明确的信号来判断可以访问什么。

默认行为

默认情况下,npx next-ai-ready build 写入的 public/robots.txt 允许所有 AI 爬虫:

text
User-agent: *
Allow: /

# AI-specific rules
User-agent: GPTBot
Allow: /

User-agent: ClaudeBot
Allow: /

# ...(每个已知 AI 爬虫一个块)

# AI-readable content
# llms.txt: https://your-site.com/llms.txt
# llms-full.txt: https://your-site.com/llms-full.txt

每个已知 AI 爬虫都有显式的 Allow: / 块。注释指向你的 llms.txtllms-full.txt 端点。

配置

ai-ready.config.mjs 中控制 robots.txt 行为:

js
export default defineConfig({
  site: { /* ... */ },
  robots: {
    aiBots: "allow",       // "allow"(默认)或 "disallow"
    sitemap: true,         // true = 自动生成,string = 自定义 URL
    extra: [
      "Disallow: /admin/",
    ],
  },
})
字段类型默认值说明
aiBots`"allow" \"disallow"`"allow"AI 爬虫是否可以抓取你的站点。
sitemap`boolean \string`falsetrue = 自动生成 Sitemap: <baseUrl>/sitemap.xml。string = 直接使用。
extrastring[][]原始行,逐字追加到输出末尾。

禁用 AI 爬虫

阻止所有 AI 爬虫:

js
robots: {
  aiBots: "disallow",
}

这会为每个已知 AI 爬虫 user agent 生成 Disallow: /

静态 vs 动态

默认情况下,robots.txt 作为静态文件写入 public/。这是最简单、最经济的方式——适用于任何托管提供商。

如果需要动态 robots.txt(如按请求 A/B 测试),可以:

1. 在配置中设置 emit: { robots: false } 跳过静态生成。

2. 使用 Next.js 内置的 robots 支持创建 app/robots.ts(本文档站采用此方式)。

3. 在 app/robots.ts 中使用 aiRobots()next-ai-ready / @next-ai-ready/core)。

Doctor 认可 app/robots.ts + emit.robots: false,不会误报缺少 public/robots.txt

已知 AI 爬虫

框架检测以下 user agent:

  • GPTBot — OpenAI
  • OAI-SearchBot — OpenAI Search
  • ChatGPT-User — ChatGPT 浏览
  • PerplexityBot — Perplexity
  • ClaudeBot — Anthropic
  • anthropic-ai — Anthropic
  • Google-Extended — Google AI
  • CCBot — Common Crawl
  • Bytespider — ByteDance
  • Applebot-Extended — Apple