feat(browser): Tree shake feature flags#3166
Merged
dustinbyrne merged 11 commits intomainfrom Mar 5, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
d760b74 to
c0d9c91
Compare
Contributor
Contributor
|
Size Change: -24 kB (-0.36%) Total Size: 6.65 MB
ℹ️ View Unchanged
|
c0d9c91 to
60847a5
Compare
e5b6d39 to
cf4e2c4
Compare
cf4e2c4 to
b09724d
Compare
eaac7cc to
48bb99d
Compare
This was referenced Feb 26, 2026
a4e8518 to
f3449a9
Compare
602ff2a to
79e7b7a
Compare
marandaneto
approved these changes
Mar 2, 2026
18 tasks
adcfed2 to
13f9506
Compare
5 tasks
Move PostHogFeatureFlags to conditional construction via __extensionClasses. When not included in the bundle, a proxy stub provides graceful degradation with no-op methods. Bootstrap flag logic is moved from posthog-core into PostHogFeatureFlags.initialize(). The updateFlags() method is added to encapsulate flag injection logic previously in posthog-core.
This closes a behavioral gap where `.featureFlags` would return a stub before `init()` is called
- So we can mangle it - Because `featureFlags` should always be available anyway
Still somewhat experimental, but annoying as it can't be disabled.
79e7b7a to
00e88cc
Compare
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.
Problem
PostHogFeatureFlagsis always constructed in the PostHog constructor and deeply referenced throughout posthog-core. It's the largest remaining class that can't be excluded from custom bundles, but it's also the most pervasive, so making it optional requires careful handling of every call site.This is PR 3/3
Changes
Makes
PostHogFeatureFlagstree-shakable via__extensionClasseswith a Proxy-based stub for graceful degradation.posthog-featureflags-stub.ts): When FeatureFlags isn't in the bundle,this.featureFlagsreturns a Proxy that logs an error and returns no-op values for any method call. This avoids crashes or unexpected behavior without requiring optional chaining at every call site (esp. considering end-user code may expect these calls to work)._featureFlags?is the real property;get featureFlags()returns the instance or the stub. Internal code that needs to distinguish "not loaded" from "loaded" uses_featureFlagsdirectly. This is necessary because it allows us to avoid changing thePostHogInterface(i.e., a breaking change).posthog-core._init()intoPostHogFeatureFlags.initialize(), keeping feature-flag-specific logic in its own class (and tree-shakable).updateFlags()method: Flag injection logic moved fromposthog-core.setFeatureFlags()into the FeatureFlags class. Similar reasoning to above.FeatureFlagsExtensionsadded. Bundles that depend on flags (ProductToursExtensions,SurveysExtensions,ExperimentsExtensions) spread...FeatureFlagsExtensionsso flags are automatically included.Bundle size
Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file