feat(flags): add fresh option to feature flag methods#3119
Merged
Conversation
Add a `fresh` option to `getFeatureFlag()`, `getFeatureFlagResult()`, and
`isFeatureEnabled()` that only returns values loaded from the server, not
cached localStorage values.
By default, these methods may return cached values from localStorage if the
`/flags` endpoint hasn't responded yet. This reduces flicker but means you
might briefly see stale values (e.g., a flag that was disabled on the server).
With `{ fresh: true }`, the methods return `undefined` until the `/flags`
endpoint responds, ensuring you always get the current server state.
Usage:
```js
// Default behavior (may return cached values)
posthog.getFeatureFlag('my-flag')
// Only return fresh values from server
posthog.getFeatureFlag('my-flag', { fresh: true })
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Contributor
|
Size Change: +970 B (+0.02%) Total Size: 6.15 MB
ℹ️ View Unchanged
|
gustavohstrassburger
approved these changes
Feb 19, 2026
| * @param {Object|String} options (optional) If {send_event: false}, we won't send an $feature_flag_call event to PostHog. | ||
| * If {fresh: true}, we won't return cached values from localStorage - only values loaded from the server. | ||
| */ | ||
| getFeatureFlag(key: string, options?: { send_event?: boolean }): boolean | string | undefined { |
Contributor
There was a problem hiding this comment.
We have the type options?: { send_event?: boolean; fresh?: boolean } in several places, maybe worth creating a type or an interface for it an centralize it.
Extract the inline `{ send_event?: boolean; fresh?: boolean }` options
type into a centralized `FeatureFlagOptions` type in @posthog/types.
This reduces duplication across 9 occurrences in 3 files and provides
a single source of truth for the feature flag lookup options.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
freshoption togetFeatureFlag(),getFeatureFlagResult(), andisFeatureEnabled()that only returns values loaded from the server, not cached localStorage values.Problem
By default, feature flag methods may return cached values from localStorage if the
/flagsendpoint hasn't responded yet. This reduces flicker but means users might briefly see stale values - for example, a flag that was disabled on the server would still return its cached value until the network request completes.This was causing issues where disabled flags continued to show their enabled variants to users who had previously been enrolled.
Solution
Add a non-breaking
{ fresh: true }option that returnsundefineduntil the/flagsendpoint responds, ensuring you always get the current server state.Changes
fresh?: booleanoption togetFeatureFlag(),getFeatureFlagResult(), andisFeatureEnabled()fresh: true, returnsundefinedif_flagsLoadedFromRemoteis falsefresh: true