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.
You typically obtain the bootstrap flag values from a server SDK. The server sends to the browser an HTML page containing the JavaScript and the feature flags your site needs during the initial page render. Your site then bootstraps the LaunchDarkly client using the provided flags during client initialization in the start() function. Feature flags are ready immediately, and clients always receive the latest feature flag values.
Bootstrapping using server-rendered content requires additional setup, but it provides several benefits over using only cached flag values in local storage:
Some SDKs support providing bootstrap values in the identify() function in addition to start(). Providing values in identify() lets you use bootstrap values after an anonymous context logs in as an identified user.
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, usually via the start() function. To learn more, read Getting all flags.
For a demonstration of bootstrapping from the server, visit our hello-bootstrap GitHub repository.
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.
Some newer client SDKs support bootstrapping flag values in addition to caching flag evaluations for unique contexts. When flag evaluations are cached to local storage, on subsequent launches the SDK reads the cached values before any network connection is established. The client still initializes and connects to LaunchDarkly’s service to fetch the most recent flag values, but it is not dependent on those values to reach its ready state.
The following SDKs enable local storage caching by default, and you can configure both bootstrapping and local caching independently:
Other SDKs use the same implementation for both bootstrapping and local storage caching. For these SDKs you cannot configure both bootstrapped flag values and local storage caching. The following SDKs use a single bootstrap configuration parameter to either provide bootstrap flag values or enable local storage caching, but not both:
Using local storage caching instead of bootstrap server values can be useful if you can’t dynamically add bootstrapping data to your page. However, using only local storage caching has these downsides:
For an example of using local storage without bootstrapping on a static site, read Configure local storage for cached contexts.
To learn more, read Caching contexts to local storage.
The bootstrapping 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 provide bootstrap flag values you cannot enable caching to local storage. To learn more, read Caching contexts to 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 use bootstrap values after an anonymous context logs in as an identified user.
In JavaScript SDK v3.x 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.x. To learn more, read Caching contexts to local storage.
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:
If you provide bootstrap flag values you cannot enable caching to local storage. To learn more, read Caching contexts to local storage.
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.