<!-- Generated from zh/llms-txt-vercel/index.html. The canonical document is the HTML page. -->

- [ 首页 ](/zh/) 
/
- [ 如何创建 ](/zh/how-to-create/) 
/
- Vercel 指南            
# 使用 Vercel 的 `llms.txt`

三种做法：在 public/ 中放静态文件（零配置），使用 Next.js App Router Route Handler，或者为非 Next.js 项目使用运行在 Node.js 运行时上的 Vercel Function。

最近更新: 2026年10月29日

本页内容

- [ 方法 1：public/ 中的静态文件 ](#approach-1)
- [ 方案 2，Next.js App Router 路由处理程序 ](#approach-2)
- [ 方法 3：Vercel Function（Node.js 运行时） ](#edge)
- [ Cache-Control 与 CDN 行为 ](#cache)
- [ 部署后验证 ](#verify)             
## 方案 1，静态文件位于 `public/`

这是最简单、最可靠的选项。Vercel 会提供你框架静态资源目录中的所有文件，并自动在其 CDN
上缓存静态文件。无需更改代码、无需配置，也没有运行时成本。
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 and caches static files on its CDN
# after the first request. 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 once the file has been requested at least once
```

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

示例

Vercel + GitHub 集成

Vercel 直接连接你的 GitHub 仓库。生产分支每次推送都会触发构建和部署。提交  public/llms.txt  就足够了：CDN 缓存键包含部署本身，因此更新后的文件会从新部署中提供，无需手动清除缓存。

## 方案 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）。在这种情况下，只有当
`Cache-Control`
标头包含 `s-maxage` 时，Vercel 才会在其 CDN 上缓存函数响应。

关于包含 Pages Router 和 `llms-full.txt` 的生成，请参阅
[Next.js 专属页面](/zh/llms-txt-nextjs/).

## 方法 3：Vercel Function（Node.js 运行时）

对于需要动态生成的非 Next.js 项目，请在 `api/` 目录中使用 Vercel Function，并配合一条
`vercel.json` 重写规则，将 `/llms.txt` 映射到该函数。下面的函数使用 Node.js
运行时文档中记载的 Web 标准签名：导出一个返回 `Response` 的
`GET(request: Request)`。
api/llms.ts, Node.js Vercel Function    复制     

```
// api/llms.ts, Vercel Function (Node.js runtime, the default)
// Files in /api are served under /api, so this one answers at /api/llms.
// The vercel.json rewrite below maps /llms.txt → /api/llms.

export function GET(request: Request) {
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',
// s-maxage is what lets Vercel's CDN cache a function response
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
// Optional: a cache tag so this response can be purged on demand
'Vercel-Cache-Tag': 'llms-txt',
},
});
}

```
vercel.json, rewrite rule    复制     

```
// vercel.json, rewrite /llms.txt to the function in api/llms.ts
// Only needed if you're not using Next.js App Router (which handles routing natively)

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

```

备注

为什么不使用 edge 运行时？

Vercel 的文档建议从 edge 运行时迁移到 Node.js，并指出从 Next.js 16.3 开始，不再支持设置
runtime = 'edge' 。本指南的早期版本在此处使用的是 edge 函数。

警告

与 /public/ 的路由冲突

如果静态  public/llms.txt  与 API 路由或 Vercel Function 同时存在于同一路径时，Vercel 会优先使用静态文件。每个项目只使用一种方法。

## Cache-Control 与 CDN 行为

`public/` 中的静态文件在首次请求后会自动缓存在 Vercel 的 CDN 上。函数响应（未使用
`force-static` 的 Route Handler、Vercel Function）只有在 `Cache-Control` 包含
`s-maxage`（可选搭配 `stale-while-revalidate`）时才会缓存在 CDN 上：

- `public, max-age=3600, s-maxage=86400`，浏览器中缓存一小时，CDN 上缓存一天。 
- `public, s-maxage=86400, stale-while-revalidate=604800`，采用积极的 CDN 缓存，
适合稳定内容。 
- `public, max-age=0, s-maxage=300`，由 CMS 驱动的内容使用 5 分钟 CDN 缓存。   
对于函数响应，不含 `s-maxage` 的标头（例如
`public, max-age=3600, stale-while-revalidate=86400`）不会被 Vercel 的 CDN
缓存。当你设置
`Cache-Control` 而没有设置 `CDN-Cache-Control` 标头时，Vercel 会在响应到达浏览器之前移除
`s-maxage` 和 `stale-while-revalidate`。

关于部署：CDN 缓存键包含唯一的部署 URL，因此新部署不会复用为上一个部署缓存的响应，更新后的
`llms.txt` 无需手动清除缓存即可提供。在同一个部署内，可以按缓存标签清除已缓存的函数响应（`Vercel-Cache-Tag` 标头，然后使用 `vercel cache invalidate --tag`、REST API 或控制台）。

## 部署后验证
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

# A new deployment has its own cache key, so it does not reuse
# responses cached for the previous deployment.
# To purge within the same deployment, invalidate by cache tag
# (requires the Vercel-Cache-Tag header set in the function):
vercel cache invalidate --tag llms-txt
```

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

&rarr; [完整创建指南](/zh/how-to-create/) &middot;
[Cloudflare Pages 指南](/zh/llms-txt-cloudflare/) &middot;
[Next.js 指南](/zh/llms-txt-nextjs/)

## 来源

- [ llmstxt.org，社区提案 ](https://llmstxt.org/)
- [ Vercel，Functions ](https://vercel.com/docs/functions)
- [ Vercel，Node.js 运行时 ](https://vercel.com/docs/functions/runtimes/node-js)
- [ Vercel，Edge 运行时 ](https://vercel.com/docs/functions/runtimes/edge)
- [ Vercel，CDN 缓存 ](https://vercel.com/docs/caching/cdn-cache)
- [ Vercel，清除 CDN 缓存 ](https://vercel.com/docs/caching/cdn-cache/purge)
- [ Vercel CLI，vercel cache ](https://vercel.com/docs/cli/cache)
- [ Vercel，/public/ 中的静态文件 ](https://vercel.com/docs/builds/configure-a-build#output-directory)
