Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useFeatureFlagEnabled returns an incorrect value when flag is enabled #1714

Open
mwickman opened this issue Feb 4, 2025 · 7 comments
Open
Assignees

Comments

@mwickman
Copy link

mwickman commented Feb 4, 2025

Hi, thank you for your hard work on this library!

I'm using posthog-js and posthog-js/react, version ^1.215.1

I have a feature flag which is enabled, and sometimes useFeatureFlagEnabled returns false when the value of my flag has never been set to false. This is easy to recreate when I log the user out.

Typescript indicates that useFeatureFlagEnabled can return a boolean or undefined, and it seems logical to me it would return undefined instead of false if it has not yet fetched a value. However in the network tools I can see that I've fetched from /decide multiple times and each time the only value returned was true, but the hook still gave false.

Perhaps I just need clarification that we should never expect to get back undefined, and that false will be returned if posthog has failed to fetch flags, but I struggled to find any documentation about this. Otherwise it appears to be a bug that posthog would return a value it never received from its server.

update: I've tried to solve the problem by bootstrapping the flag values but still see false returned when it seems like there should be no scenarios left where that is a possible return value.
Image

Thank you,
Matt

@havenbarnes havenbarnes self-assigned this Feb 4, 2025
@havenbarnes
Copy link
Contributor

Hi @mwickman , thanks for the report. I am digging into this, in the meantime could you also provide a snippet where you use useFeatureFlagEnabled for this flag? Thanks!

@mwickman
Copy link
Author

mwickman commented Feb 4, 2025

Thank you! Here is a screenshot of the code that calls useFeatureFlagEnabled

Image

@havenbarnes
Copy link
Contributor

Thanks for the code @mwickman ! Quick follow-up, are you able to tell if your hook is returning false / undefined persistently, or is it only for the first render?

I am looking into a similar report from another PostHog user and we've realized that the lack of a default value (due to compatibility w/ NextJS) causes hooks to return undefined initially: https://github.com/PostHog/posthog-js/blob/main/react/src/hooks/useFeatureFlagEnabled.ts#L7-L9

Let me know if you agree that this is the same root cause / symptom or something else - I'm already working on addressing that specific issue so hopefully I can help you both out with the same code change!

@havenbarnes
Copy link
Contributor

#1714

Possible fix for you @mwickman , depending on if I've diagnosed your problem accurately!

@mwickman
Copy link
Author

mwickman commented Feb 7, 2025

Thanks so much Haven!

In my case I do not see the issue on first render, but after a number of renders have already occurred and the user has signed out, where we call posthog.reset(). I would actually like to get back undefined if posthog does not yet know the correct value as that lets me choose a default value easily, but false if fine as long as I understand that may mean we haven't fetched flags yet and I can do the flag bootstrap.

I suspect my problem is related to the posthog.reset() that we do when signing out. The flow is roughly like this:

  1. feature flag is enabled for all users
  2. flag evaluates to true
  3. user signs in and we call posthog.identify()
  4. flag is still true
  5. user signs out and we call posthog.reset()
  6. flag is now false, seemingly ignoring both the bootstrapped value and the real value which are both true

@havenbarnes
Copy link
Contributor

Ah, yes the reset() call would do it. I believe this is because reset() clears every bit of data, including whatever local storage your SDK is using which stores the bootstrapped values passed in from the SDK config. It looks like we are not re-populating the bootstrap feature flags.

IMO, this is actually intended behavior. Typically, bootstrapped values are passed from a server-side call which would be user-specific. So, we wouldn't want this SDK to re-populate old bootstrapped values as they could be completely wrong for the newly logged out user.

For now, if you need bootstrap values to persist after a logout, I'd recommend either re-initializing the SDK with your bootstrap values after the signout, or if you want to permanent overrides, you can just use overrideFeatureFlags after the posthog.reset() call with your flag values

@mwickman
Copy link
Author

So you are saying that if I have a feature flag set to True in posthog, then call reset(), its expected that I'll no longer get my feature flag values back correctly from that point? That is what I'm experiencing; after the reset() call a flag which should be enabled for all users is returning false. What should I call to have the flags start getting returned correctly again? Thanks again for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants