Adds a feature-flags collection to Payload with on/off state, percentage rollouts and weighted variants. Flags are read through server helpers for React Server Components, client hooks, or an optional public evaluate endpoint. Bucketing is deterministic per flag and user. Requires Payload 3.56 or later and React 19.
Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts
Installation
npm i @xtr-dev/payload-feature-flags4
Stars
0
Forks
0
Open Issues
1 weeks ago
Last Updated
508
Weekly Downloads
v0.3.0
npm Version
252.0 KB
Unpacked Size
0
Dependencies
4+1 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 4 |
| Dec 4, 2026 | 4 |
| Dec 3, 2026 | 4 |
| Dec 2, 2026 | 4 |
| Dec 1, 2026 | 4 |
| Nov 30, 2026 | 4 |
| Nov 29, 2026 | 4 |
| Nov 28, 2026 | 4 |
| Nov 27, 2026 | 4 |
| Nov 26, 2026 | 4 |
| Nov 25, 2026 | 4 |
| Nov 24, 2026 | 4 |
| Nov 23, 2026 | 4 |
| Nov 22, 2026 | 4 |
| Nov 21, 2026 | 4 |
| Nov 20, 2026 | 4 |
| Nov 19, 2026 | 4 |
| Nov 18, 2026 | 4 |
| Nov 17, 2026 | 4 |
| Nov 16, 2026 | 4 |
| Nov 15, 2026 | 4 |
| Nov 14, 2026 | 4 |
| Nov 13, 2026 | 4 |
| Nov 12, 2026 | 4 |
| Nov 11, 2026 | 4 |
| Nov 10, 2026 | 4 |
| Nov 9, 2026 | 4 |
| Nov 8, 2026 | 4 |
| Nov 7, 2026 | 4 |
| Nov 6, 2026 | 4 |
| Nov 5, 2026 | 4 |
| Nov 4, 2026 | 4 |
| Nov 3, 2026 | 4 |
| Nov 2, 2026 | 4 |
| Nov 1, 2026 | 4 |
| Oct 31, 2026 | 4 |
| Oct 30, 2026 | 4 |
| Oct 29, 2026 | 4 |
| Oct 28, 2026 | 4 |
| Oct 27, 2026 | 4 |
| Oct 26, 2026 | 4 |
| Oct 25, 2026 | 4 |
| Oct 24, 2026 | 4 |
| Oct 23, 2026 | 4 |
| Oct 22, 2026 | 4 |
| Oct 21, 2026 | 4 |
| Oct 20, 2026 | 4 |
| Oct 19, 2026 | 4 |
| Oct 18, 2026 | 4 |
| Oct 17, 2026 | 4 |
| Oct 16, 2026 | 4 |
| Oct 15, 2026 | 4 |
| Oct 14, 2026 | 4 |
| Oct 13, 2026 | 4 |
| Oct 12, 2026 | 4 |
| Oct 11, 2026 | 4 |
| Oct 10, 2026 | 4 |
| Oct 9, 2026 | 4 |
| Oct 8, 2026 | 4 |
| Oct 7, 2026 | 4 |
| Oct 6, 2026 | 4 |
| Oct 5, 2026 | 4 |
| Oct 4, 2026 | 4 |
| Oct 3, 2026 | 4 |
| Oct 2, 2026 | 4 |
| Oct 1, 2026 | 4 |
| Sep 30, 2026 | 4 |
| Sep 29, 2026 | 4 |
| Sep 28, 2026 | 4 |
| Sep 27, 2026 | 4 |
| Sep 26, 2026 | 4 |
| Sep 25, 2026 | 4 |
| Sep 24, 2026 | 4 |
| Sep 23, 2026 | 4 |
| Sep 22, 2026 | 4 |
| Sep 21, 2026 | 4 |
| Sep 20, 2026 | 4 |
| Sep 19, 2026 | 4 |
| Sep 18, 2026 | 4 |
| Sep 17, 2026 | 4 |
| Sep 16, 2026 | 3 |
| Sep 15, 2026 | 3 |
| Sep 14, 2026 | 3 |
| Sep 13, 2026 | 3 |
| Sep 12, 2026 | 3 |
| Sep 11, 2026 | 3 |
| Sep 10, 2026 | 3 |
| Sep 9, 2026 | 3 |
| Sep 8, 2026 | 3 |
| Sep 7, 2026 | 3 |
| Sep 6, 2026 | 3 |
671+647 since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 671 |
| Dec 4, 2026 | 797 |
| Dec 3, 2026 | 796 |
| Dec 2, 2026 | 754 |
| Dec 1, 2026 | 785 |
| Nov 30, 2026 | 839 |
| Nov 29, 2026 | 630 |
| Nov 28, 2026 | 509 |
| Nov 27, 2026 | 331 |
| Nov 26, 2026 | 363 |
| Nov 25, 2026 | 395 |
| Nov 24, 2026 | 346 |
| Nov 23, 2026 | 268 |
| Nov 22, 2026 | 159 |
| Nov 21, 2026 | 108 |
| Nov 20, 2026 | 105 |
| Nov 19, 2026 | 100 |
| Nov 18, 2026 | 104 |
| Nov 17, 2026 | 128 |
| Nov 16, 2026 | 176 |
| Nov 15, 2026 | 179 |
| Nov 14, 2026 | 181 |
| Nov 13, 2026 | 232 |
| Nov 12, 2026 | 267 |
| Nov 11, 2026 | 269 |
| Nov 10, 2026 | 254 |
| Nov 9, 2026 | 205 |
| Nov 8, 2026 | 196 |
| Nov 7, 2026 | 202 |
| Nov 6, 2026 | 189 |
| Nov 5, 2026 | 123 |
| Nov 4, 2026 | 84 |
| Nov 3, 2026 | 66 |
| Nov 2, 2026 | 66 |
| Nov 1, 2026 | 68 |
| Oct 31, 2026 | 52 |
| Oct 30, 2026 | 16 |
| Oct 29, 2026 | 15 |
| Oct 28, 2026 | 12 |
| Oct 27, 2026 | 15 |
| Oct 26, 2026 | 27 |
| Oct 25, 2026 | 54 |
| Oct 24, 2026 | 104 |
| Oct 23, 2026 | 101 |
| Oct 22, 2026 | 103 |
| Oct 21, 2026 | 123 |
| Oct 20, 2026 | 120 |
| Oct 19, 2026 | 108 |
| Oct 18, 2026 | 77 |
| Oct 17, 2026 | 25 |
| Oct 16, 2026 | 26 |
| Oct 15, 2026 | 85 |
| Oct 14, 2026 | 109 |
| Oct 13, 2026 | 142 |
| Oct 12, 2026 | 156 |
| Oct 11, 2026 | 169 |
| Oct 10, 2026 | 171 |
| Oct 9, 2026 | 173 |
| Oct 8, 2026 | 115 |
| Oct 7, 2026 | 72 |
| Oct 6, 2026 | 39 |
| Oct 5, 2026 | 26 |
| Oct 4, 2026 | 12 |
| Oct 3, 2026 | 17 |
| Oct 2, 2026 | 23 |
| Oct 1, 2026 | 24 |
| Sep 30, 2026 | 32 |
| Sep 29, 2026 | 43 |
| Sep 28, 2026 | 42 |
| Sep 27, 2026 | 49 |
| Sep 26, 2026 | 46 |
| Sep 25, 2026 | 43 |
| Sep 24, 2026 | 57 |
| Sep 23, 2026 | 50 |
| Sep 22, 2026 | 42 |
| Sep 21, 2026 | 58 |
| Sep 20, 2026 | 53 |
| Sep 19, 2026 | 52 |
| Sep 18, 2026 | 50 |
| Sep 17, 2026 | 34 |
| Sep 16, 2026 | 35 |
| Sep 15, 2026 | 32 |
| Sep 14, 2026 | 18 |
| Sep 13, 2026 | 15 |
| Sep 12, 2026 | 16 |
| Sep 11, 2026 | 21 |
| Sep 10, 2026 | 19 |
| Sep 9, 2026 | 20 |
| Sep 8, 2026 | 21 |
| Sep 7, 2026 | 20 |
| Sep 6, 2026 | 24 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1055577181-root)<a href="https://payloaddirectory.dev/plugins/1055577181-root"><img src="https://payloaddirectory.dev/badge/1055577181-root.svg" alt="Payload Directory"></a>README
Feature flags plugin for Payload CMS v3. Manage feature toggles, A/B tests, and rollouts from your admin panel.
v0.x — minor versions may contain breaking changes until 1.0.
feature-flags collection with on/off state, percentage rollouts, weighted variants, tags and metadata/admin/feature-flags-overviewread access can stay closedpayload and @payloadcms/ui ^3.56.0react ^19.1.0pnpm add @xtr-dev/payload-feature-flags
Add the plugin to your Payload config:
import { buildConfig } from 'payload'
import { payloadFeatureFlags } from '@xtr-dev/payload-feature-flags'
export default buildConfig({
// ...
plugins: [payloadFeatureFlags()],
})
Then regenerate the admin import map, since the plugin adds admin components:
pnpm payload generate:importmap
0.3.0 corrects the FeatureFlag type exported from the root entry point. It used to describe a stored document (id, createdAt, updatedAt), but declared an environment field the plugin never adds and left out the null Payload returns for empty optional fields. Only types change; nothing behaves differently at runtime.
FeatureFlag from @xtr-dev/payload-feature-flags is now the type the helpers and hooks return, the same one /rsc and /client already exported. It has no id, createdAt, updatedAt or environment.FeatureFlag for documents read with payload.find or the REST API, switch to the new FeatureFlagDocument, and handle null for description, rolloutPercentage, tags and variants (for example flag.rolloutPercentage ?? 100).collectionOverrides.fields (such as environment), use the FeatureFlag type from your own generated payload-types.ts.See Types.
0.2.0 changes how users are bucketed (#54):
rolloutPercentage: 0 now means nobody. Before, it let everyone in. Set it to 100, or clear it, if you relied on that.isUserInRolloutBucket / selectVariantForUser (see Bucketing).The admin dashboard now points at the FeatureFlagsOverviewView export, so run payload generate:importmap after upgrading.
All options are optional.
| Option | Default | Description |
|---|---|---|
disabled | false | Skip the /admin/feature-flags-overview view and the public endpoint. The collection is still added, so the database schema stays the same. |
defaultValue | false | Default of the enabled field for new flags. |
enableRollouts | true | Add the rolloutPercentage field. |
enableVariants | true | Add the variants field. |
enableCustomListView | false | Replace the collection's list view with the dashboard. Merges with collectionOverrides.admin.components. |
publicEndpoint | false | Add GET /api/<collection>/evaluate. See Public endpoint. |
cache | false | Cache the RSC helpers across requests. See Caching. |
collectionOverrides | Any CollectionConfig property (slug, access, admin, hooks, versions, ...), plus fields: ({ defaultFields }) => Field[]. The slug defaults to feature-flags. |
payloadFeatureFlags({
collectionOverrides: {
slug: 'my-feature-flags',
access: {
read: ({ req: { user } }) => !!user,
update: ({ req: { user } }) => user?.role === 'admin',
},
fields: ({ defaultFields }) => [
...defaultFields,
{ name: 'expiresAt', type: 'date' },
],
},
})
The plugin records the collection slug in config.custom.payloadFeatureFlags.collectionSlug, which is where the server helpers read it from.
name (text, required, unique): the flag's identifierdescription (textarea)enabled (checkbox, required)rolloutPercentage (number 0-100, default 100): share of users who get the flag. Empty means everyone, 0 means nobody.variants (array of name, weight, metadata): weights are percentages. When they add up to less than 100, users in the remaining buckets get the first variant.tags (array of tag)metadata (JSON)rolloutPercentage and variants only show in the admin panel while the flag is enabled.
import { getPayload } from 'payload'
import config from '@payload-config'
import { isFeatureEnabled, getUserVariant } from '@xtr-dev/payload-feature-flags/rsc'
export default async function ProductPage({ userId }: { userId: string }) {
const payload = await getPayload({ config })
if (!(await isFeatureEnabled('new-product-page', payload))) {
return <LegacyProductPage />
}
const variant = await getUserVariant('product-layout-test', userId, payload)
return variant === 'layout-b' ? <LayoutB /> : <LayoutA />
}
All helpers take the payload instance as their last argument, and are wrapped in React cache(), so repeated calls in one request read the database once.
| Function | Returns |
|---|---|
getFeatureFlag(name, payload) | The flag, or null |
isFeatureEnabled(name, payload) | boolean |
getAllFeatureFlags(payload) | Enabled flags keyed by name |
isUserInRollout(name, userId, payload) | boolean; false when the flag is disabled |
getUserVariant(name, userId, payload) | Variant name, or null when the flag is disabled or has no variants |
getFeatureFlagsByTag(tag, payload) | Flags with that tag |
A flag here is a FeatureFlag: name, enabled, and optional description, rolloutPercentage, variants, tags and metadata. Lookups that fail log the error and return null, {} or [].
'use client'
import { useFeatureFlag, useVariantSelection } from '@xtr-dev/payload-feature-flags/client'
export function Banner({ userId }: { userId: string }) {
const { isEnabled, loading } = useFeatureFlag('new-banner')
const { variant } = useVariantSelection('banner-copy-test', userId)
if (loading || !isEnabled) return null
return <p>{variant === 'short' ? 'Hi!' : 'Welcome back, good to see you!'}</p>
}
| Hook | Returns |
|---|---|
useFeatureFlags(initialFlags, options?) | { flags, loading, error, refetch } for the flags named in initialFlags, in the same order |
useFeatureFlag(name, options?) | { isEnabled, flag, loading, error } |
useSpecificFeatureFlag(name, options?) | { flag, loading, error, refetch } |
useVariantSelection(name, userId, options?) | { variant, flag, loading, error } |
useRolloutCheck(name, userId, options?) | { isInRollout, flag, loading, error } |
withFeatureFlag(name, Fallback?, options?)(Component) | A component that renders Component when the flag is on, else Fallback (or nothing) |
options (FeatureFlagOptions): serverURL (defaults to the page's origin), apiPath (/api), collectionSlug (feature-flags), and source:
'collection' (default): reads Payload's REST API for the collection, so visitors need read access. flag holds the whole flag, including description and tags.'public': reads the public endpoint. flag holds only name and enabled; variant and rollout are resolved on the server, and a missing flag shows as disabled instead of an error.The root entry point exports two types for a flag:
| Type | What it describes |
|---|---|
FeatureFlag | What the server helpers and client hooks return: name, enabled, and optional description, rolloutPercentage, variants, tags and metadata. Empty optional fields are undefined, never null. Also exported from /rsc and /client. |
FeatureFlagDocument | A document as Payload stores and returns it (payload.find, the REST API, collection hooks): the fields above plus id, createdAt and updatedAt. Empty optional fields are null, and array rows carry an id. |
import type { FeatureFlag, FeatureFlagDocument } from '@xtr-dev/payload-feature-flags'
const flag: FeatureFlag | null = await getFeatureFlag('new-dashboard', payload)
const { docs } = await payload.find({ collection: 'feature-flags' })
const documents: FeatureFlagDocument[] = docs
const percentage = documents[0].rolloutPercentage ?? 100 // null means no gradual rollout
Neither type includes fields added through collectionOverrides.fields. For those, use the FeatureFlag type in your generated payload-types.ts, which is assignable to FeatureFlagDocument.
To keep the collection's read access closed, turn on publicEndpoint and call the hooks with source: 'public':
payloadFeatureFlags({
publicEndpoint: true,
collectionOverrides: {
access: { read: ({ req: { user } }) => !!user },
},
})
const { isEnabled } = useFeatureFlag('new-homepage', { source: 'public' })
const { variant } = useVariantSelection('checkout-test', userId, { source: 'public' })
GET /api/<collection>/evaluate?names=a,b&userId=u returns { flags: { [name]: { name, enabled, variant?, inRollout? } } } (types PublicFeatureFlagsResponse and PublicFeatureFlag). It answers every name you pass (at most 100), and a name with no flag gets enabled: false, so the response does not reveal which flags exist. It never lists all flags. variant and inRollout are only included when userId is sent.
The endpoint is public by design: anyone can ask for the on/off state of a flag they can name, and for the variant and rollout result of any userId. Don't put secrets in flag names, and don't rely on users being unable to see other users' buckets.
isUserInRolloutBucket(flagName, userId, rolloutPercentage) and selectVariantForUser(flagName, userId, variants) are exported from the root and /client entry points. Use them to evaluate flags you fetched yourself, for example from the REST API, with the same result as the plugin:
import { isUserInRolloutBucket, selectVariantForUser } from '@xtr-dev/payload-feature-flags'
const { docs } = await fetch('/api/feature-flags?where[name][equals]=new-feature').then((r) => r.json())
const flag = docs[0]
const inRollout = flag?.enabled && isUserInRolloutBucket(flag.name, userId, flag.rolloutPercentage)
const variant = flag?.enabled ? selectVariantForUser(flag.name, userId, flag.variants) : null
Buckets come from a hash of the flag name and the user ID, so each flag splits users independently, and a flag's rollout and its variant assignment are independent of each other too.
The RSC helpers read the database on every request unless you turn on cache:
payloadFeatureFlags({
cache: true, // Next.js unstable_cache, tag 'payload-feature-flags', revalidate 60s
// or: cache: { tag: 'my-flags', revalidate: 300 },
})
The plugin appends afterChange and afterDelete hooks to the collection (after your own) that clear the cache, so a changed flag shows on the next request. This also works outside a request inside the Next.js process, such as Payload jobs, except before Next.js has served its first request (for example in onInit), where it logs a warning.
Changes the hooks cannot see stay cached until revalidate runs out: direct database writes, and writes from another process or server instance (the Payload CLI, seed scripts, a second app instance). That's why revalidate defaults to 60 seconds. Use revalidate: false only if every change goes through Payload in the same process, or clear the tag yourself with revalidateTag('payload-feature-flags', { expire: 0 }) (on Next.js 15, revalidateTag('payload-feature-flags')). Outside a Next.js server, lookups are not cached.
For another store (Redis, in-memory, ...) pass an object with wrap(key, load) and invalidate(). The FeatureFlagsCache type, createNextCache() and DEFAULT_CACHE_TAG are exported from the root entry point.
/admin/feature-flags-overview lists every flag with search (name, description, tags), sorting, totals (enabled, disabled, rolling out, A/B tests), and inline toggles for enabled and rolloutPercentage. It needs a logged-in user with read access to the collection; editing needs update access. With enableCustomListView: true the same dashboard replaces the collection's list view.
The views are exported from @xtr-dev/payload-feature-flags/views (FeatureFlagsView, FeatureFlagsOverviewView).
The collection is a normal Payload collection, so Payload's REST and GraphQL APIs work as usual and respect its access control:
curl 'http://localhost:3000/api/feature-flags?where[name][equals]=new-dashboard'
payload generate:importmap.read access to the collection, or use publicEndpoint with source: 'public'.cache on, a change from outside the app process shows after revalidate seconds. See Caching.pnpm install
pnpm dev # demo app at http://localhost:3000, SQLite, login [email protected] / test
pnpm test # Vitest
pnpm lint
pnpm build
See dev/README.md for the demo app.
MIT. See CHANGELOG.md for changes, and open an issue for bugs and questions.
Topics
payload plugin cache refresh
Admin dashboard button that refreshes Next.js cache tags for permitted users, with confirmation and optional page…
78385/wk0v3
payload plugin analytics
GA4-backed analytics dashboard in the Payload admin with a client-side tracking layer for Next.js front ends
70821/wk37v3
payload plugin ga4 ecommerce
GA4 reporting dashboards and per-record analytics tabs inside the Payload admin panel
44115/wk10v3
payload typesense
Typesense search for Payload with automatic sync, search endpoints, optional vector search and React search components
851.4k/wk33v3 v4
payload upstash
Upstash Redis key-value adapter for Payload's built-in KV store
82333/wk7v3 v4
payload posthog
Native PostHog integration for Payload Admin covering analytics, session replay, error tracking, logs and feature flags
791.7k/wk0v3