Feature Flags with Next.js and GrowthBook
Tags: nextjs, react, javascript, tutorial
Published: 2021-12-31 12:05:08

Deploying code to production is scary and stressful. There's nothing worse than seeing an unexpected spike or dip in your metrics as soon as your code goes live. Your heart is racing as you quickly revert the commit. The site is broken and there's nothing you can do but wait 5+ agonizing minutes for your CI/CD pipeline to finish. And that's the best case scenario when a simple code revert is possible. Need to deal with rolling back database or infrastructure changes? Good luck with that.

![Broken server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkloophh8enour3h4djf.jpg) 

<small><em>Photo by <a href="https://unsplash.com/@etaplus?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">ETA+</a> on <a href="https://unsplash.com/s/photos/broken-computer?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></em></small>

## Feature flags to the rescue!

Luckily, there's a better way to deploy code thanks to Feature Flags.  With feature flags, your deploy is easy - the new code goes live, but everyone just continues getting the old experience initially. Feature flags decouple deployment and release.  It's still possible to break things during deploy, but a lot less likely when no one is seeing the code right away. 

Using remote configuration, you are able to instantly update which version of the code users see without a deploy.  Maybe you start by only enabling it for you and your teammates while you click around and see if it's working.  Then you can gradually roll it out to 100% of your users while you watch your metrics.  If you do notice a bug, you can instantly just switch everyone back to the old code while you take your time debugging and fixing the problem. 

By using feature flags, you just decreased your downtime from 5+ minutes to mere seconds and limited the impact to a small percent of your traffic. Plus, you decreased the stress levels of you and your teammates 100 fold, which is always a good thing.

## Tutorial

