NNext.js 中文网 GitHub

robots.txt

robots.txt 文件的 API 参考。

在 app 目录的根目录中添加或生成一个符合机器人排除标准(Robots Exclusion Standard)的 robots.txt 文件,告诉搜索引擎爬虫它们可以访问你站点上的哪些 URL。

静态 robots.txt#

app/robots.txt
User-Agent: *
Allow: /
Disallow: /private/

Sitemap: https://acme.com/sitemap.xml

生成 Robots 文件#

添加一个 robots.js 或 robots.ts 文件,该文件返回一个 Robots 对象。

小知识:robots.js 是一种特殊的 Route Handler,默认情况下会被缓存,除非它使用了 Request-time API 或动态配置选项。

app/robots.ts
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute.Robots {
  return {
    rules: {
      userAgent: '*',
      allow: '/',
      disallow: '/private/',
    },
    sitemap: 'https://acme.com/sitemap.xml',
  }
}
app/robots.js
export default function robots() {
  return {
    rules: {
      userAgent: '*',
      allow: '/',
      disallow: '/private/',
    },
    sitemap: 'https://acme.com/sitemap.xml',
  }
}

输出:

txt
User-Agent: *
Allow: /
Disallow: /private/

Sitemap: https://acme.com/sitemap.xml

自定义特定用户代理#

你可以通过向 rules 属性传入一个用户代理数组,来自定义各个搜索引擎爬虫抓取你站点的方式。例如:

app/robots.ts
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute.Robots {
  return {
    rules: [
      {
        userAgent: 'Googlebot',
        allow: ['/'],
        disallow: '/private/',
      },
      {
        userAgent: ['Applebot', 'Bingbot'],
        disallow: ['/'],
      },
    ],
    sitemap: 'https://acme.com/sitemap.xml',
  }
}
app/robots.js
export default function robots() {
  return {
    rules: [
      {
        userAgent: 'Googlebot',
        allow: ['/'],
        disallow: ['/private/'],
      },
      {
        userAgent: ['Applebot', 'Bingbot'],
        disallow: ['/'],
      },
    ],
    sitemap: 'https://acme.com/sitemap.xml',
  }
}

输出:

txt
User-Agent: Googlebot
Allow: /
Disallow: /private/

User-Agent: Applebot
Disallow: /

User-Agent: Bingbot
Disallow: /

Sitemap: https://acme.com/sitemap.xml

非标准指令#

一些搜索引擎支持不属于机器人排除标准(Robots Exclusion Standard)的指令,例如 Request-Rate(Seznam)或 Clean-param(Yandex)。通过规则上的 other 字段传递这些指令。键名保留其大小写,数组值会为每个条目生成一行,作用域限定在该规则的 User-Agent 块内。

app/robots.ts
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute.Robots {
  return {
    rules: [
      { userAgent: '*', allow: '/' },
      {
        userAgent: 'SeznamBot',
        allow: '/',
        other: {
          'Request-Rate': '10/1m',
        },
      },
    ],
  }
}
app/robots.js
export default function robots() {
  return {
    rules: [
      { userAgent: '*', allow: '/' },
      {
        userAgent: 'SeznamBot',
        allow: '/',
        other: {
          'Request-Rate': '10/1m',
        },
      },
    ],
  }
}

输出:

txt
User-Agent: *
Allow: /

User-Agent: SeznamBot
Allow: /
Request-Rate: 10/1m

小知识:other 中的值会原样传递。Next.js 不会校验指令名称或值,因此请参考目标搜索引擎的文档以了解确切语法。

Robots 对象#

tsx
type Robots = {
  rules:
    | {
        userAgent?: string | string[]
        allow?: string | string[]
        disallow?: string | string[]
        crawlDelay?: number
        other?: Record<string, string | number | Array<string | number>>
      }
    | Array<{
        userAgent: string | string[]
        allow?: string | string[]
        disallow?: string | string[]
        crawlDelay?: number
        other?: Record<string, string | number | Array<string | number>>
      }>
  sitemap?: string | string[]
  host?: string
}

版本历史#

版本变更内容
v16.3.0新增 other 字段,用于按用户代理添加非标准指令。
v13.3.0引入 robots。
Next.js 中文网