Skip to content
Guides
Build guide

How to add feature flags in Next.js

Feature flags in Next.js: gate the UI with WhenWorkspaceFeatureEnabled, check the flag again on the server, and know why a server component inside the gate still runs.

Dharmendra Jagodana8 min read

In short

Next.js feature flags come in two kinds. Workspace flags follow the subscription plan; user flags target chosen users, tags or lists. The SDK components decide what the browser displays, not what the server computes, so anything paid or private needs a second check against the workspace features map.

<WhenWorkspaceFeatureEnabled> hides the reports panel. In Next.js it does not stop the panel being built.

The reason is the App Router rather than anything BuildBase does. @buildbase/sdk/react ships with "use client" at the top of its entry, so the gate drops straight into a server component with no wrapper of your own. Pass a server component as its children and React renders those children on the server first, then hands the finished output to the gate as a slot. The gate decides whether to display the slot. By then the query has run, and the result has already travelled to the browser inside the RSC payload, where anyone who opens the network tab can read it.

So the question that decides your design is not which component to wrap things in. It is which side of the network the flag has to be honest on.

What you are building: an analytics panel that only workspaces on a paying plan see, a beta editor a handful of named users see, and an export action that refuses anyone whose flag is off, whatever the browser rendered.

Before you start

A Next.js App Router app with sign-in working and the bb() helper from BuildBase on Next.js, plus workspaces if you want the plan-tied half. whileUnknown and the entitlement components need @buildbase/sdk 1.0.0 or later. Snippets below were checked against @buildbase/sdk 1.8.0 and Next.js 14.2.35 on 24 November 2026, against the published types rather than run end to end in a live org, so treat the console steps as the part to confirm on your own screen.

Which gate you actually need

Pick this before you write any JSX, because they look identical at the render site and age completely differently. Feature flags and entitlements are not the same thing even when the same boolean renders both.

You want to sayUseWho changes it
Paying workspaces get analyticsWorkspace flagThe plan, when they subscribe
These twelve users get the new editorUser flagYou, in the console or the API
This workspace bought the analytics add-onEntitlementThe plan and its add-ons
Workspace admins may switch this onUser-managed workspace flagThe admin, in their settings

A workspace flag follows the subscription. Link it to a plan and it turns on when a workspace subscribes, and falls back to its default in a workspace that is canceled, unpaid or suspended, so a customer who stops paying stops seeing paid features without you writing a line. A user flag ignores plans entirely and targets people: named users, user tags, user lists, or audience lists over the API.

1. Create the flag and switch it on

In the console: Users in the left rail, then Features under Users for a user flag or under Workspaces for a workspace flag.

A new flag arrives switched off, and a flag that is off turns on for nobody. This is the step people skip and then spend twenty minutes debugging a correctly working gate. Switch it on (Enabled on a workspace flag, Enable on a user flag), then either attach it to a plan or set it to allow all workspaces.

You should see the flag listed with its Feature ID. That string, not the display name, is what goes in slug.

2. Gate the UI

// app/reports/page.tsx
import {
  WhenWorkspaceFeatureDisabled,
  WhenWorkspaceFeatureEnabled,
} from '@buildbase/sdk/react';

import { AdvancedAnalytics } from './advanced-analytics';

export default function ReportsPage() {
  return (
    <>
      <WhenWorkspaceFeatureEnabled slug="advanced-analytics">
        <AdvancedAnalytics />
      </WhenWorkspaceFeatureEnabled>

      <WhenWorkspaceFeatureDisabled slug="advanced-analytics">
        <p>Upgrade to turn analytics on.</p>
      </WhenWorkspaceFeatureDisabled>
    </>
  );
}

Four components cover the combinations: WhenWorkspaceFeatureEnabled, WhenWorkspaceFeatureDisabled, WhenUserFeatureEnabled and WhenUserFeatureDisabled. All of them take slug and children.

Pairing the enabled gate with the disabled one is worth the extra four lines. A feature that is off should usually say something, and an empty space where a panel used to be reads as a bug to the person looking at it.

After this step, a workspace on the right plan sees the panel and one without it sees the upgrade line. Flip the flag in the console, reload, and watch it change.

3. Check it again on the server

Here is the part the opening was about. If AdvancedAnalytics is a server component that queries something, or if a server action does work behind that panel, the gate above has not protected either of them.

// app/reports/actions.ts
'use server';

import { bb } from '@/lib/buildbase';

export async function exportReport(workspaceId: string) {
  const ws = await bb().workspace.get(workspaceId);

  if (!ws.features['advanced-analytics']) {
    throw new Error('Analytics is not enabled for this workspace');
  }

  // ...build the export
}

IWorkspace.features is a plain Record<string, boolean>, so the server check is one lookup against the same slug the component uses. Keep the slug in a constant and import it in both places. Two string literals that have to match is a typo waiting to happen, and a mistyped slug fails quietly: an unknown slug is just an unknown value, not an error.

Why bother, when the button is hidden? Because Next.js server functions are reachable by direct POST request, not only through your UI. The same reasoning that puts a permission check inside every server action puts the flag check there too.

For something you actually sell, reach for the entitlement API instead of the flag map: bb().entitlements.check(...) answers one slug with the plan's reasoning, including add-ons, and useEntitlement is its client half.

4. Target a beta with a user flag

User flags are set per person, not per plan, which makes them the right tool for an early-access list.

'use client';

import { useUserFeatures } from '@buildbase/sdk/react';

