For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • SDKs
    • SDK concepts
    • SDK features
      • Aliasing users
      • Anonymous contexts and users
      • Automatic environment attributes
      • Big segments
      • Bootstrapping
      • Configuration
      • Context configuration
      • Customizing AgentControl
      • Data saving mode
      • Evaluating flags
      • Flag evaluation reasons
      • Flushing events
      • Getting all flags
      • Hooks
      • Identifying and changing contexts
      • Inspectors
      • Logging configuration
      • Migrations
      • Monitoring SDK status
      • Multiple environments
      • Offline mode
      • OpenTelemetry in server-side SDKs
      • Private attributes
      • User feedback
      • Reading flags from a file
      • Relay Proxy configuration
      • Secure mode
      • Sending custom events
      • Shutting down
      • Storing data
      • Subscribing to flag changes
      • Test data sources
      • Tracking AI metrics
      • Web proxy configuration
    • Client-side SDKs
    • Server-side SDKs
    • AI SDKs
    • Edge SDKs
    • OpenFeature providers
    • Observability SDKs
    • Relay Proxy
Sign inTry it free
LogoLogo
On this page
  • Overview
  • About the bootstrapping feature
  • Bootstrapping using local storage
  • Bootstrapping using server-rendered content
  • Client-side SDKs
  • Electron
  • JavaScript
  • Node.js (client-side)
  • React Server Wrapper
  • React Web
SDKsSDK features

Bootstrapping

Was this page helpful?
Previous

Configuration

Next
Built with

Overview

This topic explains how the bootstrapping feature works in the LaunchDarkly SDKs that support it.

About the bootstrapping feature

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:

  • Bootstrapping using local storage (the default)
  • Bootstrapping using server-rendered content

Each method has its advantages and drawbacks, explained below.

Bootstrapping using local storage

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:

  • It is enabled by default in the JavaScript SDK beginning with version 4.x, and requires little maintenance.
  • It is useful when you can’t dynamically add bootstrapping data to the page.

Bootstrapping using only local storage also has some downsides:

  • The first time the customer visits your site, local storage is empty. This means the customer receives the site’s default behavior, as specified in the fallback values you provide, before the values from feature flags load. Only after the customer’s second visit does the page use the values in local storage. It’s important to choose effective fallback values to limit rendering delays.
  • If there is a long time between page visits, the values stored in local storage may be out of date.
  • Some customers have privacy settings that block sites from using their browser’s local storage.
  • Flag changes that happen while the customer is on the page automatically update the local storage cache, keeping it in sync with the server. However, if the customer is not on the page when the flag change happens, the flag value in local storage may go out of sync with the server. The next time this customer visits that page, they could experience a flicker because of the client using the previous flag value from local storage.

For an example of bootstrapping from local storage on a static site, read Bootstrap using local storage.

Bootstrapping using server-rendered content

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:

  • It provides values the first time a customer visits a page.
  • Bootstrapped values will not become out of date if there is a lag between page visits.
  • It does not rely on a customer’s privacy settings to function.

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.

Newer versions of LaunchDarkly SDKs replace users with contexts

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.

Client-side SDKs

This feature is available in the following client-side SDKs:

  • Electron
  • JavaScript
  • Node.js (client-side)
  • React Server Wrapper
  • React Web

Electron

Expand Electron code sample

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:

1const client = LaunchDarkly.initialize(
2 'example-client-side-id',
3 user,
4 {
5 bootstrap: {
6 flagKey1: flagValue1,
7 flagKey2: flagValue2
8 }
9 }
10);

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:

1const client = LaunchDarkly.initializeInMain(
2 'example-client-side-id',
3 user,
4 {
5 bootstrap: 'localStorage'
6 }
7);

JavaScript

Expand JavaScript code sample

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:

1import { createClient } from '@launchdarkly/js-client-sdk';
2
3const context = { kind: 'user', key: 'example-user-key'};
4
5// Local storage is enabled by default
6// You can optionally configure the maximum number of cached contexts (default is 5)
7const options = { maxCachedContexts: 3 };
8
9const client = createClient(
10 'example-client-side-id',
11 context,
12 options
13);
14
15client.start();

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:

1import { createClient } from '@launchdarkly/js-client-sdk';
2
3const context = { kind: 'user', key: 'example-user-key'};
4
5const client = createClient(
6 'example-client-side-id',
7 context
8);
9
10// bootstrapData is the result of your server-side SDK call to get all flags
11const flags = JSON.parse(bootstrapData)
12const options = { bootstrap: flags }
13
14client.start(options);

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:

1import { createClient } from '@launchdarkly/js-client-sdk';
2
3const context = { kind: 'user', key: 'example-user-key'};
4
5const client = createClient(
6 'example-client-side-id',
7 context
8);
9
10client.start({
11 // Load values from a Ruby template directive
12 bootstrap: <%= client.all_flags_state(user, {client_side_only: true}).to_json %>
13});

You can also read about How to bootstrap when serving from a CDN, not a backend.

Node.js (client-side)

Expand Node.js (client-side) code sample

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:

1// bootstrapData is the result of your server-side SDK call to get all flags
2const flags = JSON.parse(bootstrapData)
3
4function onPageLoad(flags) {
5 ...
6
7 const client = LDClient.initialize(
8 'example-client-side-id',
9 context,
10 options = {
11 bootstrap: flags
12 }
13 );
14
15 ...
16}

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":

1const client = LDClient.initialize(
2 'example-client-side-id',
3 context,
4 {
5 bootstrap: 'localStorage'
6 }
7);

React Server Wrapper

Expand React Server Component support sample

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:

JavaScript
1// app/page.tsx (Server Component)
2import { init } from '@launchdarkly/node-server-sdk';
3import { createLDServerSession, LDIsomorphicProvider } from '@launchdarkly/react-sdk/server';
4
5const ldBaseClient = init(process.env.LAUNCHDARKLY_SDK_KEY!);
6
7export default async function Page() {
8 await ldBaseClient.waitForInitialization({ timeout: 10 });
9
10 const session = createLDServerSession(ldBaseClient, {
11 kind: 'user',
12 key: 'user-key',
13 name: 'Sandy',
14 });
15
16 return (
17 <LDIsomorphicProvider
18 session={session}
19 clientSideId={process.env.LAUNCHDARKLY_CLIENT_SIDE_ID!}
20 >
21 <App />
22 </LDIsomorphicProvider>
23 );
24}

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:

Something went wrong!

For a complete working application, read the isomorphic provider example.

React Web

Expand React Web code sample

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:

React Web SDK v4.0
1import { createLDReactProvider } from '@launchdarkly/react-sdk';
2
3// bootstrapData is the result of your server-side SDK call to get all flags
4const flags = JSON.parse(bootstrapData);
5
6const LDProvider = createLDReactProvider(
7 'example-client-side-id',
8 { kind: 'user', key: 'example-user-key' },
9 {
10 bootstrap: flags,
11 },
12);

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.

React Web SDK v4.0
1import { createLDReactProvider } from '@launchdarkly/react-sdk';
2
3const LDProvider = createLDReactProvider(
4 'example-client-side-id',
5 { kind: 'user', key: 'example-user-key' },
6 {
7 startOptions: {
8 bootstrap: flags,
9 },
10 },
11);

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.