Integrate FlagSync feature flags into Next.js applications with Vercel Flags.
@flagsync/nextjs-sdk
integrates into Next.js applications with Vercel Flags for feature management and event tracking—ideal for SSR and static site generation workflows.
Initialize the Client
client
and export the adapter
helper to be used in your flag definition.Define Your Feature Flag
flag()
function to define your feature flag, and connect it to the adapter
.Use the Flag in a Server Component
identify
function that returns an FsUserContext
object. Pass this function to Vercel’s flag()
function.
key
in FsUserContext
is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.Create a Helper Function
identify
with the createIdentify
helper, linking flags to user contexts. We’ll create getFlagSyncUserContext
next.Set Up Identification
key
is set using a persistent userId
or visitorId
from cookies, falling back to a generated ID with nanoid()
. For proper MAU tracking and consistent flag evaluations, ensure this key
is unique and persistent across requests—see User Context Best Practices. Set Cookies in Middleware (Optional)
getUserContextKey
.flag
function, which applies targeting rules, rollouts, and defaults for the user context:
track
function:
SDK_UPDATE
: Emitted when flags are updatedSDK_READY
: Emitted when the SDK is readyERROR
: Emitted when an error occurs during initializationSDK_UPDATE
does not fire if syncing is disabled.FsConfig
interface:
key
in FsUserContext
is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.sync
object: stream
, poll
, or off
.
Stream (Default)
Polling
Off
stream
/poll
/off
) based on your application’s needs.FLAGSYNC_SDK_KEY
: Your server-side FlagSync SDK key (required)