js-sdk-contrib icon indicating copy to clipboard operation
js-sdk-contrib copied to clipboard

Flagsmith: Inconsistent Identity Propagation in `FlagsmithClientProvider` on Context Updates

Open belaczek opened this issue 8 months ago • 2 comments

Description

When using Flagsmith as an OpenFeature provider, updating the context does not always trigger an identity API call. This results in:

  • Missing traits in the /identities API request
  • Incorrect segmentation behavior

The issue arises when the provider is set before the context.

Steps to Reproduce

Working Flow

This sequence correctly triggers the /identities endpoint with the expected traits:

const flagsmithClient = new FlagsmithClient();
await OpenFeature.setContext({ targetingKey: 'id', traitA: 'abc' });
OpenFeature.setProvider(flagsmithClient);

Non-Working Flow

In this case, the identity API call is not made after setting the context, resulting in empty traits:

const flagsmithClient = new FlagsmithClient();
OpenFeature.setProvider(flagsmithClient);
await OpenFeature.setContext({ targetingKey: 'id', traitA: 'abc' });

Observed Behavior

  • Without a targetingKey, segmentation defaults to environment-level settings.
  • Features that should be disabled remain enabled, with a "STATIC" reason.
  • Updating the context after setting the provider does not trigger a re-fetch with the correct identity data.

Expected Behavior

  • Whenever context is updated, the provider should correctly propagate identity and traits and fetch correct state of flags for target user.

Possible Cause

In FlagsmithClientProvider, when the context updates, the provider calls:

this._client.getFlags();

However, this does not include the new identity information. Updating this call to:

this._client.identify(identity, context);

resolves the issue.

🔍 [Problematic line in the source code](https://github.com/open-feature/js-sdk-contrib/blob/main/libs/providers/flagsmith-client/src/lib/flagsmith-client-provider.ts#L56).

Environment

  • OpenFeature JS SDK with Flagsmith as the provider.

belaczek avatar Mar 17 '25 15:03 belaczek

Would a maintainer please assign this issue to me? Thanks

rolodato avatar Mar 17 '25 15:03 rolodato

I think I ran into this problem as well with the React SDK. Using flagsmith.setContext(...) to identify the user seems to trigger a fetch but does not use the correct context so the result is the default flags. Going back to using flagsmith.identify(...) seems to work even though it seems labeled as deprecated.

AnotherHermit avatar Jul 03 '25 11:07 AnotherHermit