launchdarkly / js-client-sdk

LaunchDarkly Client-side SDK for Browser JavaScript
Other
109 stars 62 forks source link

LD double initialisation with Strict Mode set to true. #284

Closed matias-pinata closed 1 year ago

matias-pinata commented 1 year ago

Describe the bug In my Next.js application and only on local environment with React Strict Mode set to true, LD makes two calls to https://app.launchdarkly.com/sdk/goals/{id}. This then opens 2 websockets on https://clientstream.launchdarkly.com/eval/ and for some reason all my flags do not update, they get stuck on default values.

To reproduce Create a new Next.js app (any React 18 app should trigger it) and initialize App.tsx using

export default withLDProvider({
  clientSideID: process.env.NEXT_PUBLIC_LAUNCH_DARKLY_CLIENT_ID || "",
})(MyApp);

App.tsx renders twice, which initializes LD provider twice, triggering the bug.

Expected behavior LD should be a singleton, or there should be a way to manually call the initialize (so I can avoid calling it twice). Or it should initialize twice and handle correctly the flags updates.

SDK version 3.0.6

Language version, developer tools Nextjs 13.4.4

OS/platform Mac Pro M1