# Fumadocs (Framework Mode, Next.js): Takumi
URL: /zh-TW/docs/next/integrations/og/takumi
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/og/takumi.zh-TW.mdx

整合 Takumi，快速產生中繼資料圖片，且不受框架限制。

## 安裝 [#installation]

```npm
npm install takumi-js
```

`takumi-js` 在 Node.js 上會載入原生綁定 (`@takumi-rs/core`) 。請將它標記為外部套件，避免被打包進伺服器 bundle：

```ts title="next.config.ts"
import type { NextConfig } from 'next';

const config: NextConfig = {
  // [!code ++]
  serverExternalPackages: ['@takumi-rs/core'],
};
```

## 中繼資料圖片 [#metadata-image]

新增一個回傳頁面圖片 URL 的函式，並在文件頁面的中繼資料中引用該圖片：

```ts tab="lib/source.ts"
// [!code ++:8]
export function getPageImageUrl(page: (typeof source)['$inferPage']) {
  const segments = [...page.slugs, 'image.webp'];

  return {
    segments,
    url: '/' + [page.locale, 'og', 'docs', ...segments].filter(Boolean).join('/'),
  };
}
```

```tsx tab="app/docs/[[...slug]]/page.tsx"
import { notFound } from 'next/navigation';
import { source, getPageImageUrl } from '@/lib/source';
import type { Metadata } from 'next';

export async function generateMetadata(props: PageProps<'/docs/[[...slug]]'>): Promise<Metadata> {
  const params = await props.params;
  const page = source.getPage(params.slug);
  if (!page) notFound();

  return {
    title: page.data.title,
    description: page.data.description,
    openGraph: {
      // [!code ++]
      images: getPageImageUrl(page).url,
    },
  };
}
```

> `/docs/my-page` 的圖片位於 `/og/docs/my-page/image.webp`，因為 WebP 圖片的檔案比 PNG 小。若要產生 PNG，請改用 `image.png` 與 `format: 'png'`。

接著建立用來產生圖片的路由：

```tsx title="app/og/docs/[...slug]/route.tsx"
import { getPageImageUrl, source } from '@/lib/source';
import { notFound } from 'next/navigation';
import { generateOGImage } from 'fumadocs-ui/og/takumi';

export const revalidate = false;

export async function GET(_req: Request, { params }: RouteContext<'/og/docs/[...slug]'>) {
  const { slug } = await params;
  const page = source.getPage(slug.slice(0, -1));
  if (!page) notFound();

  return generateOGImage({
    title: page.data.title,
    description: page.data.description,
    site: 'My App',
    format: 'webp',
  });
}

export function generateStaticParams() {
  return source.getPages().map((page) => ({
    lang: page.locale,
    slug: getPageImageUrl(page).segments,
  }));
}
```

> Takumi 已內建完整字重軸 (100-900) 的 [`Geist`](https://vercel.com/font) 與 `Geist Mono` 字型，無須另外設定字型。

進階用法請參閱 [Takumi 官方文件](https://takumi.kane.tw/docs)。

### 其他範本 [#other-templates]

可用的範本請參閱 [Takumi 範本](https://takumi.kane.tw/docs/templates)。