跳到內容

robots.txt

app 目錄的根目錄中新增或產生符合 Robots 排除標準robots.txt 檔案,以告知搜尋引擎爬蟲它們可以在您的網站上存取哪些網址。

靜態 robots.txt

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

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

產生 Robots 檔案

新增一個 robots.jsrobots.ts 檔案,該檔案會回傳一個 Robots 物件

注意事項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