robots.txt
検索エンジンクローラーにサイト上のどのURLにアクセスできるかを伝えるために、app
ディレクトリの**ルート**に、ロボット除外標準に一致するrobots.txt
ファイルを追加または生成します。
静的 robots.txt
app/robots.txt
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xml
Robotsファイルの生成
Robots
オブジェクトを返すrobots.js
またはrobots.ts
ファイルを追加します。
**知っておくと便利**:
robots.js
は、動的 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',
}
}
出力
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',
}
}
出力
User-Agent: Googlebot
Allow: /
Disallow: /private/
User-Agent: Applebot
Disallow: /
User-Agent: Bingbot
Disallow: /
Sitemap: https://acme.com/sitemap.xml
Robotsオブジェクト
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 が導入されました。 |
お役に立ちましたか?