The Unleash Next.js SDK lets you evaluate feature flags in Next.js applications. It connects to Unleash or Unleash Edge to fetch evaluated flags for a given Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
There is an ./example project that you can deploy to Vercel or edit in CodeSandbox.
This package will attempt to load configuration from Next.js Environment variables.
When using Unleash client-side, with <FlagProvider /> or getFrontendFlags() configure:
NEXT_PUBLIC_UNLEASH_FRONTEND_API_URL. URL should end with /api/frontend or /proxyNEXT_PUBLIC_UNLEASH_FRONTEND_API_TOKEN client-side API token
if you’re using the Frontend API./api/frontend.If using server-side (SSR, SSG, API), using getDefinitions() and evaluateFlags(), set:
UNLEASH_SERVER_API_URL of you instance. URL should end with /apiUNLEASH_SERVER_API_TOKEN Backend API client tokenIf you plan to use configuration in the browser, add NEXT_PUBLIC_ prefix.
If both are defined and available, private variable takes priority.
You can use both to have different values on client-side and server-side.
UNLEASH_SERVER_INSTANCE_ID instead of UNLEASH_SERVER_API_TOKEN to authorize with GitLab’s service.This package is ready for server-side use with App Router.
Refer to ./example/README.md#App-router for an implementation example.
It’s possible to run this SDK in Next.js Edge Middleware. This is a great use case for A/B testing, where you can transparently redirect users to different pages based on a feature flag. Target pages can be statically generated, improving performance.
Refer to ./example/README.md#Middleware for an implementation example.
Fastest way to get started is to connect frontend directly to Unleash. You can find out more about direct Frontend API access in our documentation, including a guide on how to setup a client-side SDK key.
Important: Hooks and provider are only available in @unleash/nextjs/client.
With <FlagProvider /> in place you can now use hooks like: useFlag, useVariant, or useFlagsStatus to block rendering until flags are ready.
Optionally, you can configure FlagProvider with the config prop. It will take priority over environment variables.
If you only plan to use Unleash client-side React SDK now also works with Next.js. Check documentation there for more examples.
With same access as in the client-side example above you can resolve Unleash feature flags when building static pages.
The same approach will work for ISR (Incremental Static Regeneration).
Both getDefinitions() and getFrontendFlags() can take arguments overriding URL, token and other request parameters.
You can bootstrap Unleash React SDK to have values loaded from the start. Initial value can be customized server-side.
The Developer Toolbar provides runtime flag and context overrides during development. It supports both client components and server-side rendering through cookie-based state sync.
Use UnleashToolbarProvider from @unleash/toolbar/next as a drop-in replacement for FlagProvider:
The toolbar syncs state to cookies, which server components can read using applyToolbarOverrides:
Refer to the Developer Toolbar documentation for configuration options and the complete Next.js example on GitHub.
Next.js applications using Server-Side Rendering (SSR) are often deployed in serverless or short-lived environments, such as Vercel. This creates challenges for server-side metrics reporting.
Typically, Unleash backend SDKs (like the Node.js SDK run in long-lived processes, allowing them to cache metrics locally and send them to the Unleash API or Edge API at scheduled intervals.
However, in some short-lived serverless environments where Next.js is commonly hosted (e.g., Vercel), there is no persistent in-memory cache across multiple requests. As a result, metrics must be reported on each request.
To address this, the SDK provides a sendMetrics function that can be called wherever needed, but it should be executed after feature flag checks client.isEnabled() or variant checks client.getVariant().
We also recommend setting up the Edge API in front of your Unleash API. This helps protect your Unleash API from excessive traffic caused by per-request metrics reporting.
If your runtime does not allow setInterval calls then you can report metrics on each request as shown below. Consider using Unleash Edge in this scenario.
Latest versions of Next.js allow to run code after the response is sent, with after function.
In middleware.ts you can use event.waitUntil() to reliably send metrics without delaying the response.
If your Next application resolves flags only in SSR mode and setInterval is supported then you may also consider using Node.js SDK instead, which is less taxing on metrics reporting.
Check this blog post for more information on short-running lambdas that still support setInterval.
You can use unleash [action] [options] in your package.json scripts section, or with:
For the CLI to work, the following environment variables must be configured with appropriate values:
UNLEASH_SERVER_API_URLUNLEASH_SERVER_API_TOKENThe CLI will attempt to read environment values from any .env files if they’re present. You can also set the variables directly when invoking the interface, as in the CLI usage example.
get-definitions <outputFile.json> Download feature flags definitions for bootstrapping (offline use) of server-side SDK.generate-types [options] <outputFile.ts> Generate types and typed functions from feature flags defined in an Unleash instance.
It will also generate strictly typed versions of useFlag, useVariant, useFlags and flagsClient (unless --typesOnly is used).
-t, --typesOnly don’t include typed versions of functions exported from @unleash/nextjs (default: false)-b, --bootstrap <sourceFile.json> load definitions from a file instead of fetching definitions - work offline-V Output the version numberTry it now