export function EditorEntry() {
  const { isFeatureEnabled, loading } = useUserFeatures();

  if (loading) return <EditorSkeleton />;
  return isFeatureEnabled('beta-editor') ? <BetaEditor /> : <Editor />;
}

Use the hook rather than WhenUserFeatureEnabled when you need the value outside JSX, when you are reading several flags at once, or when you want to handle loading yourself. That last one matters more than it sounds. The components carry no loading state of their own, so until the flags arrive they render as though the flag were off, which is why the SDK documents reading loading beside them as an edge case. Around a large panel that reads as a layout jump once the session resolves. Reserving the space with a skeleton costs three lines and looks deliberate.

The hook also hands you refetch(), which is how a flag change shows up without a page reload. You will want it if your support team flips a flag while the customer is on the phone.

Who is in the beta is a console change, and you can make the same change from your own code with an org API token: PATCH /api/features/:id/users/add, with matching routes for user lists, audience lists and tags, and a /remove for each. A sign-up flow can put someone into a beta without anyone opening a browser. Adding a target needs a plan that includes advanced flag targeting. Removing one never does, so a plan without it can always hand a flag back to everybody.

5. Let workspace admins flip their own

Mark a workspace feature as user-managed and it appears on a built-in settings screen, no UI from you:

'use client';

import { useSaaSAuth } from '@buildbase/sdk/react';

export function FeatureSettingsLink() {
  const { openWorkspaceSettings } = useSaaSAuth();
  return (
    <button onClick={() => openWorkspaceSettings('features')}>
      Manage features
    </button>
  );
}

To write the value yourself instead, updateFeature(workspaceId, key, value) from useSaaSWorkspaces() does it in the browser and bb().features.update(workspaceId, key, value) does it on your server.

One rule governs both: a request naming only features the workspace is not allowed to change comes back 403 FEATURE_NOT_USER_MANAGED with the offending slugs listed in features. Mixed in beside features it can change, those are skipped quietly and named in the response's ignoredFeatures. Read that field. A partial success that looks like a success is how a settings screen ends up lying to the person using it.

What breaks

The flag is on and nothing appears. Check the Feature ID rather than the display name, and check the flag's own enabled switch. A flag attached to a plan but left off is off for everyone, which looks exactly like a slug typo.

A new flag hides the feature from everybody. A workspace that loaded before the flag existed has no value for that slug, and whileUnknown defaults to 'deny'. Correct for a paid panel. Wrong for a cosmetic tweak, where you want whileUnknown="allow":

<WhenWorkspaceFeatureEnabled slug="new-dashboard" whileUnknown="allow">
  <NewDashboard />
</WhenWorkspaceFeatureEnabled>

The change does not reach an open tab. Flags ride the session, so a flip lands on the next page load or session refresh. How long a flag takes to reach a user is the question to ask any flag vendor, and the honest answer here is that a tab left open has no upper bound. That is fine for a permission flag and useless for a kill switch, so do not build a kill switch on one. refetch() is the escape hatch for user flags.

The gated server component ran anyway. Covered in step 3, and worth repeating because it fails silently: the gate controls display, not execution. Anything expensive or private needs the server check.

You wanted 10% of traffic. There is no percentage rollout and no experiment analysis here. Target users, lists or tags, or tie the feature to a plan. If your release process depends on a gradual percentage ramp, use a tool built for that, and keep these for plan gating and named betas.

Flags are loaded with the session and cached locally, so none of this costs an API call per render. OnceBooks runs feature flags as one of its fourteen BuildBase modules, next to credits, workflows and email campaigns, which is the shape this is built for: plan gating and named betas, in an app that already has sessions.

One last thing, and it is the one that actually bites six months out. Give every temporary flag an owner and a removal date when you create it. A flag is not debt; forty of them that nobody dares delete is.

npm i @buildbase/sdk
feature-flags
nextjs
releases

Frequently Asked Questions

How fast does a feature flag change reach a user in Next.js?

On their next page load or session refresh. Flags are fetched with the session and cached locally, so there is no API call per render and no deploy to flip one. There is also no upper bound while a tab sits open: a user who leaves your app running sees the old value until something reloads the session. Call refetch() from useUserFeatures() if you need a user flag to update without a reload.

Does BuildBase support percentage rollouts or A/B tests?

No. You target specific users, user lists or tags, or tie a feature to a subscription plan. There is no percentage rollout and no experiment analysis, so if you need to ship to 5% of traffic and read significance off a dashboard, this is the wrong tool and you should say so before you build on it. Conversion tracking is yours to do, through workflows or your own analytics.

Why does my new flag hide the feature for everybody?

Because a workspace that loaded before the flag existed has no value for that slug, and WhenWorkspaceFeatureEnabled defaults whileUnknown to deny. That is the safe default for a paid feature and the wrong one for a cosmetic change. Pass whileUnknown="allow" on the gate when hiding is worse than showing. A brand new flag is also switched off until you enable it in the console.

Should a paid feature use a feature flag or an entitlement?

An entitlement. A workspace flag tied to a plan works, but useEntitlement and WhenEntitled resolve from the plan itself, follow add-ons, and tell a locked feature apart from one the plan never mentions. They need @buildbase/sdk 1.0.0 or later. Either way the browser check is a suggestion: the check that protects the feature runs on your server.

Ship it

Create a project and run this guide against your own workspace.

Free plan7-day free trial on paid plansNo credit card required