how-to

How to Set Up Feature Flags in Next.js - A Practical 2026 Guide

A hands-on guide to adding feature flags to a Next.js app, from your first server-side toggle to edge evaluation and safe rollouts, with the tools that fit the App Router.

Published:

Every Next.js feature-flag tutorial shows you the same three-line snippet - import an SDK, check a boolean, render a component. That part is easy. The part that bites is Next.js-specific: should the flag resolve on the server or the client, and how do you avoid the flash of the wrong variant when it resolves in the wrong place? Get that decision right and flags in Next.js are clean. Get it wrong and you ship a visible flicker on every flagged page.

This is a practical guide for the App Router era. It is tool-agnostic on the process, and I name specific tools where they concretely fit.

Step 1 - Decide server or client first

This is the decision that matters most in Next.js, so make it before you install anything.

  • Server-side evaluation happens in a Server Component, a route handler, or middleware. The flag resolves before HTML leaves your server, so the browser receives the correct variant already rendered. No flicker, and your SDK key never reaches the client.
  • Client-side evaluation happens after hydration in a Client Component. It is the right choice only for flags that gate an interaction the user triggers later - a button behaviour, a modal - not for anything visible on first paint.

The default should be server-side. In the App Router, Server Components make this the natural path, and it removes the single most common Next.js flag bug in one move. Reach for client evaluation only when the flag genuinely depends on runtime user action.

Step 2 - Pick a tool that fits your evaluation model

All three tools below have a Next.js or React SDK. The difference that matters is how they bill and where they evaluate.

  • Statsig lists a dedicated Next.js SDK, and its billing model suits Next.js apps that serve real traffic - flag and config checks are unlimited and free on every tier, so evaluating flags on every request costs nothing. It monetises analytics events instead. The free Developer tier gives 2M events a month with no credit card, and Pro is a flat $150 a month.
  • ConfigCat has open-source JavaScript and Node SDKs that cache flag config locally. It charges no per-seat and no per-MAU fee, so a high-traffic Next.js app pays the same as a small one, as long as config-download volume stays in tier. The Forever Free tier gives 10 flags and 5M config downloads a month.
  • LaunchDarkly is the pick if you need the deepest targeting and progressive delivery. It has around 38 SDKs including React and edge, and a free Developer tier with core targeting and unlimited seats. Just price it on your highest-volume context kind first, because it bills client-side MAU at $8.33 per 1,000 and devices can dwarf users.

Each tool links to our full review with the pricing detail.

Step 3 - Initialise the SDK once, on the server

The performance trap in Next.js is re-creating the SDK client on every request. Don’t. Initialise it once per server instance and reuse it, so flag config is fetched once and cached in memory. After that, a flag check is a local lookup, not a network call.

In practice that means creating the client in a module that is imported by your Server Components or route handlers, guarded so it only initialises a single time per process. On serverless or edge, the same principle applies per warm instance - the cold-start config fetch is the cost you are minimising by keeping the client alive.

Step 4 - Evaluate in a Server Component

With the client initialised, resolve the flag where the page is rendered. In a Server Component you evaluate the flag, pass the boolean down as a prop, and render the correct branch. The HTML that reaches the browser already reflects the decision, so there is nothing to flicker.

Pass a stable identifier for the user or session into the evaluation call, because that is what targeting and percentage rollouts key off. Without a consistent identifier, a percentage rollout will bounce a user between variants on every request. Use a signed cookie or your session ID, not a value that changes per request.

Step 5 - Use middleware for edge routing decisions

Some flags decide routing rather than UI - which landing page to serve, whether a route exists yet. Those belong in Next.js middleware, which runs at the edge before the request reaches your page. Evaluate the flag there and rewrite or redirect accordingly.

This is where edge SDK support earns its place: the closer the flag config lives to the middleware, the lower the added latency. LaunchDarkly’s edge coverage and ConfigCat’s local caching both keep this cheap. Keep middleware evaluation limited to routing - it runs on every matched request, so heavy logic there taxes every page load.

Step 6 - Roll out in stages, keep the kill switch

Once flags are wired in, use them properly. Ship the flag off, turn it on for yourself, then a small internal segment, then a small percentage of production traffic, watching your error and performance metrics at each step. Widen only when the metrics hold.

The point of a flag is that you can turn it off without a redeploy. Keep every release flag as a kill switch until the feature is proven, so a bad rollout is a dashboard toggle, not an incident and a deploy. For the broader rollout discipline, our feature flag best practices guide goes deeper.

Step 7 - Clean up when the feature is permanent

A flag that has been at 100% for a month is not a flag any more - it is dead code with a config entry. Remove the flag, the branch it gated, and the config, and do it on a schedule. Next.js apps accumulate flag debt exactly like any other codebase, and the flags-in-Server-Components pattern makes stale branches easy to forget. Our guide on how to clean up feature flags covers the process.

The short version

  • Default to server-side evaluation in Server Components or middleware - it kills the flash of the wrong variant and keeps your SDK key off the client.
  • Initialise the SDK once per instance and reuse it, so flag checks are local lookups.
  • Pass a stable user identifier into every evaluation, or percentage rollouts will thrash.
  • Pick the tool for your scale: Statsig for free unlimited checks at high traffic, ConfigCat for flat headcount-independent pricing, LaunchDarkly for the deepest targeting.

Feature flags in Next.js are not hard once the server-versus-client question is settled. Settle it first, wire the SDK once, and the rest is the same disciplined rollout and cleanup you would run anywhere. For the ground-up version across any framework, see how to implement feature flags.

Frequently Asked Questions

Where should I evaluate feature flags in a Next.js app?

On the server whenever you can. In the App Router that means evaluating flags inside Server Components, route handlers or middleware, so the decision is made before HTML reaches the browser and you avoid a flash of the wrong variant. Evaluate in the client only for interactions that genuinely happen after hydration. Server-side evaluation is also safer, because your SDK key and targeting rules never ship to the browser.

How do I avoid a flash of the wrong content with feature flags in Next.js?

The flash happens when the client renders a default, then re-renders once the flag resolves. The fix is to resolve the flag on the server before the page is sent, so the correct variant is baked into the HTML. In the App Router, evaluate in a Server Component or middleware. If you must evaluate on the client, gate the flagged UI behind a loading state rather than rendering the default first.

Do feature flags slow down a Next.js app?

Not if you evaluate them right. Most SDKs cache flag configuration in memory and evaluate locally, so a flag check is a map lookup, not a network round trip. The latency risk is the initial config fetch on a cold server or edge function. Initialise the SDK once per server instance, reuse it, and prefer tools with edge support so the config lives close to your functions.

Which feature flag tool works best with Next.js?

All the major tools have a Next.js or React SDK, so the real question is your billing and evaluation model. Statsig lists a dedicated Next.js SDK and never charges for flag checks, which suits a high-traffic app. ConfigCat charges no per-seat or per-MAU fee and its SDKs cache locally, which keeps a chatty app cheap if you cache well. LaunchDarkly has the deepest targeting and around 38 SDKs if you need enterprise depth. Match the tool to your scale and budget, not to a logo.

Explore More

Free Newsletter

Get the Feature Flags Newsletter

Platform benchmarks, real pricing data and progressive delivery practice. No spam.

Free. Unsubscribe any time. See our privacy policy.

Related Articles