JavaScript Browser SDK
This JavaScript Frontend SDK is designed to help you integrate with Unleash and evaluate feature flags inside your application.
You can use this client with Unleash Enterprise or Unleash Open Source.
The JavaScript client is a tiny Unleash client written in JavaScript without any external dependencies (except from browser APIs). This client stores toggles relevant for the current user in localStorage and synchronizes with Unleash (the Unleash Frontend API or Unleash edge) in the background. Because toggles are stored in the user’s browser, the client can use them to bootstrap itself the next time the user visits the same web page.
This client expect fetch to be available.
Frameworks Supported
This package is not tied to any framework, but can be used together most popular frameworks, examples:
- Unleash React SDK
- React
- React Native
- Angular JS
- Vue.js
- …and probably your favorite!
How to Use the Client
Step 1: Installation
Step 2: Initialize the SDK
Configure the client according to your needs. The following example provides only the required options. Refer to the section on available options for the full list.
Connection Options
To connect this SDK to your Unleash instance’s Frontend API, use the URL to your Unleash instance’s Frontend API (<unleash-url>/api/frontend) as the url parameter. For the clientKey parameter, use a FRONTEND token generated from your Unleash instance. Refer to the API tokens documentation for more information.
This SDK can also be used with Unleash Edge.
Step 3: Let the Client Synchronize
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.
Step 4: Check Feature Toggle States
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
Updating the Unleash Context
The Unleash context is used to evaluate features against attributes of a the current user. 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.
Available Options
The Unleash SDK takes the following options:
Listen for Updates via the EventEmitter
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.
Available 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.SessionId - Important Note
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 toggles is evaluated, in combination with a gradual (percentage based) rollout.
Stop the SDK
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 toggles 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:
How to Use in Node.js
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
How to Use the Client via CDN
Bootstrap
Now it is possible to bootstrap the SDK with your own feature toggle configuration when you don’t want to make an API call.
This is also useful if you require the toggles to be in a certain state immediately after initializing the SDK.
How to Use It?
Add a bootstrap attribute when create a new UnleashClient.
There’s also a bootstrapOverride attribute which is by default is true.
If bootstrapOverride is true (by default), any local cached data will be overridden with the bootstrap specified.
If bootstrapOverride is false any local cached data will not be overridden unless 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.