在 app 目录的根目录中添加或生成一个符合 Robots 排除标准的 robots.txt 文件,以告知搜索引擎爬虫可以访问您网站上的哪些 URL。
robots.txtUser-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xml添加一个 robots.js 或 robots.ts 文件,该文件返回一个 Robots 对象。
须知:
robots.js是一个特殊的路由处理器(Route Handlers),默认情况下会被缓存,除非它使用了 动态 API 或 动态配置 选项。
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: '/private/',
},
sitemap: 'https://acme.com/sitemap.xml',
}
}export default function robots() {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: '/private/',
},
sitemap: 'https://acme.com/sitemap.xml',
}
}Output:
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xml您可以通过将用户代理数组传递给 rules 属性来自定义各个搜索引擎机器人如何抓取您的网站。例如:
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',
}
}export default function robots() {
return {
rules: [
{
userAgent: 'Googlebot',
allow: ['/'],
disallow: ['/private/'],
},
{
userAgent: ['Applebot', 'Bingbot'],
disallow: ['/'],
},
],
sitemap: 'https://acme.com/sitemap.xml',
}
}Output:
User-Agent: Googlebot
Allow: /
Disallow: /private/
User-Agent: Applebot
Disallow: /
User-Agent: Bingbot
Disallow: /
Sitemap: https://acme.com/sitemap.xmltype Robots = {
rules:
| {
userAgent?: string | string[]
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
}
| Array<{
userAgent: string | string[]
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
}>
sitemap?: string | string[]
host?: string
}| 版本 | 更改 |
|---|---|
v13.3.0 | 引入了 robots。 |