惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
罗磊的独立博客
Last Week in AI
Last Week in AI
爱范儿
爱范儿
The Cloudflare Blog
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
IT之家
IT之家
博客园 - 【当耐特】
雷峰网
雷峰网
博客园_首页
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
量子位
V
V2EX

Flags SDK Documentation

Global Config Bulk Evaluation Precompute GrowthBook Hypertune PostHog Split Evaluation Context Quickstart Precompute Evaluation Context Quickstart Precompute GrowthBook Hypertune PostHog Split Proxy Dashboard Pages Marketing Pages Dashboard Pages Marketing Pages Data Locality Providers LaunchDarkly Optimizely Reflag Statsig Server-side vs Client-side Flags as Code
Evaluation Context
Vercel · 2026-05-28 · via Flags SDK Documentation

Segment by any criteria, using an evaluation context.

It is common for features to be on for some users, but off for others. For example team members working on a new setting might need to see and use the setting, while the rest of the team need the setting to be hidden.

The flag declaration accepts an identify function. The entities returned from the identify function are passed as an argument to the decide function.

A trivial case to illustrate the concept:

import { flag } from 'flags/next';

export const exampleFlag = flag<boolean>({
  key: 'identify-example-flag',
  identify() {
    return { user: { id: 'user1' } };
  },
  decide({ entities }) {
    return entities?.user?.id === 'user1';
  },
});

Having first-class support for an evaluation context allows decoupling the identifying step from the decision making step.

The entities can be typed using the flag function.

import { flag } from 'flags/next';

interface Entities {
  user?: { id: string };
}

export const exampleFlag = flag<boolean, Entities>({
  key: 'identify-example-flag',
  identify() {
    return { user: { id: 'user1' } };
  },
  decide({ entities }) {
    return entities?.user?.id === 'user1';
  },
});

The identify function is called with headers and cookies arguments, which is useful when dealing with anonymous or authenticated users.

The arguments are normalized to a common format so the same flag to be used in Routing Middleware, App Router, and Pages Router without having to worry about the differences in how headers and cookies are represented there.

import { flag } from 'flags/next';

export const exampleFlag = flag<boolean, Entities>({
  // ...
  identify({ headers, cookies }) {
    // access to normalized headers and cookies here
    headers.get('auth');
    cookies.get('auth')?.value;
    // ...
  },
  // ...
});

The dedupe function is a helper to prevent duplicate work.

Any function wrapped in dedupe will only ever run once for the same request within the same runtime and given the same arguments.

This helper is extremly useful in combination with the identify function, as it allows the identification to only happen once per request. This is useful in preventing overhead when passing the same identify function to multiple feature flags.

The Marketing Pages example which shows how to identify and target users using cookies when precomputing pages.

Custom evaluation context

While it is best practice to let the identify function determine the evaluation context, it is possible to provide a custom evaluation context.

// pass a custom evaluation context from the call side
await exampleFlag.run({ identify: { user: { id: 'user1' } } });

// pass a custom evaluation context function from the call side
await exampleFlag.run({ identify: () => ({ user: { id: 'user1' } }) });

This should be used sparsely, as custom evaluation context can make feature flags less predictable across your code base.

Full example

The example below shows how to use the identify function to display different content to different users.

The above example is implemented using this feature flag:

import type { ReadonlyRequestCookies } from 'flags';
import { dedupe, flag } from 'flags/next';

interface Entities {
  user?: { id: string };
}

const identify = dedupe(
  ({ cookies }: { cookies: ReadonlyRequestCookies }): Entities => {
    // This could read a JWT instead
    const userId = cookies.get('identify-example-user-id')?.value;
    return { user: userId ? { id: userId } : undefined };
  },
);

export const identifyExampleFlag = flag<boolean, Entities>({
  key: 'identify-example-flag',
  identify,
  decide({ entities }) {
    if (!entities?.user) return false;
    return entities.user.id === 'user1';
  },
});

See the Marketing Pages example