SEOと検索
検索はFumadocsが提供します。Onedocsにはサイトマップ、robots.txt、llms.txtファイル用のヘルパーがあり、すべてNext.js App Routerのルートとしてセットアップできます。ページメタデータとOpen Graph画像については、APIリファレンスのonedocs/metadataとonedocs/ogを参照してください。
検索
OnedocsはFumadocsに組み込まれたOrama検索を使います。APIルートを作成してください。
import { source } from "@/lib/source";
import { createFromSource } from "fumadocs-core/search/server";
export const { GET } = createFromSource(source);
RootProviderが追加する検索ダイアログは、デフォルトで/api/searchにクエリを送るため、追加の設定は不要です。
サイトマップ
/sitemap.xmlにサイトマップを生成します。
import { generateSitemap } from "onedocs/seo";
import meta from "../../content/docs/meta.json";
const baseUrl = "https://yourdomain.com";
export default function sitemap() {
return generateSitemap({
baseUrl,
pages: meta.pages ?? [],
});
}
generateSitemapは最初にサイトのルートを列挙し、その後、/docsにあるpagesの各エントリーにつきURLを1つずつ追加します。---で始まるエントリー(サイドバーの区切り)はスキップされ、indexは/docs自体に対応します。エントリーは指定どおりに読み込まれるため、サブフォルダー内のページには個別のエントリーが必要です(例:"guides/setup")。
Robots.txt
/robots.txtにrobots.txtを生成します。
import { generateRobots } from "onedocs/seo";
const baseUrl = "https://yourdomain.com";
export default function robots() {
return generateRobots({ baseUrl });
}
次のファイルが作成されます。
User-Agent: *
Allow: /
Host: https://yourdomain.com
Sitemap: https://yourdomain.com/sitemap.xml
LLMs.txt
LLMs.txtを使うと、AIアシスタントがドキュメントを理解しやすくなります。Onedocsでは要約版と全文版の両方を生成できます。
要約版
ページタイトルと説明を含む/llms.txtを作成します。
import { createLLMsSource, generateLLMsText } from "onedocs/llms";
import { source } from "@/lib/source";
import config from "../../../onedocs.config";
export async function GET() {
const llmsSource = createLLMsSource(source);
const text = await generateLLMsText(llmsSource, {
title: config.title,
description: config.description,
});
return new Response(text, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
ファイルはタイトルと説明で始まり、説明付きで各ページを- Title: URLとして列挙し、/llms-full.txtへの参照で終わります。どちらの関数もFumadocsソースそのものではなく、createLLMsSourceでラップしたソースを受け取り、Promiseを返します。
全文版
ドキュメント全体を含む/llms-full.txtを作成します。
import { createLLMsSource, generateLLMsFullText } from "onedocs/llms";
import { source } from "@/lib/source";
export async function GET() {
const llmsSource = createLLMsSource(source);
const text = await generateLLMsFullText(llmsSource);
return new Response(text, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
各ページは、タイトル、URL、説明、処理済みMarkdownの順に記述され、ページ間には---が入ります。
ページのテキストは、Fumadocs MDXで処理されたMarkdownから取得します。これはデフォルトでは無効です。有効にしない場合、llms-full.txtにはタイトル、URL、説明のみが含まれます(#3)。source.config.tsで有効にしてください。
import { defineDocs } from "fumadocs-mdx/config";
export const docs = defineDocs({
dir: "content/docs",
docs: {
postprocess: {
includeProcessedMarkdown: true,
},
},
});
設定オプション
サイトマップのオプション
generateSitemap({
baseUrl: "https://yourdomain.com",
pages: ["index", "getting-started", "configuration"],
docsPath: "/docs", // Default: "/docs"
});
Robotsのオプション
generateRobots({
baseUrl: "https://yourdomain.com",
sitemapPath: "/sitemap.xml", // Default: "/sitemap.xml"
});
できること
これらのルートをセットアップすると、次のようになります。
| URL | 機能 |
|---|---|
/api/search?query=... |
検索APIエンドポイント |
/sitemap.xml |
検索エンジン向けXMLサイトマップ |
/robots.txt |
サイトマップ参照を含むRobotsファイル |
/llms.txt |
AIアシスタント向けの要約 |
/llms-full.txt |
AIアシスタント向けのドキュメント全文 |