Package name and location changes
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.
Client initialization
In v4.0 of the SDK, client initialization has changed in the following ways:- The
initializemethod was removed. - Initialization is now split into two parts:
createClientandstart. LDOptions, which was formerly passed into theinitialize()function, is now split into two types:LDStartOptionsandLDOptions. To learn more, read Changes to LDOptions.
Client initialization flow
In v4.0 of the SDK, client initialization flow has changed in the following ways:- In v4.0 the
waitForInitialization()method returns a result object instead of rejecting promises. The v3.xwaitUntilReady()method has been removed. waitForInitialization()now always resolves, never rejects, and returns a result object with astatusfield.timeoutis now specified as an option object,{ timeout: 5 }, instead of a direct parameter- The result object allows you to handle all cases, including success, failure, and timeout, without try/catch.
Changes to LDOptions
In v4.0 of the SDK,LDOptions has changed in the following ways:
- The
bootstrapoption moved fromLDOptionstoLDStartOptionsandidentify. Bootstrapping data is now part of the initialization of a client instance.identifyis a key part of the client initialization process required to associate the instance with an initial context. - The SDK now defaults to using local storage-based caching. Previously, to enable local storage caching, you needed to set this as a special value for the
bootstrapproperty. - Version 3.x of the SDK used the
streamUrl,baseUrl, andeventsUrlproperties to specify the base URIs for alternative service endpoints. Version 4.0 of the JavaScript SDK uses thestreamUri,baseUri, andeventsUriproperties to specify the base URIs for alternative service endpoints.
Report changes
In the JavaScript SDK v3.x and earlier, if you usedREPORT, 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.
Flag evaluation now has typed methods
Thevariation 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:
boolVariationDetailfor boolean flags.numberVariationDetailfor number flags.stringVariationDetailfor string flags.jsonVariationDetailfor JSON flags.
*variationDetail methods, read LDEvaluationDetail. To learn more about the configuration option, read evaluationReasons.
Events changes
Version 4.0 of the SDK includes changes to theallFlags() method and the sendEvents configuration.
Analytics events changes
In v4.0 of the SDK, theallFlags() method no longer sends analytics events. To learn more, read Getting all flags.
Changes to sending events
In v4.x of the JavaScript SDK, you can disable sending events by setting thesendEvents 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.Flag listener changes
In v4.0 of the SDK, flag listeners have changed in the following ways:- The
changeevent listener now receives(context, changedKeys), wherechangedKeysis an array of strings. The SDK no longer returns the flag value object along with this event. - The event does not include flag values. You must call
variation()to get the current value. change:<example-flag-key>event listener now only receives(context).
Error event handling changes
The new SDK has changed how errors are logged when error event listeners are present. In v3.x of the SDK, themaybeReportError function would check if there was an error listener:
- If an error listener was registered, the error event was emitted but not logged to the console
- If no error listener was registered, the error was logged to the console
- Errors are always logged using the logger, even if you have your own error listeners
- Your error listeners will still receive the error events
- You may see duplicate error logs if you’re also logging errors in your error handler
LDLogger for your client to suppress handled errors.
Addition of data sources
Version 4.0 of the SDK adds thechange, 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.