Nuxt.js向けllms.txt

Nuxt.jsには三つの方法がある:public/に静的ファイルを置く(設定不要)、動的生成用のNitroサーバールートを使う、またはNuxt Contentドキュメントから自動生成する。

最終更新:

オプション 1:public/ 内の静的ファイル

Nuxtは public/ ディレクトリをビルド出力に直接配置し、 サイトのルートで配信します。ファイルを public/llms.txt そして /llms.txt デプロイ先ごとに利用できます。

public/llms.txt, directory structure
# Nuxt static file approach
#
# Place your file at: public/llms.txt
# Nuxt copies everything in public/ directly to the build output.
#
# Project structure:
# your-nuxt-app/
# ├── public/
# │   └── llms.txt   ← add this
# ├── pages/
# └── nuxt.config.ts
#
# No configuration needed. Works with all deployment presets.

この方法は設定不要で、すべての Nitro プリセットで同じように動作します: node-server, cloudflare-pages, vercel, netlify、および static.

選択肢2:サーバー API ルート

以下の場所にファイルを作成してください: server/routes/llms.txt.ts。Nuxt の Nitro エンジンはファイルを server/routes/ URLパスに直接対応するため、このファイルは正確に /llms.txt 追加の ルーティング設定なしで提供されます。

server/routes/llms.txt.ts
// server/routes/llms.txt.ts
// Nuxt server route, served at /llms.txt
export default defineEventHandler(() => {
  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 in minutes.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog with examples.

## Product

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

## Optional

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

  setResponseHeader(event, 'Content-Type', 'text/plain; charset=utf-8');
  setResponseHeader(event, 'Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');
  return content;
});

使用方法 setResponseHeader から h3 ライブラリ(Nuxt に同梱)を使用して、正しい Content-Type. 追加します Cache-Control ヘッダーを追加すると、CDN エッジがレスポンスをキャッシュし、オリジンの負荷を軽減できます。

オプション3:Nuxt Contentで自動生成

次を使用する場合: Nuxt Content をドキュメントに使用している場合は、コンテンツコレクションを照会してリンク一覧を自動構築できます。これにより、 llms.txt 手動での更新を必要とせず、 ドキュメントと常に同期が保たれます。

server/routes/llms.txt.ts, Nuxt Content
// server/routes/llms.txt.ts
// Auto-generate llms.txt from Nuxt Content documents
import { serverQueryContent } from '#content/server';

export default defineEventHandler(async (event) => {
  // Query all docs; adjust collection name as needed
  const docs = await serverQueryContent(event, '/docs')
    .only(['title', 'description', '_path'])
    .find();

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

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

  setResponseHeader(event, 'Content-Type', 'text/plain; charset=utf-8');
  return body;
});

クエリパス(/docs)とフィールド名をコンテンツ構造に合わせて調整してください。デプロイ後に バリデーター デプロイ後に、書式設定の退行がないかを確認する。

Nitroおよびデプロイ先

  • node-serverでは、サーバールートが Node.js HTTP ハンドラーとして実行されます。次の対象にリバースプロキシ(nginx/Caddy)のキャッシュルールを追加してください: /llms.txt により、リクエストのたびにNodeへ到達するのを避けます。
  • cloudflare-pagespublic/ は Cloudflare の グローバル CDN から配信されます。サーバーのルートは Pages Functions になります。詳しくは Cloudflare ガイド のキャッシュヘッダーについて確認してください。
  • vercelでは、静的ファイルは自動的にエッジキャッシュされます。サーバールートは設定に応じてVercel Serverless FunctionsまたはEdge Functionsになります。
  • static (完全に事前レンダリングされる場合)は、 public/ では静的ファイル方式を使用します。完全な静的出力ではサーバールートを利用できません。

検証

デプロイ後、ファイルが正しく配信されていることを確認します:

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

関連ガイド

ソース