Nuxt.js 的 llms.txt

Nuxt.js 的三种做法:在 public/ 中放静态文件(零配置),用 Nitro 服务器路由动态生成,或从 Nuxt Content 文档自动生成。

最近更新:

public/ 中的选项 1:静态文件

Nuxt 会将 public/ 目录中的内容直接复制到构建输出中,并通过 网站根路径提供。请将文件放在 public/llms.txt ,并可通过以下位置访问: /llms.txt 在每个部署目标上。

public/llms.txt, directory structure
# Nuxt static file approach
#
# Place your file at: public/llms.txt
# Nuxt copies everything in public/ directly to the build output.
#
# Project structure:
# your-nuxt-app/
# ├── public/
# │   └── llms.txt   ← add this
# ├── pages/
# └── nuxt.config.ts
#
# No configuration needed. Works with all deployment presets.

此方案无需配置,并且在所有 Nitro 预设中都以相同方式运行: node-server, cloudflare-pages, vercel, netlify,以及 静态.

选项 2:服务端 API 路由

server/routes/llms.txt.ts。Nuxt 的 Nitro 引擎会将以下目录中的文件映射到 URL 路径: server/routes/ 直接映射到网址路径,因此此文件准确地通过 /llms.txt ,无需额外的 路由配置。

server/routes/llms.txt.ts
// server/routes/llms.txt.ts
// Nuxt server route, served at /llms.txt
export default defineEventHandler(() => {
  const content = `# Your Site

> One-sentence description of what your site or product does.

## Documentation

- [Getting Started](https://yoursite.com/docs/start): Install and configure in minutes.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog with examples.

## Product

- [Overview](https://yoursite.com/product): Core features and capabilities.
- [Pricing](https://yoursite.com/pricing): Plans and billing details.

## Optional

- [Changelog](https://yoursite.com/changelog): Release history.
- [GitHub](https://github.com/your-org/your-repo): Source code.
`;

  setResponseHeader(event, 'Content-Type', 'text/plain; charset=utf-8');
  setResponseHeader(event, 'Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');
  return content;
});

使用 setResponseHeaderh3 库(随 Nuxt 捆绑)来设置正确的 Content-Type。添加一个 Cache-Control 通过 header 让 CDN 边缘缓存响应并降低源站负载。

选项 3:Nuxt Content 自动生成

如果你使用 Nuxt Content 管理文档,则可以查询内容集合并自动构建链接列表。这样可以确保 llms.txt 无需手动更新即可与文档保持同步。

server/routes/llms.txt.ts, Nuxt Content
// server/routes/llms.txt.ts
// Auto-generate llms.txt from Nuxt Content documents
import { serverQueryContent } from '#content/server';

export default defineEventHandler(async (event) => {
  // Query all docs; adjust collection name as needed
  const docs = await serverQueryContent(event, '/docs')
    .only(['title', 'description', '_path'])
    .find();

  const links = docs
    .map((doc) => `- [${doc.title}](https://yoursite.com${doc._path}/): ${doc.description ?? ''}`)
    .join('\n');

  const body = [
    '# Your Site',
    '',
    '> One-sentence description of your project.',
    '',
    '## Documentation',
    '',
    links,
  ].join('\n');

  setResponseHeader(event, 'Content-Type', 'text/plain; charset=utf-8');
  return body;
});

调整查询路径(/docs)和字段名称以匹配你的内容结构。运行 验证器 ,以发现任何格式回归。

Nitro 和部署目标

  • node-server,服务器路由以 Node.js HTTP 处理程序的形式运行。为其添加反向代理(nginx/Caddy)缓存规则: /llms.txt 以避免每次请求都命中 Node。
  • cloudflare-pages,位于 public/ 通过 Cloudflare 的全球 CDN 提供。服务器路由会成为 Pages Functions。请参阅 Cloudflare 指南 的缓存标头。
  • vercel,静态文件会自动在边缘缓存。根据配置,服务器路由会成为 Vercel Serverless Function 或 Edge Function。
  • 静态 (完全预渲染),请使用 public/ 静态文件方案;纯静态输出不支持服务器路由。

验证

部署后,确认文件已正确提供:

Verification
curl -I https://yoursite.com/llms.txt
# Expected:
# HTTP/2 200
# content-type: text/plain; charset=utf-8

curl https://yoursite.com/llms.txt | head -5
# Should print the first lines of your file

相关指南

来源