Skip to content

Feature flags in Next.js (App Router): server components, middleware and static pages

How to use feature flags in Next.js with the App Router: evaluate in Server Components, precompute in proxy.ts to keep pages static, and avoid flicker. Tested Flags SDK code on Next.js 16 and a comparison of vendor Next.js SDKs.

6 min read

Short answer: In the Next.js App Router, evaluate flags on the server, in Server Components, route handlers or proxy.ts (the file Next.js 16 uses for middleware), and send finished HTML, so users never see the wrong version flash. Vercel's open-source Flags SDK and most vendors' Next.js SDKs work this way; to keep pages static, precompute flags in the proxy and serve a cached variant.

Where can I evaluate flags in Next.js?

Where Good for Trade-off
Server Component Most flags: the page renders with the right values With the Flags SDK, calling a flag reads headers and cookies, so the route renders on every request
proxy.ts (middleware) + precompute Static or ISR pages with a few variants Needs a flag group and a dynamic route segment; more setup
Route handler or server action API responses, form handling Same as any server code
Client Component Interactive UI after load Flags load after hydration: plan for the first render (see feature flags in React)

Evaluate on the server whenever you can, and pass the values to client components as props, or bootstrap the client SDK with them.

How do I read a flag in a Server Component?

With the Flags SDK (flags 4.4.0), a flag is a function you await. We built and ran this on Next.js 16.4.6 with next build and next start, outside Vercel: without the cookie the page shows "Old checkout", and with plan=pro it shows "New checkout". decide receives the request's headers and cookies.

// flags.ts
import { flag } from 'flags/next'

export const newCheckout = flag<boolean>({
  key: 'new-checkout',
  defaultValue: false,
  decide({ cookies }) {
    return cookies.get('plan')?.value === 'pro'
  },
})
// app/checkout/page.tsx
import { newCheckout } from '../../flags'

export default async function Page() {
  const enabled = await newCheckout()
  return <p>{enabled ? 'New checkout' : 'Old checkout'}</p>
}

decide is where you'd call your flag tool. Instead of writing it yourself, you can pass an adapter from a provider package: there are adapters for Vercel Flags, LaunchDarkly, Statsig, GrowthBook, PostHog, Flagsmith, Optimizely, Split, Reflag and OpenFeature, among others (providers). The Flags SDK is a library, so it runs on any Next.js host; Vercel-only parts include the Vercel Flags service, Global Config (formerly Edge Config) storage and the Flags Explorer in the Vercel Toolbar.

How do I keep pages static with flags?

Calling a flag in a page makes it dynamic. The Flags SDK's precompute pattern evaluates flags in proxy.ts, encodes the results into a short code, and rewrites the request to a route with that code, so each combination of flag values is a separately cached static page (precompute). We ran this on Next.js 16.4.6: the default and plan=pro requests got different variants; the first request for each variant rendered it and later requests came from cache (x-nextjs-cache: HIT).

// flags.ts
import { flag } from 'flags/next'

export const newBanner = flag<boolean>({
  key: 'new-banner',
  defaultValue: false,
  decide({ cookies }) {
    return cookies.get('plan')?.value === 'pro'
  },
})

export const marketingFlags = [newBanner] as const
// proxy.ts
import { type NextRequest, NextResponse } from 'next/server'
import { precompute } from 'flags/next'
import { marketingFlags } from './flags'

export const config = { matcher: ['/'] }

export async function proxy(request: NextRequest) {
  const code = await precompute(marketingFlags)
  const url = new URL(`/${code}${request.nextUrl.pathname}${request.nextUrl.search}`, request.url)
  return NextResponse.rewrite(url, { request })
}
// app/[code]/page.tsx
import { notFound } from 'next/navigation'
import { marketingFlags, newBanner } from '../../flags'

export async function generateStaticParams() {
  return []
}

export default async function Page({ params }: { params: Promise<{ code: string }> }) {
  const { code } = await params
  let showBanner: boolean
  try {
    showBanner = await newBanner(code, marketingFlags)
  } catch {
    notFound()
  }
  return <p>{showBanner ? 'New banner' : 'Old banner'}</p>
}

