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

feat: deprecate featureFlags.override in favor of featureFlags.overrideFeatureFlags, a new function that supports overriding flags and flag payloads #1697

Merged
merged 7 commits into from
Jan 30, 2025

Conversation

dmarticus
Copy link
Contributor

@dmarticus dmarticus commented Jan 28, 2025

Changes

The context for these changes are this feature request (#1304) and this slack thread: https://posthog.slack.com/archives/C07Q2U4BH4L/p1738088710814869. TL;DR – if folks want to use override to manage the value of the feature flag in the client side, I don't see any reason not to.

To make this change ergonomically (I was frustrated by the semantics of override, since to override payloads I'd have to do something like override($flagValue, $some_bool, $payload), instead of just letting users do override($flagValue, $payload) without needing to specify the override warning), I ended up deprecating override and replaced it with the more ergonomic overrideFeatureFlags. Even though override is deprecated, though, it still works – it just calls that new method under the hood. I added the @deprecated flag and a warning so that folks on the latest version of posthog-js move away from it.

Closes #1304

Updated the docs around this change to support using the new method, too: https://github.com/PostHog/posthog.com/pull/10528/files

Checklist

  • Tests for new code (see advice on the tests we use)
  • Accounted for the impact of any changes across different browsers
  • Accounted for backwards compatibility of any changes (no breaking changes in posthog-js!)

Copy link

vercel bot commented Jan 28, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Jan 29, 2025 10:39pm

Comment on lines 153 to 164
const overriddenPayloads = this.instance.get_property(PERSISTENCE_OVERRIDE_FEATURE_FLAG_PAYLOADS)

if (!overriddenPayloads) {
return flagPayloads || {}
}

const finalPayloads = extend({}, flagPayloads || {})
const overriddenKeys = Object.keys(overriddenPayloads)
for (let i = 0; i < overriddenKeys.length; i++) {
finalPayloads[overriddenKeys[i]] = overriddenPayloads[overriddenKeys[i]]
}
return finalPayloads
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same pattern that we do in getFlags

Comment on lines 419 to 420
if (triggerFlagEvent) {
this._fireFeatureFlagsCallbacks()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be sufficient to trigger hooks like useFeatureFlagEnabled, which is what I was yapping about here: https://posthog.slack.com/archives/C07Q2U4BH4L/p1738088710814869

Copy link

github-actions bot commented Jan 28, 2025

Size Change: +10.2 kB (+0.31%)

Total Size: 3.29 MB

Filename Size Change
dist/array.full.es5.js 267 kB +1.04 kB (+0.39%)
dist/array.full.js 370 kB +1.02 kB (+0.28%)
dist/array.full.no-external.js 369 kB +1.02 kB (+0.28%)
dist/array.js 182 kB +1.02 kB (+0.56%)
dist/array.no-external.js 181 kB +1.02 kB (+0.57%)
dist/main.js 183 kB +1.02 kB (+0.56%)
dist/module.full.js 370 kB +1.02 kB (+0.28%)
dist/module.full.no-external.js 369 kB +1.02 kB (+0.28%)
dist/module.js 182 kB +1.02 kB (+0.56%)
dist/module.no-external.js 181 kB +1.02 kB (+0.57%)
ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 215 kB
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 68.8 kB
dist/surveys.js 71.8 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

Comment on lines 385 to 386
* @param {boolean} [triggerFlagEvent] Optional parameter to trigger the _fireFeatureFlagsCallbacks() event.
* If set to true, calling `override` will trigger the callback, which is useful for triggering things like the `useFeatureFlagEnabled` hook.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I make this default true? It's definitely new behavior, but perhaps users want it? Just saw this thread on this type of thing and it seems like the user is expecting that override methods work like bootstrapping methods.

Copy link
Contributor

@havenbarnes havenbarnes Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so and would even go as far as to say we might not even want to add an argument for this? Even our docs seem to communicate overrides as being the same as bootstraps outside of how long the value is persisted: https://posthog.com/docs/feature-flags/bootstrapping#overriding-feature-flags

@dmarticus dmarticus requested a review from havenbarnes January 28, 2025 21:43
Copy link
Contributor

@havenbarnes havenbarnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement and test coverage!

@dmarticus dmarticus added the bump minor Bump minor version when this PR gets merged label Jan 28, 2025
@dmarticus dmarticus changed the title feat(flags sdk): enhance override to support feature flag payload overrides and give users to ability to optionally trigger feature flag callback events from override feat: deprecate featureFlags.override in favor of featureFlags.overrideFeatureFlags Jan 29, 2025
* Override feature flags on the client-side. Useful for setting non-persistent feature flags, or for testing/debugging
* feature flags in the PostHog app.
/**
* @deprecated Use overrideFeatureFlags instead. This will be removed in a future version.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the deprecated tag, so users of this latest version will see the warning in their IDE like this
image

* @deprecated Use overrideFeatureFlags instead. This will be removed in a future version.
*/
override(flags: boolean | string[] | Record<string, string | boolean>, suppressWarning: boolean = false): void {
logger.warn('override is deprecated. Please use overrideFeatureFlags instead.')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging a deprecation warning too; is this overkill? cc @havenbarnes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me, good to have the forcing function so no one gets completely blindsided later when this gets removed

*/
override(flags: boolean | string[] | Record<string, string | boolean>, suppressWarning: boolean = false): void {
overrideFeatureFlags(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implement the new method here

Copy link
Contributor

@havenbarnes havenbarnes Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if we're here changing the shape of the method signature anyway, should we go ahead and transition to a single object parameter (I'd suggest making flags optional too) to keep things nice and clean?

Reason I bring this up is that if someone is only interested in overriding payloads, they will always have to pass some empty value in for flags which is a tad annoying

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled this

Copy link
Contributor

@havenbarnes havenbarnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, left one nit on the new method's arguments. Also we'll wanna update the code example here https://posthog.com/docs/feature-flags/bootstrapping#overriding-feature-flags

@dmarticus dmarticus changed the title feat: deprecate featureFlags.override in favor of featureFlags.overrideFeatureFlags feat: deprecate featureFlags.override in favor of featureFlags.overrideFeatureFlags, a new function that supports both override flags and overriding flag payloads Jan 29, 2025
@dmarticus dmarticus changed the title feat: deprecate featureFlags.override in favor of featureFlags.overrideFeatureFlags, a new function that supports both override flags and overriding flag payloads feat: deprecate featureFlags.override in favor of featureFlags.overrideFeatureFlags, a new function that supports overriding flags and flag payloads Jan 29, 2025
@dmarticus
Copy link
Contributor Author

Looks good, left one nit on the new method's arguments. Also we'll wanna update the code example here posthog.com/docs/feature-flags/bootstrapping#overriding-feature-flags

yup, doing that here: PostHog/posthog.com#10528

@dmarticus dmarticus merged commit 690773b into main Jan 30, 2025
32 checks passed
@dmarticus dmarticus deleted the feat/support-payload-overrides-and-override-reloads branch January 30, 2025 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature flag override does not fetch new payload
2 participants