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

- [ ホーム ](/ja/) 
/
- [ llms.txtの作成方法 ](/ja/how-to-create/) 
/
- Next.js            
# Next.jsでの`llms.txt`

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

最終更新: 2026年4月22日

このページの内容

- [ 2つのアプローチ ](#overview)
- [ 方法1：/public/ 内の静的ファイル ](#static)
- [ 方法 2: App Router のルートハンドラー ](#route-handler)
- [ Pages Routerを使う代替方法 ](#pages-router)
- [ llms-full.txt の追加 ](#llms-full)
- [ 設定の確認 ](#verify)            
## 2つのアプローチ

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

- **次の場所にある静的ファイル: `/public/`**。最も簡単な方式です。ファイルを 手動で作成するか、ビルド時に生成します。App RouterとPages
Routerの両方で動作し、 ランタイムのオーバーヘッドはありません。 
- **App Router の Route Handler**、次の場所に置くTypeScriptファイルです：
`app/llms.txt/route.ts` データソース（MDXファイル、CMS、データベース）からコンテンツを動的に生成します。コンテンツが頻繁に変わる場合に最適です。   
ほとんどのサイトでは、 `/public/` が適切な選択です。サイトのデータからビルド時またはリクエスト時にコンテンツを自動生成したい場合にのみ、Route
Handlerを使用してください。

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

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

- ファイルを作成します： `touch public/llms.txt` 
- `llms.txt` コンテンツを作成します（詳しくは [作成方法ガイド](/ja/how-to-create/) と
[ジェネレーター](/ja/generator/) 正しい形式については、   
```
# 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.

```

- ファイルをcommitしてデプロイします。これで完了です。     
例

Content-Type ヘッダー

Next.jsは  /public/llms.txt  で  Content-Type: text/plain 
自動的に（  .txt  拡張子）に基づきます。追加のヘッダー設定は不要です。

## 方法 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に指示する。
ライブデータベースからリクエスト時に取得する真に動的なコンテンツが必要なら、この行を削除する。

注記

MDXファイルから自動生成

MDX
コンテンツディレクトリからページ一覧を自動生成するには、ハードコードした文字列を、コンテンツソースを読み取るロジックに置き換えます:
const files = fs.readdirSync('content/docs')  → タイトルとスラッグを抽出 → Markdownの
リストを作成します。これにより  llms.txt  実際のページと自動的に同期できます。

## 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`](/ja/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' },
});
}

```

## 設定の確認

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

- ブラウザーで `https://yourdomain.com/llms.txt` ブラウザーで開くと、プレーンテキストが表示されるはずです。 
- HTTPヘッダーを確認します。 `curl -I https://yourdomain.com/llms.txt` に次が表示される必要があります:
`Content-Type: text/plain` とステータスコード 200 が表示されます。 
- URLを [llmtxt.infoバリデーター](/ja/validator/) で仕様への準拠を確認します。 
- 次を確認します： `llms.txt` が `public/robots.txt`.   
## 続きを読む

- [llms.txtの作成方法](/ja/how-to-create/)、すべてのフレームワーク向けテンプレートを含む総合ガイド。 
- [llms-full.txt とは何ですか？](/ja/llms-full-txt/)、コーパス全文を収録する付属ファイル。 
- [バリデータ](/ja/validator/)、ファイルを仕様と照合します。 
- [ベストプラクティス](/ja/best-practices/)、含めるべき内容と避けるべき内容。        
## ソース

- [ Next.js ドキュメント、「/public/」の静的アセット ](https://nextjs.org/docs/app/api-reference/file-conventions/public-folder)
- [ Next.jsドキュメント、Route Handlers ](https://nextjs.org/docs/app/api-reference/file-conventions/route)
- [ llmstxt.org、仕様リファレンス ](https://llmstxt.org/)
