指南

為 Next.js 加入 llms.txt

Next.js 讓這件事變得很簡單:這個檔案只需以純文字形式在 https://yoursite.com/llms.txt 提供即可。共有兩種俐落的做法。

做法一 — public/ 資料夾(最簡單)

public/ 中的任何檔案都會從網站根目錄提供。撰寫你的檔案(請參閱如何建立 llms.txt 檔案),並將它儲存為 public/llms.txt

my-app/
  public/
    llms.txt        # served at https://yoursite.com/llms.txt
  app/
  package.json

不需要程式碼,也不需要任何設定。Next.js 會自動以 Content-Type: text/plain 提供它。

做法二 — 路由處理常式(用於動態產生的內容)

如果你想從資料(你的 sitemap、CMS 或 MDX)建構這個檔案,可以新增一個路由。在 App Router 中,建立 app/llms.txt/route.ts

export function GET() {
  const body = "# My App\n\n> Short summary.\n\n## Docs\n- [Start](https://myapp.com/start): Get going\n";
  return new Response(body, {
    headers: { "Content-Type": "text/plain; charset=utf-8" },
  });
}

請留意這些重點

驗證它

部署完成後,將你的網域透過驗證工具執行一次 — 它會確認結構是否正確,以及每個連結的頁面是否確實能載入。

繼續閱讀

驗證你的 llms.txt →