Next.jsでのllms.txt

Next.js プロジェクトに llms.txt ファイルを数分で追加できます。静的アセットとして追加する方法と、動的な Route Handler として追加する方法があります。

最終更新:

2つのアプローチ

Next.jsでは、 llms.txt/llms.txt:

  1. 次の場所にある静的ファイル: /public/。最も簡単な方式です。ファイルを 手動で作成するか、ビルド時に生成します。App RouterとPages Routerの両方で動作し、 ランタイムのオーバーヘッドはありません。
  2. App Router の Route Handler、次の場所に置くTypeScriptファイルです: app/llms.txt/route.ts データソース(MDXファイル、CMS、データベース)からコンテンツを動的に生成します。コンテンツが頻繁に変わる場合に最適です。

ほとんどのサイトでは、 /public/ が適切な選択です。サイトのデータからビルド時またはリクエスト時にコンテンツを自動生成したい場合にのみ、Route Handlerを使用してください。

方法1:/public/ 内の静的ファイル

Next.jsは /public/ ディレクトリ内の各ファイルをドメインのルートで配信します。次の場所にあるファイル /public/llms.txthttps://yourdomain.com/llms.txt 追加設定は不要です。

  1. ファイルを作成します: touch public/llms.txt
  2. llms.txt コンテンツを作成します(詳しくは 作成方法ガイドジェネレーター 正しい形式については、
# Your Site Name

> One-sentence description of your site for LLM context.

## Core pages

- [Page title](https://yourdomain.com/page/): brief description.
- [Another page](https://yourdomain.com/other/): brief description.

## Optional

- [About](https://yourdomain.com/about/): who maintains this site.
  1. ファイルをcommitしてデプロイします。これで完了です。

方法 2: App Router のルートハンドラー

llms.txt プログラムで、たとえばコンテンツディレクトリ内のすべての MDX ファイルを 読み取って生成したい場合は、Route Handler を使用します。

ファイル app/llms.txt/route.ts:

import { NextResponse } from 'next/server';

export const dynamic = 'force-static'; // generate at build time

export async function GET() {
  // Build your content. Here: hardcoded; in practice: read from MDX, CMS, etc.
  const content = `# Your Site Name

> Description of your site.

## Core pages

- [Getting started](https://yourdomain.com/docs/getting-started/): installation and first steps.
- [API reference](https://yourdomain.com/docs/api/): full endpoint reference.
`;

  return new NextResponse(content, {
    headers: {
      'Content-Type': 'text/plain; charset=utf-8',
      // Optional: cache for 1 hour in production
      'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
    },
  });
}

dynamic = 'force-static' は、リクエストごとではなくビルド時にこのルートをレンダリングするようNext.jsに指示する。 ライブデータベースからリクエスト時に取得する真に動的なコンテンツが必要なら、この行を削除する。

Pages Routerを使う代替方法

Next.js Pages Router(App Router より前)を使っている場合でも、最も簡単な方法は引き続き /public/。Route Handlerのファイル規約はApp Routerにのみ存在します。

Pages Router で動的生成を行うなら、カスタムサーバー(Express または Fastify)か getServerSidePropsカスタムコンテンツタイプを持つページですが、静的ファイルより大幅に複雑になります。 /public/llms.txt Pages Routerプロジェクト向けです。

llms-full.txt の追加

llms-full.txt は主要ページの全文を単一ファイル内にインライン化します。Next.js App Routerでは次のようにします:

// app/llms-full.txt/route.ts
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';

export const dynamic = 'force-static';

export async function GET() {
  // Example: read MDX files from content/docs and concatenate them
  const docsDir = path.join(process.cwd(), 'content/docs');
  const files = fs.readdirSync(docsDir).filter(f => f.endsWith('.mdx'));

  const sections = files.map(file => {
    const content = fs.readFileSync(path.join(docsDir, file), 'utf8');
    const slug = file.replace('.mdx', '');
    return `## https://yourdomain.com/docs/${slug}/\n\n${content}`;
  });

  const body = `# yourdomain.com, full content\n\n${sections.join('\n\n---\n\n')}`;

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

設定の確認

デプロイ後、ファイルにアクセス可能であり、形式が正しいことを確認してください:

  1. ブラウザーで https://yourdomain.com/llms.txt ブラウザーで開くと、プレーンテキストが表示されるはずです。
  2. HTTPヘッダーを確認します。 curl -I https://yourdomain.com/llms.txt に次が表示される必要があります: Content-Type: text/plain とステータスコード 200 が表示されます。
  3. URLを llmtxt.infoバリデーター で仕様への準拠を確認します。
  4. 次を確認します: llms.txtpublic/robots.txt.

続きを読む

ソース