Skip to content
Dashboard

Flags as code in Next.js

Software Engineer

Exploring the usage of feature flags in Next.js with Vercel's Flags SDK.

Try the template

Jump right in with this ecommerce example that shows how to use the Flags SDK for Next.js applications. Deploy the template with one click.

Get started

Link to headingBasic start

flags.ts
import { flag } from "flags/next";
export const showBanner = flag({
key: "banner",
decide: () => false,
});

app/page.tsx
import { showBanner } from "../flags";
export default async function Page() {
const banner = await showBanner();
return (
<div>
{banner ? <Banner /> : null}
{/* other components */}
</div>
);
}

Link to headingGuiding principles of the Flags SDK

Link to headingFeature Flags are functions

Link to headingFeature Flags are only ever evaluated on the server

Link to headingFeature Flags accept no arguments on the call side

Link to headingA primitive flag

const showBanner = false;

Link to headingFlag progressions and their tradeoffs

Link to headingBasic case

flags.ts
import { flag } from "flags/next";
export const showBanner = flag({
key: "banner",
decide: () => false,
});

Link to headingOverriding from Vercel Toolbar

We were able to use ‌‌‍‌the Flags SDK and Vercel's Flags Explorer to override a flag and place real-life orders in production before revealing a set of new payment methods to our customers. This setup gives us peace of mind and makes releasing new features almost boring as we've already seen them working behind the flag.
Phil Wolstenholme Lead Frontend Engineer at Co-op

Link to headingUsing an environment variable

export const showBanner = flag({
key: "banner",
decide: () => process.env.SHOW_BANNER === "1",
});

Link to headingUsing Edge Config

Link to headingA primer on Edge Config

{
"flags": {
"banner": true,
"sale": false
}
}

flags.ts
import { flag } from “flags/next”;
import { get } from “@vercel/edge-config”;
export const showBanner = flag({
key: “banner”,
defaultValue: false,
async decide() {
// educational example, use @flags-sdk/edge-config for real applications
const flags = await get(“flags”);
return flags?.banner;
}
});

flags.ts
import { flag } from 'flags/next';
import { edgeConfigAdapter } from '@flags-sdk/edge-config';
export const showBanner = flag({
// Will get the `example-flag` key from the `flags` object
key: 'banner',
// Will load the `flags` key from Edge Config
adapter: edgeConfigAdapter(),
});

Link to headingUsing a feature flag provider

// flags.ts
import { flag } from "flags/next";
import { statsigAdapter } from "@flags-sdk/statsig";
export const showBanner = flag({
key: "banner",
adapter: statsigAdapter.featureGate((gate) => gate.value),
});‍‌‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍‌‍‌‍‍‌‌‌‍‌‌‌‍‍‌‌‌‍‌‍‌‌‌‌‍‌‍‌‍‌‌‌‌‍‌‍​‍​‍​‍‍‌‍​‌‌‌‍‍‌‌‍​‍‌‍‌‍‌‍‌‌‍‌‌‌‍‌‌‍‍‌‌‌‌‍‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‍‍‌‌‍‍‌‍‌‌‌‌‌‌‍​‌‍‍‌‍‍‌‌‍‌‌‌‌‌‍‌‌‌‌‌‌‍‌‌‌

Link to headingA note on init time

Link to headingOpenFeature

Link to headingGet started today

Check out the SDK

Start experimenting server-side, with better performance and less jank.

View the docs

Ready to deploy?