Server-side and client-side feature flags for Next.js applications
Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.
npm install flagbit-client
// pages/dashboard.js — Server-side flag evaluation
export async function getServerSideProps(context) {
const userId = context.req.cookies.userId || 'anonymous';
const res = await fetch(
'https://flagbit.anethoth.com/api/v1/evaluate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-SDK-Key': process.env.FLAGBIT_SDK_KEY
},
body: JSON.stringify({
flag_key: 'new-dashboard-layout',
context: { user_id: userId }
})
});
const { value } = await res.json();
return { props: { useNewLayout: value } };
}
export default function Dashboard({ useNewLayout }) {
return useNewLayout ? <NewDashboard /> : <ClassicDashboard />;
}
// For App Router (Next.js 13+)
// app/dashboard/page.js
async function getFlag(key, ctx) {
const res = await fetch(
'https://flagbit.anethoth.com/api/v1/evaluate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-SDK-Key': process.env.FLAGBIT_SDK_KEY
},
body: JSON.stringify({ flag_key: key, context: ctx }),
next: { revalidate: 60 } // Cache for 60s
});
return (await res.json()).value;
}
export default async function DashboardPage() {
const showBeta = await getFlag('beta-features', {});
return <Dashboard showBeta={showBeta} />;
}
Evaluate flags at the edge in getServerSideProps — no client-side flicker, no loading states.
Use Next.js middleware to redirect users based on feature flags before the page even renders.
Roll out new App Router pages alongside Pages Router — flag controls which version users see.
Enable experimental features only in Vercel preview deployments via environment-based flag rules.
Define flags in your FlagBit dashboard with targeting rules and rollout percentages.
Call the evaluate endpoint from your Next.js app with user context for targeted rollouts.
Enable, disable, or adjust rollouts in real-time. No redeployment needed.
Server-side avoids UI flicker and is better for SEO-sensitive pages. Client-side is fine for interactive features where a brief loading state is acceptable.
Use the fetch cache option: next: { revalidate: 60 } to cache flag evaluations for 60 seconds in the App Router.
Yes — evaluate flags in getStaticProps with revalidate. The flag value updates on each ISR regeneration.
FlagBit's API works from Vercel Edge Functions. Use the evaluate endpoint directly — no SDK needed.
Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.
Get Your Free API Key