JavaScript Browser SDK
The Unleash JavaScript SDK is a lightweight client with no external dependencies (beyond browser APIs) that lets you evaluate feature flags in any JavaScript application. It is not tied to any framework, so you can use it with React, Vue, Angular, or plain JavaScript.
The SDK stores flag configuration in localStorage and synchronizes with Unleash or Unleash Edge in the background. Because flags are stored in the browser, the SDK can bootstrap itself the next time the user visits the same page.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
Installation
Initialization
Configure the client according to your needs. The following example provides only the required options. Refer to configuration options for the full list.
Set url to the /api/frontend endpoint of your Unleash instance or your Unleash Edge instance. Set clientKey to a frontend API token.
Check if the SDK is ready
You should wait for the client’s ready or initialized events before you start working with it. Before it’s ready, the client might not report the correct state for your features.
The difference between the events is described in the section on available events.
Check flags
Once the client is ready, you can start checking features in your application. Use the isEnabled method to check the state of any feature you want:
You can use the getVariant method to get the variant of an enabled feature that has variants. If the feature is disabled or if it has no variants, then you will get back the disabled variant
Unleash context
The Unleash context holds properties such as user ID, session ID, and custom fields that drive flag evaluation, including strategy targeting, constraints, and stickiness. To update and configure the Unleash context in this SDK, use the updateContext, setContextField, and removeContextField methods.
The context you set in your app will be passed along to the Unleash Edge or the Frontend API as query parameters for feature evaluation.
The updateContext method will replace the entire
(mutable part) of the Unleash context with the data that you pass in.
The setContextField method only acts on the property that you choose. It does not affect any other properties of the Unleash context.
The removeContextField method only acts on the property that you choose. It does not affect any other properties of the Unleash context.
Configuration options
The Unleash SDK takes the following options:
Events
The client is also an event emitter. This means that your code can subscribe to updates from the client. This is a neat way to update a single page app when toggle state updates.
The SDK emits the following events:
- error - emitted when an error occurs on init, or when fetch function fails, or when fetch receives a non-ok response object. The error object is sent as payload.
- initialized - emitted after the SDK has read local cached data in the storageProvider.
- ready - emitted after the SDK has successfully started and performed the initial fetch of flags via the network (Edge, or Frontend API). When bootstrapping, the client can emit this event twice: once when the bootstrapped flags are loaded, and once on first successful connection to Unleash.
- update - emitted every time Unleash returns a new feature toggle configuration. The SDK will emit this event as part of the initial fetch from the SDK.
- recovered - emitted when the SDK has recovered from an error. This event will only be emitted if the SDK has previously emitted an error.
- sent - emitted when the SDK has successfully sent metrics to Unleash.
unleash.start(). If you register them after you have started the SDK you risk losing important events.Session ID
You may provide a custom session id via the “context”. If you do not provide a sessionId this SDK will create a random session id, which will also be stored in the provided storage (local storage). By always having a consistent sessionId available ensures that even “anonymous” users will get a consistent experience when feature flags are evaluated, in combination with a gradual (percentage based) rollout.
Stop the client
You can stop the Unleash client by calling the stop method. Once the client has been stopped, it will no longer check for updates or send metrics to the server.
A stopped client can be restarted.
Custom store
This SDK can work with React Native storage @react-native-async-storage/async-storage or react-native-shared-preferences and many more to backup feature flags locally. This is useful for bootstrapping the SDK the next time the user comes back to your application.
You can provide your own storage implementation.
Examples:
Node.js usage
This SDK can also be used in node.js applications (from v1.4.0). Please note that you will need to provide a valid “fetch” implementation. Only ECMAScript modules is exported from this package.
index.mjs
Developer toolbar
The Developer Toolbar wraps your Unleash client to provide runtime flag overrides during development. Install @unleash/toolbar and wrap your client:
The wrapped client has the same API as the original. Refer to the Developer Toolbar documentation for configuration options and framework guides.
CDN usage
Bootstrap
You can bootstrap the SDK with your own feature flag configuration to make flags available immediately, without waiting for an API call. This is useful when you need flags in a specific state right after initialization.
Pass a bootstrap array and an optional bootstrapOverride flag when you create the client:
When bootstrapOverride is true (the default), the bootstrap data replaces any locally cached data. When bootstrapOverride is false, the bootstrap data is only used if the local cache is empty.
Manage your own refresh mechanism
You can opt out of the Unleash feature flag auto-refresh mechanism and metrics update by settings the refreshInterval and/or metricsInterval options to 0.
In this case, it becomes your responsibility to call updateToggles and/or sendMetrics methods.
This approach is useful in environments that do not support the setInterval API, such as service workers.