# Fumadocs (Framework Mode, Waku): Notion
URL: /ja/docs/waku/integrations/content/notion
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/content/notion.ja.mdx

Fumadocs を使って Notion データベースのページを公開します。

`@fumadocs/notion` は、Notion データベースのページを[コンテンツソース](/docs/headless/source-api)として読み込み、各ページのブロックを Fumadocs UI でサーバー側でレンダリングします。

## セットアップ [#setup]

```package-install
@fumadocs/notion @notionhq/client
```

Fumadocs UI のスタイルの後に、そのスタイルを追加してください。

```css title="Tailwind CSS"
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
/* [!code ++] */
@import '@fumadocs/notion/css/preset.css';
```

### Notion データベース [step] [#notion-database]

[Notion インテグレーションを作成](https://developers.notion.com/docs/getting-started)し、ページを格納しているデータベースに接続します。データベースの各行が1つのページに対応し、以下のプロパティを持ちます。

* タイトルプロパティはページのタイトルです。
* `Slug` は URL パスです。ネストされたページであれば `guides/install` のように指定します。指定しない場合、スラッグはタイトルから生成されます。
* `Description` はページの説明です。

別のプロパティ名を使用する場合は、`dynamicSource()` の `properties` オプションを設定してください。

インテグレーショントークンとデータベースの[データソース ID](https://developers.notion.com/guides/get-started/upgrade-guide-2025-09-03) を環境変数に追加します。

```ini title=".env.local"
NOTION_TOKEN=ntn_...
# データベース URL 内のデータベース ID ではない点に注意してください
NOTION_DATA_SOURCE_ID=...
```

### コンテンツソース [step] [#content-source]

ソースを作成し、`getSource()` からローダーを取得します。

```ts title="lib/source.ts"
import { Client } from '@notionhq/client';
import { dynamicLoader } from 'fumadocs-core/source';
import { createNotion } from '@fumadocs/notion/source';

export const notion = createNotion({
  client: new Client({ auth: process.env.NOTION_TOKEN }),
  dataSourceId: process.env.NOTION_DATA_SOURCE_ID!,
});

const source = dynamicLoader(
  notion.dynamicSource({
    // `Published` にチェックが入っている行のみを公開します
    query: {
      filter: { property: 'Published', checkbox: { equals: true } },
    },
  }),
  { baseUrl: '/docs' },
);

export async function getSource() {
  return source.get();
}
```

### ページのレンダリング [step] [#rendering-pages]

`page.data.load()` はページのブロックを取得します。取得したブロックは `NotionRenderer` でレンダリングしてください：

```tsx tab="src/pages/docs/_layout.tsx"
import type { ReactNode } from 'react';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { getSource } from '@/lib/source';

export default async function Layout({ children }: { children: ReactNode }) {
  const source = await getSource();

  return <DocsLayout tree={source.getPageTree()}>{children}</DocsLayout>;
}
```

```tsx tab="src/pages/docs/[...slugs].tsx"
import type { PageProps } from 'waku/router';
import { unstable_notFound } from 'waku/router/server';
import { getSource } from '@/lib/source';
import {
  NotionRenderer,
  blocksToTableOfContents,
  createNotionFileUrlResolver,
} from '@fumadocs/notion';
import { DocsPage, DocsBody, DocsDescription, DocsTitle } from 'fumadocs-ui/layouts/docs/page';

export default async function Page({ slugs }: PageProps<'/docs/[...slugs]'>) {
  const source = await getSource();
  const page = source.getPage(slugs);
  if (!page) unstable_notFound();

  const { blocks } = await page.data.load();

  return (
    <DocsPage toc={blocksToTableOfContents(blocks)}>
      <DocsTitle>{page.data.title}</DocsTitle>
      <DocsDescription>{page.data.description}</DocsDescription>
      <DocsBody>
        <NotionRenderer
          blocks={blocks}
          getFileUrl={createNotionFileUrlResolver({ pageId: page.data.id })}
        />
      </DocsBody>
    </DocsPage>
  );
}

export const getConfig = async () => ({ render: 'dynamic' }) as const;
```

### ファイルルート [step] [#file-route]

画像や PDF など、Notion 上でホストされているファイルの URL は約 1 時間で有効期限が切れます。`createNotionFileUrlResolver()` は、これらのファイルの URL を、新しい URL へリダイレクトするルートに置き換えます。

```ts title="src/pages/_api/api/notion/file.ts"
import { createNotionFileHandler } from '@fumadocs/notion';
import { notion } from '@/lib/source';

export const GET = createNotionFileHandler(notion);
```

ルートを別のパスにマウントする場合は、同じパスをリゾルバーにも渡してください。

```ts
createNotionFileUrlResolver({ pageId: page.data.id, path: '/notion/file' });
```

## 再検証 [#revalidation]

ローダーはページをメモリ上に保持します。Notion で編集した後は、Webhook などからローダーを再検証してください。

```ts title="lib/source.ts"
export function revalidateNotion() {
  return source.revalidate();
}
```

プレビューなどでリクエストごとに最新のコンテンツを取得するには、読み込む前にキャッシュを無効化します。

```ts title="lib/source.ts"
export async function getSource() {
  source.invalidate(); // [!code ++]
  return source.get();
}
```

## ブロックのカスタマイズ [#customizing-blocks]

ブロックタイプのレンダリングをオーバーライドするには、`components` を渡します。

```tsx
<NotionRenderer
  blocks={blocks}
  components={{
    callout({ block, children, renderRichText }) {
      return (
        <aside className="rounded-lg bg-fd-muted p-4">
          {renderRichText(block.callout.rich_text)}
          {children}
        </aside>
      );
    },
  }}
  // デフォルトは Shiki。ハイライトなしのプレーンなコードにするには `false`、または `(code, language) => ReactNode` を指定します
  highlightCode={false}
/>
```

## 制限事項 [#limitations]

* サイト上のページになるのはデータベースの行だけであるため、サブページやその他の Notion ページへのリンクは Notion 上のページを指します。
* ファイルルートにはサーバーが必要です。静的エクスポートする場合は、ファイルを Notion 以外の場所でホストし、`getFileUrl` は指定しないでください。