适用于 SvelteKit 的 llms.txt

三种做法:在 static/ 中放静态文件(零配置),用 +server.ts 端点动态生成,或使用 import.meta.glob 进行内容驱动的生成。

最近更新:

选项 1:静态文件(推荐)

SvelteKit 会将 static/ 目录中的内容直接复制到构建输出中。 请将文件放在 static/llms.txt ,它将提供于 /llms.txt 适用于每个适配器,无需更改配置。

static/llms.txt, directory structure
# SvelteKit, zero-config static approach
#
# Place your file at: static/llms.txt
# SvelteKit copies everything in static/ directly into the build output.
# Your file will be served at /llms.txt on any adapter.
#
# Project structure:
# your-sveltekit-app/
# ├── static/
# │   └── llms.txt   ← add this
# ├── src/
# └── svelte.config.js
#
# No config changes needed. Deploy as normal.

这种方法适用于 adapter-static, adapter-cloudflare, adapter-vercel, adapter-node以及其他所有官方适配器。文件会使用适配器默认的静态文件响应头提供。

选项 2:+server.ts 端点

在以下位置创建服务器路由: src/routes/llms.txt/+server.ts 导出一个 GET 处理器。SvelteKit 会在路径 /llms.txt.

src/routes/llms.txt/+server.ts
// src/routes/llms.txt/+server.ts
// SvelteKit server endpoint, dynamic generation
import type { RequestHandler } from './$types';

export const GET: RequestHandler = () => {
  const body = `# Your Site

> One-sentence description of what your site is.

## Product

- [Overview](https://yoursite.com/product): core capabilities.
- [Pricing](https://yoursite.com/pricing): plans and limits.

## Documentation

- [Getting started](https://yoursite.com/docs): install, first steps.
- [API reference](https://yoursite.com/api): full endpoint catalog.

## Optional

- [Changelog](https://yoursite.com/changelog): version history.
`;

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

设置合适的 Cache-Control 标头中,这样边缘 CDN 就会缓存响应;它很少变化 ,也不需要在每次请求时重新传送。

src/routes/llms.txt/+server.ts, with prerender
// src/routes/llms.txt/+server.ts
// Add this line to pre-render the endpoint at build time (adapter-static / SSG)
export const prerender = true;

import type { RequestHandler } from './$types';

export const GET: RequestHandler = () => {
  const body = `# Your Site\n\n> Summary.\n\n## Docs\n\n- [Guide](https://yoursite.com/docs)`;
  return new Response(body, {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
};

选项 3:从内容生成

如果你在 src/content/,你可以生成 llms.txt 在构建时自动生成,方法是使用 import.meta.glob。下面的示例读取所有 Markdown 文件,并根据其 frontmatter 构建链接列表。

src/routes/llms.txt/+server.ts, from content
// src/routes/llms.txt/+server.ts
// Generate llms.txt from your Markdown content files
import type { RequestHandler } from './$types';

// Import all Markdown files in src/content/docs/
const docs = import.meta.glob('/src/content/docs/**/*.md', { eager: true }) as Record<
  string,
  { metadata?: { title?: string; summary?: string; slug?: string } }
>;

export const prerender = true;

export const GET: RequestHandler = () => {
  const lines = Object.values(docs)
    .filter((m) => m.metadata?.title && m.metadata?.slug)
    .map((m) => `- [${m.metadata!.title}](https://yoursite.com/docs/${m.metadata!.slug}/): ${m.metadata!.summary ?? ''}`);

  const body = [
    '# Your Site',
    '',
    '> One-sentence summary.',
    '',
    '## Documentation',
    '',
    ...lines,
  ].join('\n');

  return new Response(body, {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
};

调整 glob 模式和元数据字段,使其匹配项目的内容结构。运行 验证器 集成到 CI 中,以便在内容迁移后发现损坏的输出。

适配器说明

  • adapter-cloudflare,位于 static/ 由 Cloudflare Pages 的全球 CDN 直接提供并自动缓存。服务器路由会成为 Cloudflare Pages Functions。请参阅 Cloudflare Pages 指南 获取缓存响应头建议和清除命令。
  • adapter-vercel,静态文件通过 Vercel 的 Edge Network 提供。服务器路由作为 Vercel Functions 运行(使用 export const config = { runtime: 'edge' })。参见 Vercel 指南 用于 vercel.json 重写选项。
  • adapter-static,所有路由都必须支持预渲染。添加 export const prerender = true; 添加到你的 +server.ts,或者改用静态文件方案。
  • adapter-node服务器路由以 Node.js HTTP 服务器运行。 +server.ts 方法很理想;请添加积极的 Cache-Control 标头,使反向代理(nginx、Caddy)可以缓存响应。

验证

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

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

然后将 URL 粘贴到 验证器 以进行完整的规范合规性检查。

相关指南

来源