@featureflip/browser

中文文档
2.19.1发布于 2026/11/10Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 3059

Browser SDK for Featureflip - framework-agnostic feature flag client

原始使用文档
文档目录9 节
  1. Installation
  2. Quick Start
  3. API Reference
  4. FeatureflipClient.get(config)
  5. Configuration Options
  6. Methods
  7. Bootstrapping
  8. Testing
  9. License

@featureflip/browser

Framework-agnostic browser SDK for evaluating Featureflip feature flags.

Installation

npm install @featureflip/browser

Quick Start

import { FeatureflipClient } from '@featureflip/browser';

const client = FeatureflipClient.get({
  clientKey: 'your-client-sdk-key',
});

await client.initialize();

const showBanner = client.boolVariation('show-banner', false);

Singleton by construction. FeatureflipClient.get() is the only way to obtain a client — the public constructor was removed in v2.0. Calling get() more than once with the same clientKey returns handles pointing at one shared underlying client (refcounted). This makes framework bindings, React StrictMode double-mounts, and per-component construction all harmless — they all resolve to one SSE connection and one flag store per key.

API Reference

FeatureflipClient.get(config)

FeatureflipClient.get(config: FeatureflipClientConfig): FeatureflipClient

Returns a client for the given client key. The first call constructs and registers a shared core; subsequent calls with the same key return a new handle pointing at the cached core. When the last handle for a key is closed, the core shuts down and is removed from the cache.

Configuration Options

OptionTypeDefaultDescription
clientKeystring(required)Client SDK key from your project settings
baseUrlstringhttps://eval.featureflip.ioEvaluation API base URL
contextRecord<string, unknown>{}Initial evaluation context (user attributes)
streamingbooleantrueEnable SSE streaming for real-time updates
initTimeoutnumber10000Timeout in ms for the initial evaluate request
bootstrapRecord<string, unknown> | 'localStorage'undefinedValues to serve before the first server response. See Bootstrapping.
sendEvaluationEventsbooleantrueReport which flags your app reads. Featureflip uses this to tell a flag your code still reads from one it only sends, which is what lets an unused client-side flag be archived. Only reads through flagDetail() or the typed *Variation methods count. Set false to report nothing — Featureflip then treats every flag it sends to this client as in use.

Methods

initialize(options?: { waitForServer?: boolean }): Promise<void>

Fetches all flag values from the server. Must be called before reading variations (unless the client was bootstrapped). Opens an SSE streaming connection if streaming is enabled. With bootstrap values it resolves immediately while the fetch runs in the background; pass { waitForServer: true } to wait for the first server response instead.

flagSource: 'none' | 'bootstrap' | 'localStorage' | 'server'

Where the values currently being served came from.

boolVariation(key: string, defaultValue: boolean): boolean

Returns a boolean flag value, or defaultValue if the flag is missing or not a boolean.

stringVariation(key: string, defaultValue: string): string

Returns a string flag value, or defaultValue if the flag is missing or not a string.

numberVariation(key: string, defaultValue: number): number

Returns a number flag value, or defaultValue if the flag is missing or not a number.

jsonVariation<T>(key: string, defaultValue: T): T

Returns a flag value cast to T, or defaultValue if the flag is missing.

identify(context: Record<string, unknown>): Promise<void>

Re-evaluates all flags with a new context (e.g., after login). Emits change events for any flags whose values changed.

await client.identify({ user_id: '123', plan: 'pro' });

on(event: EventType, handler: EventHandler): void

Subscribe to events.

  • 'ready' -- fired once, when the client first has values to serve (receives { source }, the same values as flagSource)
  • 'change' -- fired when flag values change (receives a FlagChanges object)
  • 'error' -- fired on streaming or network errors
client.on('change', (changes) => {
  console.log('Flags changed:', changes);
});

off(event: EventType, handler: EventHandler): void

Unsubscribe from events.

close(): void

Decrements the refcount on the shared core. When the last handle for a given client key is closed, the shared core closes the SSE connection and removes itself from the factory cache. Double-close on the same handle is a no-op.

Bootstrapping

Pass bootstrap so the first paint doesn't wait on the network:

  • An object of { flagKey: value }, typically evaluated by a server SDK while rendering the page and embedded in the HTML. The client is initialized immediately and makes no request before your first read.
  • 'localStorage': the client saves the values it receives per client key and context (the five most recent contexts, named by a hash so context attributes are not stored) and serves them on the next page load. Values for one context are never served to another. Ignored for a server SDK key.

Either way the client still fetches and streams as usual. The server's values replace the bootstrap ones and emit change for any that differ.

const client = FeatureflipClient.get({
  clientKey: 'your-client-sdk-key',
  context: window.__FLAG_CONTEXT__, // the same context the server evaluated with
  bootstrap: window.__FLAGS__,
});

For server-side rendering, FeatureflipClient.forServerRender(config) returns a client that serves the bootstrap object's values, makes no network requests, and is not cached, so concurrent requests for different users never share one.

Full guide: Bootstrap client-side flags.

Testing

Use FeatureflipClient.forTesting() to create a client with predetermined flag values -- no network calls.

const client = FeatureflipClient.forTesting({
  'show-banner': true,
  'button-color': 'blue',
});

client.boolVariation('show-banner', false); // true
client.stringVariation('button-color', 'red'); // 'blue'

License

Apache-2.0

版本与发布记录

@featureflip/browser 共发布 33 个版本,首次发布于 2026/04/02;过去 12 个月发布了 33 个版本。

版本发布日期距今
2.19.1latest2026/11/104 天前
2.19.02026/11/086 天前
2.18.02026/10/3015 天前
2.17.22026/10/2817 天前
2.17.12026/10/2717 天前
2.17.02026/10/2718 天前

关于 @featureflip/browser 的常见问题

@featureflip/browser 是什么?

Browser SDK for Featureflip - framework-agnostic feature flag client

如何安装 @featureflip/browser?

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

@featureflip/browser 支持 TypeScript 吗?

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

@featureflip/browser 支持 ESM 还是 CommonJS?

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

@featureflip/browser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@featureflip/browser 可以商用吗?

@featureflip/browser 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@featureflip/browser 的下载量有多少?

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

@featureflip/browser 还在维护吗?

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