robots.txt
appディレクトリのルートにロボット排除標準に準拠したrobots.txtファイルを追加または生成して、検索エンジンクローラーがサイトのどのURLにアクセスできるかを指定します。
静的なrobots.txt
app/robots.txt
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xmlRobotsファイルの生成
Robotsオブジェクトを返すrobots.jsまたはrobots.tsファイルを追加します。
補足:
robots.jsは特殊なRoute Handlersで、デフォルトではキャッシュされます。ただしDynamic APIまたはdynamic configオプションを使用する場合を除きます。
app/robots.ts
TypeScript
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: '/private/',
},
sitemap: 'https://acme.com/sitemap.xml',
}
}出力:
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xml特定のユーザーエージェントのカスタマイズ
rulesプロパティにユーザーエージェントの配列を渡して、個別の検索エンジンボットがサイトをクロールする方法をカスタマイズできます。例えば:
app/robots.ts
TypeScript
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',
}
}出力:
User-Agent: Googlebot
Allow: /
Disallow: /private/
User-Agent: Applebot
Disallow: /
User-Agent: Bingbot
Disallow: /
Sitemap: https://acme.com/sitemap.xmlRobotsオブジェクト
type 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。 |