Remix 的 llms.txt

Remix 有两种方法:在 public/ 中放置一个零配置静态文件,或在 app/routes/llms[.]txt.ts 创建资源路由,返回纯文本 Response,也可选择根据你的内容生成。

最近更新:

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

Remix 会将 public/ 以静态方式放在站点根目录。 将文件放在 public/llms.txt,无需路由配置。

public/llms.txt, directory structure
# Remix static file approach
#
# Place your file at: public/llms.txt
# Remix (and the underlying Node/Cloudflare/Vercel adapter)
# serves everything in public/ at the root of your site.
#
# Project structure:
# your-remix-app/
# ├── public/
# │   └── llms.txt   ← add this
# ├── app/
# └── remix.config.js (or vite.config.ts)

此方法适用于所有 Remix 适配器: @remix-run/node, @remix-run/cloudflare, @remix-run/vercel,以及 @remix-run/netlify。静态文件优先于任何匹配的路由。

选项 2:资源路由

在以下位置创建资源路由: app/routes/llms[.]txt.ts。该 [.] 语法会转义文件名中的点,使 Remix 将其映射到 URL 路径 /llms.txt。导出一个 loader 函数,该函数返回一个 Response 并设置正确的 Content-Type.

app/routes/llms[.]txt.ts
// app/routes/llms[.]txt.ts
// Remix resource route, the [.] escapes the dot in the filename
// This file is served at exactly /llms.txt
import type { LoaderFunctionArgs } from '@remix-run/node';

export async function loader({ request }: LoaderFunctionArgs) {
  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.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog.

## Product

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

## Optional

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

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

若要从内容层动态生成,请在加载器中获取文档,并以编程方式构建 Markdown 链接列表:

app/routes/llms[.]txt.ts, dynamic
// app/routes/llms[.]txt.ts
// Generate from your content / MDX files
import type { LoaderFunctionArgs } from '@remix-run/node';
import { getAllDocs } from '~/models/docs.server';

export async function loader({ request }: LoaderFunctionArgs) {
  // Fetch your documentation index from a DB or file system
  const docs = await getAllDocs();

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

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

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

Vite 插件方式

如果您使用采用 Vite 的 Remix(Remix v2.7+ 的默认设置),可以生成 llms.txt ,在构建过程中通过 Vite 插件生成。生成的文件会放入构建输出目录,并作为静态资源提供,这兼具静态文件的简洁性和构建时访问内容的能力。

vite.config.ts, generate llms.txt at build
import { vitePlugin as remix } from '@remix-run/dev';
import { defineConfig } from 'vite';
import { writeFileSync } from 'node:fs';
import { resolve } from 'node:path';

// Simple Vite plugin to generate llms.txt at build time
function llmsTxtPlugin() {
  return {
    name: 'generate-llms-txt',
    buildStart() {
      const content = `# Your Site\n\n> Summary.\n\n## Documentation\n\n- [Docs](https://yoursite.com/docs): Guide.\n`;
      writeFileSync(resolve(__dirname, 'public/llms.txt'), content, 'utf-8');
    },
  };
}

export default defineConfig({
  plugins: [remix(), llmsTxtPlugin()],
});

验证

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

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

相关指南

来源