Fumadocs rebuilt on Hamur, the original is fumadocs.dev
コンテンツへスキップ
Fumadocs

ブログ

著者、タグ、記事間のナビゲーションを備えたブログを追加します。

インストール

npx @fumadocs/cli@latest add blog

blogPageSchema を使ってコレクションを定義し、著者を登録します。

lib/source.ts
import { defineCollections } from 'fumadocs-mdx/macro';
import { loader } from 'fumadocs-core/source';
import { type BlogAuthor, blogPageSchema } from './blog';

export const blog = defineCollections({
  type: 'doc',
  dir: 'content/blog',
  schema: blogPageSchema,
});

export const blogSource = loader({
  baseUrl: '/blog',
  source: blog.toFumadocsSource(),
});

export const blogAuthors: Record<string, BlogAuthor> = {
  fuma: {
    name: 'Fuma Nama',
    title: 'Maintainer',
    url: 'https://fuma-nama.dev',
    image: 'https://avatars.githubusercontent.com/u/76240755',
  },
};

記事の作成

content/blog 配下の各ファイルが、それぞれ1つの記事になります:

content/blog/hello-world.mdx
---
title: Hello World
description: My first post.
date: 2026-10-12 # 投稿は日付の新しい順に並びます
tags: [Announcement] # タグごとに /blog/tags 以下にページが作成されます
authors: [fuma] # blogAuthors のキー。それ以外の値は名前として表示されます
image: /blog/hello-world.png # 投稿カードのカバー画像
---

投稿には下書き用のフィールドがないため、執筆途中の投稿は content/blog の外に置いてください。

リモート画像

カバー画像とアバターは next/image でレンダリングされるため、画像のホストを images.remotePatterns で許可してください。

ルート

Home Layout の配下にブログのルートを追加します。getPosts() は、ブログページを著者とタグの情報を含む投稿に変換します。投稿のフィードを作成するには、RSS フィードを参照してください。

記事一覧

app/(home)/blog/page.tsx
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts } from '@/lib/blog';
import { BlogCards } from '@/components/blog';

export default function Page() {
  return (
    <div className="mx-auto w-full max-w-(--fd-layout-width) px-4 py-12">
      <BlogCards posts={getPosts(blogSource.getPages(), blogAuthors)} />
    </div>
  );
}

記事ページ

findPost() は、記事とともに、その前後の記事(新しい記事と古い記事)を返します。BlogPage の toc prop を指定すると、共有ボタン付きのフローティング目次が追加されます。

app/(home)/blog/[slug]/page.tsx
import { notFound } from 'next/navigation';
import { blogAuthors, blogSource } from '@/lib/source';
import { findPost, getPosts } from '@/lib/blog';
import { getMDXComponents } from '@/components/mdx';
import {
  BlogAuthors,
  BlogBackLink,
  BlogBody,
  BlogDate,
  BlogDescription,
  BlogHeader,
  BlogPage,
  BlogPostNav,
  BlogTags,
  BlogTitle,
} from '@/components/blog';

export default async function Page(props: PageProps<'/blog/[slug]'>) {
  const { slug } = await props.params;
  const page = blogSource.getPage([slug]);
  const found = page && findPost(getPosts(blogSource.getPages(), blogAuthors), page.url);
  if (!page || !found) notFound();

  const { post, newer, older } = found;
  const MDX = page.data.body;

  return (
    <BlogPage toc={page.data.toc}>
      <BlogHeader>
        <BlogBackLink href="/blog" />
        <BlogTitle>{post.title}</BlogTitle>
        <BlogDescription>{post.description}</BlogDescription>
        <BlogAuthors authors={post.authors} />
        <BlogTags tags={post.tags} />
        {post.date && <BlogDate date={post.date} />}
      </BlogHeader>
      <BlogBody>
        <MDX components={getMDXComponents()} />
      </BlogBody>
      <BlogPostNav newer={newer} older={older} />
    </BlogPage>
  );
}

タグページ

getTags() はすべてのタグを返します。

app/(home)/blog/tags/page.tsx
import { blogSource } from '@/lib/source';
import { getPosts, getTags } from '@/lib/blog';
import { BlogHeader, BlogTagCards, BlogTitle } from '@/components/blog';

export default function Page() {
  const tags = getTags(getPosts(blogSource.getPages()));

  return (
    <div className="mx-auto flex w-full max-w-(--fd-layout-width) flex-col gap-4 px-4 py-12">
      <BlogHeader>
        <BlogTitle>All Tags</BlogTitle>
      </BlogHeader>
      <BlogTagCards tags={tags} />
    </div>
  );
}

getTagPosts() は、指定したタグが付いた投稿の一覧を返します。

app/(home)/blog/tags/[tag]/page.tsx
import { notFound } from 'next/navigation';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts, getTagPosts, getTags } from '@/lib/blog';
import { BlogCards, BlogHeader, BlogTitle } from '@/components/blog';

export default async function Page(props: PageProps<'/blog/tags/[tag]'>) {
  const { tag } = await props.params;
  const result = getTagPosts(getPosts(blogSource.getPages(), blogAuthors), tag);
  if (!result) notFound();

  return (
    <div className="mx-auto flex w-full max-w-(--fd-layout-width) flex-col gap-4 px-4 py-12">
      <BlogHeader>
        <BlogTitle>{result.tag.name}</BlogTitle>
      </BlogHeader>
      <BlogCards posts={result.posts} />
    </div>
  );
}

export function generateStaticParams() {
  return getTags(getPosts(blogSource.getPages())).map((tag) => ({ tag: tag.slug }));
}

output: 'export' を指定している場合、generateStaticParams() が空の配列を返すと Next.js のビルドが失敗します。このルートは、タグ付きの投稿を作成してから追加してください。

国際化

タグの URL を生成する getBlogUrl に、i18n の設定を渡してください。

lib/blog.ts
import { i18n } from '@/lib/i18n';

// `blogSource` の `baseUrl` と常に一致させてください
export const getBlogUrl = createGetUrl('/blog', i18n);

ルートでは、各ロケールのページを getPosts() に渡します。

const posts = getPosts(blogSource.getPages(lang), blogAuthors);

How is this guide?

最終更新日