---
title: 版本化文档
description: 使用包提供的 source helper 配置多个文档版本
type: guide
summary: 使用 createVersionedSources 配置稳定版、预发布版或基于域名的文档版本。
prerequisites:
  - /docs/configuration
related:
  - /docs/proxy
  - /docs/llms-txt
---

版本化文档让一个 Geistdocs 项目服务多个文档集合。可用于稳定版、预发布版、旧主版本，或基于域名的版本切换。

## 配置 source collection

在 `source.config.ts` 中为每个版本创建一个 Fumadocs collection：

```ts title="source.config.ts"
import { defineConfig, defineDocs } from "fumadocs-mdx/config";

export const v4docs = defineDocs({
  dir: "content/docs/v4",
  docs: {
    postprocess: {
      includeProcessedMarkdown: true,
    },
  },
});

export const v5docs = defineDocs({
  dir: "content/docs/v5",
  docs: {
    postprocess: {
      includeProcessedMarkdown: true,
    },
  },
});

export default defineConfig({});
```

## 创建版本化 sources

在 `lib/geistdocs/source.ts` 中使用 `createVersionedSources`：

```ts title="lib/geistdocs/source.ts"
import { createVersionedSources } from "@vercel/geistdocs/source";
import { v4docs, v5docs } from "@/.source/server";
import { config } from "./config";

export const versions = createVersionedSources({
  config,
  current: "v4",
  versions: [
    { id: "v4", label: "v4", docs: v4docs, baseUrl: "/docs" },
    {
      id: "v5",
      label: "v5 pre-release",
      docs: v5docs,
      baseUrl: "/docs",
      routePrefix: "/v5",
    },
  ],
});
```

## 渲染版本化页面

当前版本可以用于 `/docs`，特定版本可以用于带版本前缀的路由：

```tsx title="app/[lang]/v5/docs/[[...slug]]/page.tsx"
import { createDocsPage } from "@vercel/geistdocs/pages/docs";
import { config } from "@/lib/geistdocs/config";
import { versions } from "@/lib/geistdocs/source";

const docsPage = createDocsPage({
  config,
  source: versions.byId.v5,
  getPageUrl: ({ page }) => `/v5${page.url}`,
});
```

当公开 URL 和 source 的 `baseUrl` 不一致时，使用 `getPageUrl` 保持页面操作和 Markdown 链接指向公开 URL。

## 添加版本选择器

在本地布局适配器中使用 `GeistdocsVersionSelect`：

```tsx title="components/geistdocs/docs-layout.tsx"
import { GeistdocsVersionSelect } from "@vercel/geistdocs/versions";

<GeistdocsVersionSelect
  current="v4"
  versions={[
    { id: "v4", label: "v4" },
    { id: "v5", label: "v5 pre-release", routePrefix: "/v5" },
  ]}
/>
```

对于基于域名的版本，使用 `href`：

```tsx
<GeistdocsVersionSelect
  current="v6"
  versions={[
    { id: "v6", label: "v6 latest" },
    { id: "v5", label: "v5", href: "https://v5.example.com/:path*" },
  ]}
/>
```

## 下一步

- 阅读 [Proxy 和 Markdown 路由](/docs/proxy)，将版本化页面连接到 `.md`、`.mdx` 和 AI agent 重写。
- 阅读 [llms.txt](/docs/llms-txt)，为多个 source 配置完整文档的 Markdown 输出。
