This topic explains the changes in the JavaScript SDK 4.0 release and how to adapt code that uses a 3.x version of the JavaScript SDK to use version 4.0 or later.
Version 4.0 includes several breaking changes. Before you migrate to version 4.0, update to the latest 3.x version. If you update to the latest 3.x version, deprecation warnings appear in areas of your code that need to be changed for 4.0. You can update them at your own pace while still using 3.x, rather than migrating everything simultaneously. To learn more about updating to the latest 3.x version, visit the SDK’s GitHub repository.
In v4.0 of the SDK, the node module is now named @launchdarkly/js-client-sdk. To begin the migration, install the new package and swap all references from launchdarkly-js-client-sdk to @launchdarkly/js-client-sdk.
In v4.0 of the SDK, client initialization has changed in the following ways:
initialize method was removed.createClient and start.LDOptions, which was formerly passed into the initialize() function, is now split into two types: LDStartOptions and LDOptions. To learn more, read Changes to LDOptions.This two-step process ensures that you can register all event listeners and perform any necessary setup before the client begins connecting to LaunchDarkly. This eliminates race conditions where events might be missed if listeners were registered after the client had already started initializing.
Here is the new client initialization method:
In v4.0 of the SDK, client initialization flow has changed in the following ways:
waitForInitialization() method returns a result object instead of rejecting promises. The v3.x waitUntilReady() method has been removed.waitForInitialization() now always resolves, never rejects, and returns a result object with a status field.timeout is now specified as an option object, { timeout: 5 }, instead of a direct parameterHere is the new client initialization flow:
In v4.0 of the SDK, LDOptions has changed in the following ways:
bootstrap option moved from LDOptions to LDStartOptions and identify. Bootstrapping data is now part of the initialization of a client instance. identify is a key part of the client initialization process required to associate the instance with an initial context.bootstrap property.streamUrl, baseUrl, and eventsUrl properties to specify the base URIs for alternative service endpoints. Version 4.0 of the JavaScript SDK uses the streamUri, baseUri, and eventsUri properties to specify the base URIs for alternative service endpoints.To learn more, read the following SDK documentation:
In the JavaScript SDK v3.x and earlier, if you used REPORT, enabled the useReport configuration option, and wanted to use streaming, then you had to use use the LaunchDarkly EventSource polyfill.
The JavaScript SDK v4.0 supports REPORT directly. If you enable the useReport configuration option, no polyfill or additional configuration is required.
The variation method lets you evaluate a feature flag. The variationDetail method lets you evaluate a feature flag while providing more information about how the value was calculated.
In v4.0 of the SDK, you can also use typed methods. These methods include:
boolVariationDetail for boolean flags.numberVariationDetail for number flags.stringVariationDetail for string flags.jsonVariationDetail for JSON flags.To learn more about the *variationDetail methods, read LDEvaluationDetail. To learn more about the configuration option, read evaluationReasons.
Version 4.0 of the SDK includes changes to the allFlags() method and the sendEvents configuration.
In v4.0 of the SDK, the allFlags() method no longer sends analytics events. To learn more, read Getting all flags.
In v4.x of the JavaScript SDK, you can disable sending events by setting the sendEvents configuration to false.
Earlier versions of the JavaScript SDK respect the Do Not Track events header. If an end user has Do Not Track enabled in their browser, earlier versions do not send analytics events for flag evaluations or metrics to events.launchdarkly.com.
In v4.0 of the SDK, flag listeners have changed in the following ways:
change event listener now receives (context, changedKeys), where changedKeys is an array of strings. The SDK no longer returns the flag value object along with this event.variation() to get the current value.change:<example-flag-key> event listener now only receives (context).Here is the new flag listener method:
The new SDK has changed how errors are logged when error event listeners are present.
In v3.x of the SDK, the maybeReportError function would check if there was an error listener:
This meant that if you wanted to handle errors yourself, you wouldn’t get duplicate error logs.
In v4.0 of the SDK, the client always registers a default error listener that logs errors via the logger. This means:
The new error handling process allows for more robust control over what errors are ignored. This avoids not logging or informing end users when an unhandled error happens. Our recommendation is to implement LDLogger for your client to suppress handled errors.
Version 4.0 of the SDK adds the change, error, and dataSourceStatus methods to subscribe to events. When you subscribe to dataSourceStatus events, the state returned may be one of Initializing, Valid, Interrupted, SetOffline, Closed.
To learn more, read Monitoring SDK status.
All types and methods that were marked as deprecated in the last 3.x release have been removed from the 4.0 release. If you were using a recent 3.x version, you should already have received compile-time deprecation warnings with suggestions for their recommended replacements.
For a full list of deprecated types and methods, read the release notes in GitHub.