Feature flags are a great way to prevent stale branches by regularly shipping features in your code without officially enabling them. A feature flag let’s you turn code on and off, in the case of features, a feature flag means you can regularly merge branches and release them.
While there are many different ways you can approach this, one of my favourite and most simple approaches is a features.json
file in your application.
It can be something as simple as a JSON file of properties and boolean values.
{ "feature1": true, "feature2": false }
Or in the case of something more flexible and less straightforward it can be something like this where we can specify specific roles who are allowed to use a certain feature.
{ "feature1": { "roles:" ["admin", "editor"] "enabled": true }, "feature2": { "enabled": false } }
In your code, you import the features.json
file and then reference the features. Depending on your framework or environment, this will look different.
import features from './features.json'; const FeatureOne = features.feature1 ? import ('./features/feature-one') : null;
If you were to do this in a framework like Angular or Aurelia, you would create a service or middleware of some kind which compares the route against the property in your features file and reacts accordingly.
import features from './features.json'; export class FeatureService { getFeature(key) { return features?.[key] ?? null; } }
There are a few feature flag services out there which provide SDK’s and ways to turn features on and off, do A/B testing, but for most use-cases, you don’t need anything else other than a JSON file and a little code to wire it all up.