Feature flags let you roll out product changes to specific users, test features internally, and run A/B tests before a full release. The Flags SDK and Vercel Toolbar give you control over which content your users see.
This guide shows you how to set up the Flags SDK in a Fumadocs project with the Vercel Toolbar. Fumadocs is a popular framework for building documentation sites.
You'll build an application that:
- Toggles content visibility within MDX pages
- Hides entire pages behind a feature flag
- Removes flagged pages from the sidebar
- Tests flag states using the Vercel Toolbar
Copy link to headingPrerequisites
Before you begin, make sure you have:
Copy link to headingInstall and configure
To use the Flags SDK and Vercel Toolbar in your project, install the following packages:
Then update your next.config.js file to wrap the default MDX config with the Vercel Toolbar:
Copy link to headingAdd the toolbar for testing in dev
Update your root layout file to conditionally render the Vercel Toolbar in development:
Copy link to headingDefine a feature flag
Create a new file called flags.ts at your project root:
This code defines:
key: A unique identifier for the flag that you reference in codedefaultValue: The initial state of the flag. This value must be a type that thedecidefunction can return. For a boolean flag, the default is typicallyfalsedecide: A function that determines which value a user sees. It can return any value that matches the type ofdefaultValue
Copy link to headingCreate a discovery endpoint for the flag
The Vercel Toolbar discovers available flags through an API route. Create the endpoint with the following code:
Copy link to headingHide inline content in MDX
You can hide parts of your documentation from certain users with a feature flag. Create a component called InternalContent:
Register it as a globally available component in all MDX files:
Import your new component in an MDX file, then add content you want hidden when the flag is disabled:
Copy link to headingHide entire pages and navigation items
Open your source.config.ts file and extend the Fumadocs schema to add a new property to the MDX pages frontmatter fields:
Copy link to headingProtect the page route
Check the flag in your page component and return a 404 if the flag is disabled:
Copy link to headingHide the page in the navigation
When you set the internal property in your MDX pages' frontmatter, it hides the page from navigation, preventing users from landing on a 404. This code filters out internal pages from the sidebar by removing those pages from the tree:
This code:
- Gets all pages marked with
internal: true - Creates a
Setof their URLs - Recursively filters the page tree:
- Removes pages in the internal URLs set
- Removes folders that become empty after filtering
- Handles folder index pages separately
Copy link to headingMark the page as internal
Add internal: true to any MDX page's frontmatter. This will hide the page is the flag is set to true:
Copy link to headingTest locally using the Vercel Toolbar
When running your project locally, you’ll see the toolbar as a widget floating above the UI. To test your flag locally, open the flags explorer from the toolbar and:
Navigate to the page you added the InternalContent component to, you should see the banner you added that marks the page as internal to your team.