SvelteKit 向け llms.txt

3つの方法があります。static/内の静的ファイル(設定不要)、動的生成用の+server.tsエンドポイント、またはimport.meta.globを使うコンテンツ駆動型ジェネレーターです。

最終更新:

オプション 1:静的ファイル(推奨)

SvelteKitはすべてを static/ ディレクトリをビルド出力に直接配置します。 ファイルを以下の場所に配置してください。 static/llms.txt そして、それは以下で提供される予定です。 /llms.txt on すべてのアダプタにおいて、設定の変更は不要です。

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 ファイルを読み込み、フロントマターからリンクリストを作成します。

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-cloudflarestatic/ これらは、自動キャッシュ機能を備えた Cloudflare Pages のグローバル CDN によって直接配信されます。サーバーのルートは Cloudflare Pages Functions となります。詳細は Cloudflare Pages ガイド 、 キャッシュヘッダーの推奨事項とパージコマンドについては、
  • adapter-vercel、静的ファイルはVercelのEdge Networkから配信されます。サーバールートはVercel Functions(または export const config = { runtime: 'edge' })。詳しくは Vercelガイド 向けの vercel.json リライトのオプションについては [Vercel ガイド]を参照します。
  • 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 を バリデーター 完全な仕様準拠チェック用に

関連ガイド

ソース