使用 Vercel 的 llms.txt

三种做法:在 public/ 中放静态文件(零配置),使用 Next.js App Router Route Handler,或者使用 Vercel Edge Function 以获得最大灵活性。

最近更新:

方案 1,静态文件位于 public/

这是最简单、最可靠的选项。Vercel 会直接通过其全球边缘网络提供你框架静态资源目录中的所有文件。无需更改代码、无需配置,也没有运行时成本。

static file, framework directory map
# Place llms.txt in your project's static assets directory.
#
# Framework    → file location
# Next.js      → public/llms.txt
# Astro        → public/llms.txt
# SvelteKit    → static/llms.txt
# Nuxt         → public/llms.txt
# Remix        → public/llms.txt
# Hugo         → static/llms.txt

# Vercel serves it at /llms.txt from their global Edge Network.
# No config changes needed. After deploy, verify:
curl -I https://your-domain.com/llms.txt
# Expected: HTTP/2 200 | Content-Type: text/plain | x-vercel-cache: HIT

何时使用这个: 你的 llms.txt 内容相对稳定,并且你通过手动方式或构建脚本进行更新。这是绝大多数网站合适的默认方案。

方案 2,Next.js App Router 路由处理程序

如果你在 Vercel 上使用 Next.js,最简洁的动态方式是在 App Router 中使用 Route Handler,放在 app/llms.txt/route.ts。使用 export const dynamic = 'force-static',Vercel 会在构建时预渲染它并在边缘缓存输出,效果上与提供静态文件相同,但内容根据你的数据生成。

app/llms.txt/route.ts
// app/llms.txt/route.ts  (Next.js App Router)
// Vercel detects this as a static export when dynamic = 'force-static'
// and caches the output at the edge on first request.

export const dynamic = 'force-static';

export async function GET() {
  const content = [
    '# My Site',
    '',
    '> One-sentence description of what this site is about.',
    '',
    '## Documentation',
    '',
    '- [Getting started](https://yoursite.com/docs/getting-started/): first steps.',
    '- [API reference](https://yoursite.com/docs/api/): full endpoint catalog.',
    '',
    '## Optional',
    '',
    '- [Changelog](https://yoursite.com/changelog/): version history.',
  ].join('\n');

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

移除 force-static ,如果你需要在每次请求时获取真正的动态内容(例如来自实时 CMS)。在这种情况下,Vercel 仍会根据你的 Cache-Control 标头。

关于包含 Pages Router 和 llms-full.txt 的生成,请参阅 Next.js 专属页面.

方法 3:Vercel Edge Function

对于需要动态生成的非 Next.js 项目,请将 Vercel Edge Function 与 vercel.json 重写规则,将 /llms.txt 到该函数:

api/llms.ts, Edge Function
// api/llms.txt.ts, Vercel Edge Function
// Place this file in the /api directory.
// Vercel routes requests to /api/llms.txt by default,
// so use a vercel.json rewrite to map /llms.txt → /api/llms.txt

import type { VercelRequest, VercelResponse } from '@vercel/node';

export const config = {
  runtime: 'edge', // Runs on Vercel's Edge Network
};

export default function handler(req: Request): Response {
  const content = [
    '# My Site',
    '',
    '> One-sentence description.',
    '',
    '## Core pages',
    '',
    '- [Getting started](https://yoursite.com/docs/getting-started/): first steps.',
  ].join('\n');

  return new Response(content, {
    headers: {
      'Content-Type': 'text/plain; charset=utf-8',
      'Cache-Control': 'public, max-age=3600, s-maxage=86400',
    },
  });
}
vercel.json, rewrite rule
// vercel.json, rewrite /llms.txt to your Edge Function or API Route
// Only needed if you're not using Next.js App Router (which handles routing natively)

{
  "rewrites": [
    { "source": "/llms.txt", "destination": "/api/llms" }
  ]
}

Edge Functions 在 Vercel 的全球 Edge Network 上运行,冷启动时间接近于零。它们非常适合生成 llms.txt 可以从 KV 存储或外部 API 提供,而无需完整服务器。

Cache-Control 与边缘网络行为

对于 public/,Vercel 会自动设置缓存标头。对于 Route Handler 和 Edge Function,请明确设置 Cache-Control 明确地:

  • public, max-age=3600, stale-while-revalidate=86400,建议用于 手动精选的文件。
  • public, s-maxage=86400, stale-while-revalidate=604800,采用积极的边缘缓存, 适合稳定内容。
  • public, max-age=0, s-maxage=300由 CMS 驱动的内容使用 5 分钟边缘缓存。

Vercel 会在每次新部署时自动清除 CDN 缓存,因此推送更新后的 llms.txt.

部署后验证

verify
# Check headers, look for Content-Type and x-vercel-cache
curl -I https://yoursite.com/llms.txt

# Inspect content
curl https://yoursite.com/llms.txt

# Force-revalidate after a new deploy (Vercel auto-purges on deploy)
# For manual purge via Vercel REST API:
curl -X POST "https://api.vercel.com/v1/projects/{PROJECT_ID}/purge" \
  -H "Authorization: Bearer {VERCEL_TOKEN}"

验证响应头后,将在线 URL 粘贴到 验证器 以确认符合规范:恰好一个 H1、有效的链接语法 (- [title](https://...)),所有 URL 均为绝对地址,且没有空分节。

完整创建指南 · Cloudflare Pages 指南 · Next.js 指南

来源