This topic explains how the bootstrapping feature works in the LaunchDarkly SDKs that support it.
The bootstrapping feature lets you decrease startup times for client-side SDKs by providing them with an initial set of flag values that are immediately available during client initialization. The SDK serves these values to the end user before it has established a connection to LaunchDarkly so there is no inconsistency in the flag variations they receive.
There are two methods of bootstrapping the client:
Each method has its advantages and drawbacks, explained below.
The following applies to bootstrapping with the JavaScript SDK. To learn how to bootstrap using other client-side SDKs, read Electron and Node.js (client-side).
If you are using the JavaScript SDK, the client stores the latest flag settings in the browser’s local storage. The very first time the SDK is initialized, it connects to LaunchDarkly. After that, the client can initialize itself using the values in local storage. This avoids any potential additional delay from fetching the flags from the LaunchDarkly servers. The client still initializes and connects to LaunchDarkly’s service to fetch the most recent flag values, but is no longer dependent on those values to reach its ready state.
The local storage cache updates when new values arrive. On subsequent page loads, the client fetches the most recently cached flag values from local storage and emits the ready event immediately.
Bootstrapping using local storage has some benefits:
Bootstrapping using only local storage also has some downsides:
For an example of bootstrapping from local storage on a static site, read Bootstrap using local storage.
In this method, the server sends to the browser an HTML page containing the JavaScript and the feature flags your site needs during the initial render. Your site then bootstraps the LaunchDarkly client with those flags. Feature flags are ready immediately, and clients always receive the latest feature flag values.
Bootstrapping using server-rendered content requires more setup than bootstrapping from local storage, but provides more benefits.
The benefits of bootstrapping using server-rendered content include:
The downside to bootstrapping using server-rendered content is that it requires significantly more set up work than bootstrapping from local storage.
For a demonstration of bootstrapping from the server, visit our hello-bootstrap GitHub repository.
All of the server-side SDKs have a function, named some variation of allFlagsState, to evaluate flags on behalf of a specified user or context. We recommend populating the initial set of bootstrap values with a JSON object containing flag metadata derived from calling the server-side SDK’s all flags method.
If your back end passes values to your front end on page load, you can call your server-side SDK’s all flags function on page load and pass the results as a parameter to your front-end initialization code. To learn more, read Getting all flags.
You can also use LaunchDarkly’s edge SDKs to access flag values without processing delays. To learn more, read Edge SDKs. For an example, read Using LaunchDarkly with Cloudflare Workers.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: “users.”
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
This feature is available in the following client-side SDKs:
You can use bootstrapping on the Electron SDK with values provided by LaunchDarkly-enabled code on the backend.
You can use it to set the feature flags to any values you want:
If you set bootstrap to the string "localStorage", the client tries to get flag values from persistent storage, using a unique key that is based on the user properties.
In Electron, persistent storage consists of files in the userData directory. If the client finds flag values stored for this user, it uses them and starts up immediately in a ready state, but also makes a background request to LaunchDarkly to get the latest values and stores them as soon as it receives them.
Here’s how to use this mode:
The JavaScript client SDK v4.x stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts at startup.
Set maxCachedContexts to 0 to disable bootstrapping from local storage and clear any cached contexts. Setting disableCache to true disables bootstrapping from local storage, but leaves any existing local storage in place.
To learn more, read Interface LDOptions.
JavaScript SDK v3.7+ requires you to enable caching contexts to local storage at initialization using the bootstrap option:
When the client uses local storage, it stores the latest flag settings there. On page load, the client uses the previous settings and emits the ‘ready’ event immediately. This means the end user may receive cached flag values until the next page load.
You can still subscribe to flag changes when you use local storage.
To bootstrap flags from server-rendered content using the JavaScript SDK, populate the initial set of bootstrap values with a JSON object containing flag metadata derived from calling the server-side SDK’s all flags method.
In JavaScript SDK v4.x you can provide server flag values in the bootstrap option to start() or identify(). This lets you use server-provided values immediately without waiting for client-side initialization. It also lets you to use bootstrap values after an anonymous context logs in as an identified user.
In JavaScript SDK v3.7+ you can provide the bootstrap option only once, during initialization. This lets you bootstrap initial flag values for an anonymous context, but does not allow for bootstrapping after the context logs in as an identified user. Using bootstrap with server-rendered content also disables caching context to local storage with SDK v3.7+.
Here is an example, which assumes you pass the flags on page load:
If you can invoke your backend dynamically, such as in Ruby with a template directory, you can inline the function invocation and request that the SDK return only the client-side flags.
Here is an example of how to bootstrap flags into the JavaScript client, if you acquire the flags from a Ruby template directive:
You can also read about How to bootstrap when serving from a CDN, not a backend.
You can use bootstrapping on the Node.js (client-side) SDK with values provided by LaunchDarkly-enabled code on the backend. You can populate the initial set of bootstrap values with a JSON object containing flag metadata derived from calling the server-side SDK’s all flags method.
You can use it to set the feature flags to any values you want:
A more useful mode in a client-side Node application is to bootstrap from locally cached values. In this mode, if no values have been cached yet, the SDK obtains flags from LaunchDarkly and then caches them in persistent storage. The next time you start, the cached flags are immediately available, and the SDK also contacts LaunchDarkly in the background to obtain updated values.
To activate this mode, use the special string "localStorage":
Starting from version 4.x of the React Web SDK, you can use our React Server Component support
to facilitate a special kind of bootstrapping through our
LDIsomorphicProvider.
If you want to take full advantage of React Server Components for faster load time and continue getting live updates in the client, here’s how:
LDIsomorphicProvider is an async React Server Component that evaluates all flags on the server and bootstraps the client-side SDK with those values. This allows the client-side SDK to start immediately with real values instead of defaults.
After hydration, the client SDK opens a streaming connection and live flag updates propagate normally to all hooks.
Server Components inside the provider tree can call session.boolVariation(...) directly. Client Components use the standard hooks, such as useBoolVariation. They read from the bootstrapped data on first render and receive live updates afterwards.
Here is a list of available properties for LDIsomorphicProvider:
For a complete working application, read the isomorphic provider example.
In the React Web SDK v4.0 and later, bootstrap is a top-level option on createLDReactProvider. It accepts a plain key-value object, such as { 'example-flag-key': true }, or the output of the server-side SDK’s allFlagsState().toJSON(), which includes $flagsState and $valid metadata.
To bootstrap flags in the React Web SDK, we recommend populating the initial set of bootstrap values with a JSON object containing flag metadata derived from calling the server-side SDK’s all flags method.
Here is an example that assumes you pass the flags on page load:
You can also pass bootstrap data through startOptions.bootstrap. The top-level bootstrap option is merged into startOptions.bootstrap when the client starts. If you provide both, the top-level value takes precedence.
If you are migrating from the React Web SDK v3.x, the bootstrap option used to be nested inside options. To learn more, read the React Web SDK 3.x to 4.0 migration guide.