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

Copy 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>
);
}

Copy link to headingGuiding principles of the Flags SDK

Copy link to headingFeature Flags are functions

Copy link to headingFeature Flags are only ever evaluated on the server

Copy link to headingFeature Flags accept no arguments on the call side

Copy link to headingA primitive flag

const showBanner = false;

Copy link to headingFlag progressions and their tradeoffs

Copy link to headingBasic case

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

Copy 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

Copy link to headingUsing an environment variable

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

Copy link to headingUsing Edge Config

Copy 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(),
});

Copy 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),
});‍‌‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍‌‍‌‍‍‌‌‌‍‌‌‌‍‍‌‌‌‍‌‍‌‌‌‌‍‌‍‌‍‌‌‌‌‍‌‍​‍​‍​‍‍‌‍​‌‌‌‍‍‌‌‍​‍‌‍‌‍‌‍‌‌‍‌‌‌‍‌‌‍‍‌‌‌‌‍‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‍‍‌‌‍‍‌‍‌‌‌‌‌‌‍​‌‍‍‌‍‍‌‌‍‌‌‌‌‌‍‌‌‌‌‌‌‍‌‌‌

Copy link to headingA note on init time

Copy link to headingOpenFeature

Copy 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?