launchdarkly / react-client-sdk

LaunchDarkly Client-side SDK for React.js
Other
79 stars 67 forks source link

SDK subscribes to all flags even when `flags` configuration is set #149

Closed atrakh closed 1 year ago

atrakh commented 1 year ago

Describe the bug The SDK appears to continue to request all LaunchDarkly flags, even if I set the flags property when configuring the client, which seems to conflict with the documentation here.

To reproduce Repro steps here are simply to set the flags configuration to a subset of the flags in your LD environment, but I'll paint a bit more color here with some code snippets from my codebase.

Click to open code block ```tsx import { useAsync } from "react-use"; import { useAuth0 } from "@auth0/auth0-react"; import { asyncWithLDProvider, useFlags } from "launchdarkly-react-client-sdk"; export const ldFlags = { "show-teams": false, // Since this flag is commented out, I don't expect it to be evaluted for the user. // "allow-team-invites": false, }; export function useLaunchDarkly(): typeof ldFlags { return useFlags() as typeof ldFlags; } export const LaunchDarklyProvider = ({ children, }: { children: React.ReactElement; }) => { const { isLoading, user } = useAuth0(); const clientSideID = process.env.LAUNCHDARKLY_SDK_CLIENT_SIDE_ID; if (!clientSideID) { throw new Error("LaunchDarkly Client Side ID not set"); } const { value: LDProvider } = useAsync( async () => !isLoading && asyncWithLDProvider({ flags: ldFlags, reactOptions: { useCamelCaseFlagKeys: false }, clientSideID, options: { allAttributesPrivate: true, fetchGoals: false, }, user: user ? { key: user.sub, email: user.email, } : { anonymous: true }, }), [isLoading, user] ); return !isLoading && LDProvider ? ( {children} ) : ( "Loading..." ); }; ```

Once this is done, loading your webapp and inspecting network requests will show calls to /eval and /evalx that include flags that are not in the list of flags provided. The streaming connection appears to also push updates for flags I didn't subscribe to.

Expected behavior I'd like this feature to only subscribe to updates / evaluate the flags I configure so I can make better use of the "last evaluated at" feature on the LD webapp.

SDK version launchdarkly-react-client-sdk 2.26.0

Language version, developer tools react 17.0.2 running in a next app (no SSR related to LD at the moment)

XieX commented 1 year ago

That may be expected behavior. Note that the documentation says:

the React SDK will only subscribe for updates to these flags

I think what you're seeing in the network tab is the JavaScript SDK's traffic, but the React SDK is only listening for changes to the flags you specify in the flags property (the React SDK uses the JavaScript SDK internally). What you should see in the LaunchDarkly flag dashboard for your example is the show-teams flag "Evaluated N seconds ago" after loading your app, but the allow-team-invites flag et al shouldn't. If that's not what you're seeing then it is indeed not behaving as expected.

Having said that, if evaluating the flag at app load time isn't good enough for you (you'd rather it were evaluated when used) there is a workaround when not using the flags option - https://github.com/launchdarkly/react-client-sdk/issues/122#issuecomment-1215157139.

atrakh commented 1 year ago

Thanks for the followup -- I played around with this a bit, and it seems like flags not in the flags configuration do get evaluated, but only for new users or when the flag configuration changes.

Turning options.sendEventsOnlyForVariation did not seem to alter this behavior.

Here's a screenshot of what I saw in the debugger -- I refreshed my page 4 times, and toggled a flag twice

The top 4 flags are in the flags set, and the bottom 2 flags are not:

Screen Shot 2022-08-16 at 9 55 10 AM
XieX commented 1 year ago

This was fixed in 2.27.0, check it out and let me know if you're still having problems.

yusinto commented 1 year ago

@atrakh I'm assuming this is resolved since there's no further updates. I'll close this for now.

atrakh commented 1 year ago

Whoops, I didn't have email notifications on -- yup, I just updated the SDK and flag evaluation seems to work much better!

Thank you!