指南

为 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 →