Notes on this pattern:

  • The secret. Precompute needs a FLAGS_SECRET environment variable: 32 random bytes, base64url-encoded, for example from node -e "console.log(crypto.randomBytes(32).toString('base64url'))". It also encrypts Flags Explorer overrides.
  • Invalid codes. The [code] route is public; without the try/catch, an invalid code returns a 500 error, and with it a 404.
  • When variants are built. Returning [] from generateStaticParams renders each variant on its first request and caches it. To build all variants at build time, return (await generatePermutations(marketingFlags)).map((code) => ({ code })).
  • Variant count. Every flag in the group multiplies the number of variants, so keep precomputed groups small.
  • Matched paths. matcher: ['/'] only rewrites the home page; other paths need their own matcher entries and matching routes under app/[code]/.
  • Not for private content. The code is a signed token in the URL, not a secret, so don't use precompute to gate anything private.

Which vendors have Next.js SDKs?

From each vendor's documentation and npm, checked on 15 November 2026.

Tool Next.js approach Package
Vercel Flags and any provider Flags SDK: server evaluation, precompute, adapters flags 4.4.0, @flags-sdk/* adapters
Unleash Server-side flags in Server Components (getDefinitions, evaluateFlags, flagsClient); client hooks from /client @unleash/nextjs 1.6.7
DevCycle setupDevCycle exports server getVariableValue; client provider and hooks @devcycle/nextjs-sdk 3.1.1
LaunchDarkly React SDK with React Server Component support (createLDServerSession), or the Vercel edge SDK, or the Flags SDK adapter @launchdarkly/react-sdk 4.2.3
Statsig StatsigBootstrapProvider bootstraps the client from the server @statsig/next 3.33.8
PostHog Server flags with posthog-node, client hooks from @posthog/react; the pre-release @posthog/next adds bootstrapFlags, which makes pages under it dynamic posthog-node, posthog-js, @posthog/react; @posthog/next 0.11.0 (pre-release)
GrowthBook JS SDK in Server Components, a new instance per request @growthbook/growthbook 2.0.0
Flagsmith getState() on the server, passed as serverState to the client provider @flagsmith/flagsmith 12.5.0
Optimizely Fetch the datafile in a server layout and pass it to the client provider @optimizely/react-sdk 4.3.0
ConfigCat No Next.js-specific SDK; JavaScript SDK or the Flags SDK through OpenFeature @configcat/sdk 1.1.0

Sources: Flags SDK for Next.js, Unleash Next.js, DevCycle Next.js, LaunchDarkly React Server Components, Statsig Next.js, PostHog Next.js and @posthog/next, GrowthBook App Router, Flagsmith Next.js, Optimizely Next.js, ConfigCat JavaScript SDK.

What goes wrong with flags in Next.js?

  • Sharing state between requests. Server code handles many users at once. GrowthBook's and Flagsmith's Next.js examples create a new instance per request, and Flagsmith warns that a shared instance can leak identities across concurrent server-rendered requests.
  • Pages turning dynamic by accident. Reading cookies or headers, which the Flags SDK does on every flag call, opts a page out of static rendering; use precompute for static pages.
  • Flicker in client components. If a client component fetches flags itself, it renders defaults first. Pass server-evaluated values down, or bootstrap the client SDK.
  • Lost analytics. Server SDKs that send events in the background need time after the response; Unleash's guide uses Next.js after() to send metrics.
  • Too many variants. Each precomputed flag multiplies the static variants for a page (a boolean flag doubles them); keep groups small and flags short-lived (see stale feature flags).

Which approach should I choose?

  • Want one API across vendors and good Next.js integration? The Flags SDK with your vendor's adapter, or with OpenFeature.
  • Already using a vendor with a Next.js SDK? Its SDK usually supports more of its features, such as experiments and events.
  • Static marketing pages with experiments: precompute in proxy.ts.

For evaluating flags at the edge generally, see edge feature flags; for plain Node.js servers, feature flags in Node.js. To pick a tool, see the best feature flag tools or the directory.