Feature flags
The idea behind feature flags is that particular code is wrapped in a conditional, which will run the code when a flag value is true, and not run it when the flag is false.
On Luna, we want to use feature flags when it is desirable to merge code being developed for a new feature into the main branch, but are not yet ready for this new feature to be accessible in all environments.
Setting a feature flag for a particular environment to true can allow e.g. testing or demoing of the new feature, but having the flag set to false in other environments, e.g. production, prevents users from seeing a partially finished feature, which is still being developed.
Frontend
react-feature-flags
We are using the react-feature-flags npm package to help implement feature flags in our FE.
This package offers <FlagsProvider /> and <Flags /> components. The provider is used high up in the Luna app structure to provide the flags state to any lower <Flags /> components. These <Flags /> components then implement the conditional logic to run the code they contain when the relevant flag is true, and not when it is false.
A flag is a simple object containing the name of the feature and an isActive boolean:
{
name: 'clientFeedback',
isActive: false
}Documentation on the components of this package and how to use them can be found on the react-feature-flags npm page.
Flag sources
The flags provided to the react-feature-flags <FlagsProvider /> component are just an array of flag objects and can come from various sources.
Within Luna, there are 2 intended sources of feature flags — but only one of them actually reaches the app today (see How it works below).
Localstorage
A localstorage variable in the browser, _luna~user-prefs::flags - the value of this variable should be a JSON stringified array of flags objects. This is the only source that currently works.
Feature Flags API
A GET /feature-flags API is provided by the Luna BE to return a feature flags file, featureFlags.json, from an S3 bucket, ${Environment}-feature-flags-and-digital-luna-bucket, for each environment.
There is a section of the FE app state configured to hold these feature flags, with actions, thunks, and reducer available to handle fetching and updating them.
Not functional today
The FE call that fetches from this API is disabled — see How it works below for the detail. Don't rely on this path until it's fixed.
How it works
Feature Flags API fetch is disabled — broken in production
The <Gateway /> component (clients/luna-front-end/src/components/Gateway/Gateway.jsx) is meant to dispatch fetchFeatureFlags() on every authenticated route, fetching flags from the GET /feature-flags API and writing them into Redux state. That useEffect call is commented out in code, with a TODO noting that in production it continuously errored and caused an infinite dispatch loop.
Impact: the Feature Flags API source is effectively dead — no flag set only via the backend/S3 featureFlags.json ever reaches the app. Localstorage is the only flag source that actually works today. Don't rely on the backend API path until this is fixed.
The <Gateway /> component, which is used within each authenticated route of the <Routes /> component, is responsible for dispatching the call to fetch flags from the /feature-flags API and set them on the application Redux state — but see the warning above, this call is currently disabled.
The <UserPreferencesProvider /> component, which wraps the <App /> component, is responsible for providing a context with the flags from the browser's localstorage.
The <App /> component uses useSelector to fetch flags from the application state, and the UserPreferencesContext to fetch flags from the user's localstorage. These are then combined and passed to the <FlagsProvider /> component from react-feature-flags, which then provides the flag state to any <Flags /> components.
How to use it
As stated in the section above, the <Gateway /> component's logic to fetch feature flags via the /feature-flags API is currently disabled.
Therefore, currently the only way to enable features is by setting a localstorage variable with the name: _luna~user-prefs::flags. The value of this variable should be a JSON-stringified array of flags objects, e.g.
'[{"name":"clientFeedback","isActive":false},{"name":"healthCheck","isActive":true}]'Backend
The services/feature-flags service in this monorepo provides the GET /feature-flags API used by the FE to retrieve flags from an S3 bucket created in each environment, ${Environment}-feature-flags-and-digital-luna-bucket.
The BE is not currently set up to use feature flags itself, and only provides the functionality above for the FE to use.