In this tutorial, I'm going to walk through how to add feature flags as part of your development process. I'll be using a standard Next.js app and [GrowthBook](https://github.com/growthbook/growthbook) as the feature flagging platform.

GrowthBook is better known as an open source A/B testing platform, but it is also very capable at feature flagging. Other popular open source options are [Unleash](https://github.com/Unleash/unleash) and [FlagSmith](https://github.com/Flagsmith/flagsmith). For paid platforms, there is [LaunchDarkly](https://launchdarkly.com/) and [Split.io](https://www.split.io/), although these can get prohibitively expensive for many teams.

### Step 1: Next.js App

Let's start by getting a basic Next.js app running:

```sh
yarn create next-app
```

Then cd into the newly create directory and run:

```sh
cd my-app
yarn dev -p 4000
```

*Note*: Both GrowthBook and Next.js run on port 3000 by default, so we're making our Next.js app use 4000 instead to avoid conflicts.

Visit http://localhost:4000 and you should see the application running!

### Step 2: GrowthBook Account

For this tutorial, we'll run GrowthBook locally, but you can also use a free cloud-hosted account at https://app.growthbook.io instead if you want.

```sh
git clone https://github.com/growthbook/growthbook.git
cd growthbook
docker-compose up -d
```

After that, visit http://localhost:3000 and create your first user account.

![GrowthBook Signup Screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dqy45h98w54reyexxon.png)

### Step 3: Integrate the GrowthBook React SDK into our Next.js app

GrowthBook will generate some integration code for you, including a unique API endpoint to load your features from.

![GrowthBook Integration Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8yceihf8hiwrqyffum3.png)

We first need to install the GrowthBook React SDK in our Next.js app:

```sh
yarn add @growthbook/growthbook-react
```
 
Then we can modify the generated React code to work with the Next.js framework.  Modify the file `pages/_app.js` with the following contents:

```jsx
import '../styles/globals.css'
import { 
  GrowthBook, 
  GrowthBookProvider 
} from "@growthbook/growthbook-react";
import { useEffect } from "react";

const FEATURES_ENDPOINT = 
  "http://localhost:3100/api/features/key_abc123";

// Create a GrowthBook instance
const growthbook = new GrowthBook({
  trackingCallback: (experiment, result) => {
    console.log("Viewed Experiment", experiment, result);
  }
})

export default function MyApp({ 
  Component, 
  pageProps, 
  router 
}) {
  // Refresh features and targeting attributes on navigation
  useEffect(() => {
    fetch(FEATURES_ENDPOINT)
      .then((res) => res.json())
      .then((json) => {
        growthbook.setFeatures(json.features);
      });

    growthbook.setAttributes({
      "id": "123",
      "loggedIn": true,
      "deviceId": "abcdef123456",
      "employee": true,
      "company": "acme",
      "country": "US",
      "browser": navigator.userAgent,
      "url": router.pathname
    })
  }, [router.pathname])

  return (
    <GrowthBookProvider growthbook={growthbook}>
      <Component {...pageProps} />
    </GrowthBookProvider>
  )
}
```

Replace the `FEATURES_ENDPOINT` constant with the one you see in your instructions modal in GrowthBook.

In a real application, you would pull some of the targeting attributes from your authentication system or an API, but let's just leave them hard-coded for now.

### Step 4: Create a Feature in GrowthBook

Back in the GrowthBook application, we can create a new feature.  For this tutorial, we'll make a simple on/off feature flag that determines whether or not we show a welcome banner on our site.

![GrowthBook Create Feature](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2c4yuj4f4xrdspn7krb0.png)

The key we chose (`welcome-message`) is what we will reference when using the GrowthBook SDK.

We can now edit `pages/index.js` and conditionally render a welcome message based on the state of the feature:

Add an import statement:

```jsx
import { IfFeatureEnabled } from "@growthbook/growthbook-react";
```

And then put your welcome message somewhere on the page:

```jsx
  <IfFeatureEnabled feature="welcome-message">
    <p>I hope you enjoy this site and have a great day!</p>
  </IfFeatureEnabled>
```

If you refresh your Next.js app, you'll notice the welcome message is not rendered.  This is because when creating the feature, we set the default value to off. At this point, we could safely deploy our change to production and not worry about breaking anything.

### Step 5: Turn on the feature for your team

Now we can add rules to the feature to turn it on for specific groups of users.

In the hard-coded targeting attributes we set in `pages/_app.js`, we designated ourselves as an internal employee.  Let's use this attribute to turn on the feature for the whole internal team:

![GrowthBook Targeting Rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oysvcxmrr7jfwrdsjjjd.png)
 
Refresh your Next.js app and you should now see the welcome message appearing! (Note: it might take up to 30 seconds for the API cache to refresh).

![NextJS app with feature](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03sq2nkbutr2sk4a2duu.png)

If you change `employee` to false in `pages/_app.js`, you should see the welcome message disappear.

The best part about targeting attributes in GrowthBook is that they are evaluated entirely locally.  Sensitive user data is never sent over the network and there is no performance penalty. Some other libraries require an HTTP request to evaluate a feature for a user and this is often a deal breaker.

### Step 6: Gradually roll out to your users

After you test the new feature within your team, you probably want to start rolling it out to real users.

We can do that with another rule in GrowthBook:

![GrowthBook Rollout Rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9imvgccpscubv53du62.png)

In the targeting attributes in `pages/_app.js`, make sure `employee` is set to `false`. That will ensure you skip the first rule we made and fall into the second rollout rule.

The GrowthBook SDK uses deterministic hashing to figure out whether or not someone is included in a rollout.  Basically, it hashes together the selected targeting attribute (`id`) and the feature key (`welcome-message`) and coverts it to a float between 0 and 1.  If that float is less than or equal to the rollout percent, the user is included. This ensures a consistent UX and prevents one user from constantly switching between ON and OFF as they navigate your app.

Try changing the user id in the targeting attributes in `pages/_app.js` to a few different random strings and see what happens.  You should notice about half of the time the welcome message shows up and half of the time it doesn't.  The rollout is working!

## Conclusion and Next Steps

Now you've seen how to set up and integrate GrowthBook into your Next.js application, use feature flags in your code, remotely enable the feature in GrowthBook based on targeting conditions, and gradually roll out the feature to your users.

Once you do the initial integration work, it only takes a few seconds to wrap your code in feature flags. Once you realize how easy and stress-free deploys can be, there's no going back.

Next Steps:
- Look at the [GrowthBook React SDK docs](https://docs.growthbook.io/lib/react) for more ways to use feature flags in your code besides the `<IfFeatureEnabled>` component
- Read more about [using features in GrowthBook](https://docs.growthbook.io/app/features), including how to use complex feature values (not just ON/OFF), run A/B tests, and analyze the impact of your features using data.

Happy feature flagging!