@gitbook/adaptive

中文文档
1.0.2发布于 2026/02/13TypeScriptESM + CJS构建来源已验证近 30 天下载 3万

Utility to use adaptive content in GitBook

原始使用文档
文档目录7 节
  1. Prerequisites
  2. Feature Flag Helpers
  3. LaunchDarkly Integration
  4. Reflag Integration
  5. Generic Utilities
  6. writeGitBookVisitorCookie
  7. Important Note

@gitbook/adaptive

GitBook's adaptive content feature allows you to personalize your documentation based on visitor data. The @gitbook/adaptive SDK provides utilities to help facilitate passing data to GitBook for adapting your content.

This SDK includes both feature flag helpers for popular providers like LaunchDarkly and Reflag, as well as generic utilities for writing custom visitor data.


Prerequisites

Make sure you have:

  • A GitBook site with adaptive content enabled
  • An active project on LaunchDarkly or Reflag
  • A frontend project (React-based) where feature flags are available client-side

Feature Flag Helpers

The SDK provides convenient helpers for popular feature flag providers to automatically sync flag values with GitBook's adaptive content system.

LaunchDarkly Integration

1. Install the GitBook + LaunchDarkly Integration

In GitBook, install the LaunchDarkly integration for your site.

2. Add Your Access Credentials

In the GitBook integration settings, provide:

  • Your project key
  • A service access token from your LaunchDarkly account

3. Install the GitBook Helper

npm install @gitbook/adaptive

4. Configure the Client

import { render } from 'react-dom';
import { withLaunchDarkly } from '@gitbook/adaptive';
import { asyncWithLDProvider, useLDClient } from 'launchdarkly-react-client-sdk';
import MyApplication from './MyApplication';

function PassFeatureFlagsToGitBookSite() {
    const ldClient = useLDClient();

    React.useEffect(() => {
        if (!ldClient) return;
        return withLaunchDarkly(ldClient);
    }, [ldClient]);

    return null;
}

(async () => {
    const LDProvider = await asyncWithLDProvider({
        clientSideID: 'client-side-id-123abc',
        context: {
            kind: 'user',
            key: 'user-key-123abc',
            name: 'Sandy Smith',
            email: '[email protected]',
        },
        options: {}
    });

    render(
        <LDProvider>
            <PassFeatureFlagsToGitBookSite />
            <MyApplication />
        </LDProvider>,
        document.getElementById('reactDiv')
    );
})();

Visitor Schema Output

Once connected, feature flag values will be available in GitBook under:

unsigned.launchdarkly.flags

Reflag Integration

1. Install the GitBook + Reflag Integration

In GitBook, enable the Reflag integration for your site.

2. Add Your Secret Key

In the GitBook integration settings, provide your Reflag secret key.

3. Install the GitBook Helper

npm install @gitbook/adaptive

4. Configure the Client

import { withReflag } from '@gitbook/adaptive';
import { ReflagProvider, useClient } from '@reflag/react-sdk';
import MyApplication from './MyApplication';

function PassFeatureFlagsToGitBookSite() {
    const client = useClient();

    React.useEffect(() => {
        if (!client) return;
        return withReflag(client);
    }, [client]);

    return null;
}

export function Application() {
    const currentUser = useLoggedInUser();
    const appConfig = useAppConfig();

    return (
        <ReflagProvider
            publishableKey={appConfig.reflag.publishableKey}
            user={{
                id: currentUser.uid,
                email: currentUser.email ?? undefined,
                name: currentUser.displayName ?? '',
            }}
            company={{
                id: currentUser.company.id,
            }}
        >
            <PassFeatureFlagsToGitBookSite />
            <MyApplication />
        </ReflagProvider>
    );
}

Visitor Schema Output

Once connected, feature flag values will be available in GitBook under:

unsigned.reflag.flags

Generic Utilities

If you don't use LaunchDarkly or Reflag, or need to pass custom data beyond feature flags, the SDK provides generic utilities for writing visitor data.

writeGitBookVisitorCookie

The writeGitBookVisitorCookie function allows you to write custom visitor data that will be available in GitBook's adaptive content system.

import { writeGitBookVisitorCookie } from '@gitbook/adaptive';

// Write custom visitor data
writeGitBookVisitorCookie('userPlan', 'premium');
writeGitBookVisitorCookie('userRole', 'admin');
writeGitBookVisitorCookie('preferences', {
    theme: 'dark',
    language: 'en'
});

The data will be available in GitBook under:

unsigned.{cookieName}

For example, the above code would create:

  • unsigned.userPlan with value "premium"
  • unsigned.userRole with value "admin"
  • unsigned.preferences with value { theme: "dark", language: "en" }

Important Note

All visitor data is evaluated on the client side and stored in cookies. Do not pass sensitive or security-critical data through this method.

版本与发布记录

@gitbook/adaptive 共发布 6 个版本,首次发布于 2025/05/13;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.2latest2026/02/139 个月前
1.0.02025/09/091.2 年前
0.1.12025/06/221.4 年前
0.1.02025/06/091.4 年前
0.0.22025/05/131.5 年前
0.0.12025/05/131.5 年前

关于 @gitbook/adaptive 的常见问题

@gitbook/adaptive 是什么?

Utility to use adaptive content in GitBook

如何安装 @gitbook/adaptive?

在项目目录运行 npm install @gitbook/adaptive,或使用 pnpm add / yarn add / bun add。

@gitbook/adaptive 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gitbook/adaptive 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gitbook/adaptive 可以商用吗?

@gitbook/adaptive 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@gitbook/adaptive 的下载量有多少?

根据 npm 官方下载统计,@gitbook/adaptive 在 2026/10/17 – 2026/11/15 这 30 天内被下载 30,224 次,日均约 1,007 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gitbook/adaptive 还在维护吗?

最新版本 1.0.2 发布于 2026/02/13(9 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/05/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。