@posthog/browser-common 0.9.6 → 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.js CHANGED
@@ -27,7 +27,7 @@ var __webpack_require__ = {};
27
27
  })();
28
28
  var __webpack_exports__ = {};
29
29
  __webpack_require__.r(__webpack_exports__);
30
- const packageVersion = "0.9.6";
30
+ const packageVersion = "0.9.8";
31
31
  const Config = {
32
32
  DEBUG: false,
33
33
  LIB_VERSION: packageVersion,
package/dist/config.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const packageVersion = "0.9.6";
1
+ const packageVersion = "0.9.8";
2
2
  const Config = {
3
3
  DEBUG: false,
4
4
  LIB_VERSION: packageVersion,
@@ -75,6 +75,7 @@ export declare class PostHogFeatureFlags implements Extension {
75
75
  private _reloadingDisabled;
76
76
  private _additionalReloadRequested;
77
77
  private _reloadDebouncer;
78
+ private _firingFeatureFlagsCallbacks;
78
79
  private _flagsLoadedFromRemote;
79
80
  private _staleCacheRefreshTriggered;
80
81
  private _consecutiveStatusZeroFailures;
@@ -206,6 +206,7 @@ class PostHogFeatureFlags {
206
206
  this._requestGeneration = 0;
207
207
  this._reloadingDisabled = false;
208
208
  this._additionalReloadRequested = false;
209
+ this._firingFeatureFlagsCallbacks = false;
209
210
  this._flagsLoadedFromRemote = false;
210
211
  this._staleCacheRefreshTriggered = false;
211
212
  this._consecutiveStatusZeroFailures = 0;
@@ -598,6 +599,7 @@ class PostHogFeatureFlags {
598
599
  reloadFeatureFlags() {
599
600
  if (this._reloadingDisabled || this._config.featureFlagsDisabled) return;
600
601
  if (this._hasStatusZeroCircuitBreakerTripped()) return;
602
+ if (this._firingFeatureFlagsCallbacks) this._logger.warn("reloadFeatureFlags() was called inside an onFeatureFlags callback. This callback also runs when flags change in other tabs, so this can cause a request loop.");
601
603
  if (this._requestInFlight) this._additionalReloadRequested = true;
602
604
  if (this._reloadDebouncer) return;
603
605
  this._reloadingHandlers.slice().forEach((handler)=>{
@@ -809,46 +811,53 @@ class PostHogFeatureFlags {
809
811
  [external_constants_js_namespaceObject.FLAG_CALL_REPORTED]: flagCallReported,
810
812
  [external_constants_js_namespaceObject.FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
811
813
  });
812
- } else if (key in flagCallReported && flagCallReported[key].includes(flagReportValue)) {
813
- if (sessionIdToPersist) this._set({
814
- [external_constants_js_namespaceObject.FLAG_CALL_REPORTED]: flagCallReported,
815
- [external_constants_js_namespaceObject.FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
816
- });
817
814
  } else {
818
- if ((0, core_namespaceObject.isArray)(flagCallReported[key])) flagCallReported[key].push(flagReportValue);
819
- else flagCallReported[key] = [
820
- flagReportValue
821
- ];
822
- this._set({
815
+ const canCapture = this._client?.canCapture;
816
+ if (!canCapture && Object.keys(flagCallReported).length > 0) {
817
+ flagCallReported = {};
818
+ this._set({
819
+ [external_constants_js_namespaceObject.FLAG_CALL_REPORTED]: flagCallReported
820
+ });
821
+ }
822
+ if (canCapture && !flagCallReported[key]?.includes(flagReportValue)) {
823
+ if ((0, core_namespaceObject.isArray)(flagCallReported[key])) flagCallReported[key].push(flagReportValue);
824
+ else flagCallReported[key] = [
825
+ flagReportValue
826
+ ];
827
+ this._set({
828
+ [external_constants_js_namespaceObject.FLAG_CALL_REPORTED]: flagCallReported,
829
+ ...sessionIdToPersist ? {
830
+ [external_constants_js_namespaceObject.FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
831
+ } : {}
832
+ });
833
+ const flagDetails = this.getFeatureFlagDetails(key);
834
+ const errors = [
835
+ ...this._prop(external_constants_js_namespaceObject.PERSISTENCE_FEATURE_FLAG_ERRORS) ?? []
836
+ ];
837
+ if ((0, core_namespaceObject.isUndefined)(flagValue)) errors.push(FeatureFlagError.FLAG_MISSING);
838
+ const properties = {
839
+ $feature_flag: key,
840
+ $feature_flag_response: flagValue,
841
+ $feature_flag_payload: payload ?? null,
842
+ $feature_flag_request_id: requestId,
843
+ $feature_flag_evaluated_at: evaluatedAt,
844
+ $feature_flag_bootstrapped_response: this._config.bootstrap?.featureFlags?.[key] ?? null,
845
+ $feature_flag_bootstrapped_payload: this._config.bootstrap?.featureFlagPayloads?.[key] ?? null,
846
+ $used_bootstrap_value: !this._flagsLoadedFromRemote
847
+ };
848
+ if (!(0, core_namespaceObject.isUndefined)(flagDetails?.metadata?.has_experiment)) properties.$feature_flag_has_experiment = flagDetails.metadata.has_experiment;
849
+ if (!(0, core_namespaceObject.isUndefined)(flagDetails?.metadata?.version)) properties.$feature_flag_version = flagDetails.metadata.version;
850
+ const reason = flagDetails?.reason?.description ?? flagDetails?.reason?.code;
851
+ if (reason) properties.$feature_flag_reason = reason;
852
+ if (flagDetails?.metadata?.id) properties.$feature_flag_id = flagDetails.metadata.id;
853
+ if (!(0, core_namespaceObject.isUndefined)(flagDetails?.original_variant) || !(0, core_namespaceObject.isUndefined)(flagDetails?.original_enabled)) properties.$feature_flag_original_response = (0, core_namespaceObject.isUndefined)(flagDetails.original_variant) ? flagDetails.original_enabled : flagDetails.original_variant;
854
+ if (flagDetails?.metadata?.original_payload) properties.$feature_flag_original_payload = flagDetails?.metadata?.original_payload;
855
+ if (errors.length) properties.$feature_flag_error = errors.join(',');
856
+ this._captureFeatureFlagCalled(properties);
857
+ } else if (sessionIdToPersist) this._set({
823
858
  [external_constants_js_namespaceObject.FLAG_CALL_REPORTED]: flagCallReported,
824
- ...sessionIdToPersist ? {
825
- [external_constants_js_namespaceObject.FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
826
- } : {}
859
+ [external_constants_js_namespaceObject.FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
827
860
  });
828
- const flagDetails = this.getFeatureFlagDetails(key);
829
- const errors = [
830
- ...this._prop(external_constants_js_namespaceObject.PERSISTENCE_FEATURE_FLAG_ERRORS) ?? []
831
- ];
832
- if ((0, core_namespaceObject.isUndefined)(flagValue)) errors.push(FeatureFlagError.FLAG_MISSING);
833
- const properties = {
834
- $feature_flag: key,
835
- $feature_flag_response: flagValue,
836
- $feature_flag_payload: payload ?? null,
837
- $feature_flag_request_id: requestId,
838
- $feature_flag_evaluated_at: evaluatedAt,
839
- $feature_flag_bootstrapped_response: this._config.bootstrap?.featureFlags?.[key] ?? null,
840
- $feature_flag_bootstrapped_payload: this._config.bootstrap?.featureFlagPayloads?.[key] ?? null,
841
- $used_bootstrap_value: !this._flagsLoadedFromRemote
842
- };
843
- if (!(0, core_namespaceObject.isUndefined)(flagDetails?.metadata?.has_experiment)) properties.$feature_flag_has_experiment = flagDetails.metadata.has_experiment;
844
- if (!(0, core_namespaceObject.isUndefined)(flagDetails?.metadata?.version)) properties.$feature_flag_version = flagDetails.metadata.version;
845
- const reason = flagDetails?.reason?.description ?? flagDetails?.reason?.code;
846
- if (reason) properties.$feature_flag_reason = reason;
847
- if (flagDetails?.metadata?.id) properties.$feature_flag_id = flagDetails.metadata.id;
848
- if (!(0, core_namespaceObject.isUndefined)(flagDetails?.original_variant) || !(0, core_namespaceObject.isUndefined)(flagDetails?.original_enabled)) properties.$feature_flag_original_response = (0, core_namespaceObject.isUndefined)(flagDetails.original_variant) ? flagDetails.original_enabled : flagDetails.original_variant;
849
- if (flagDetails?.metadata?.original_payload) properties.$feature_flag_original_payload = flagDetails?.metadata?.original_payload;
850
- if (errors.length) properties.$feature_flag_error = errors.join(',');
851
- this._captureFeatureFlagCalled(properties);
852
861
  }
853
862
  if (!flagExists) return;
854
863
  return {
@@ -1114,6 +1123,8 @@ class PostHogFeatureFlags {
1114
1123
  _fireFeatureFlagsCallbacks(errorsLoading) {
1115
1124
  this._rebuildEventProperties();
1116
1125
  const { flags, flagVariants } = this._prepareFeatureFlagsForCallbacks();
1126
+ const wasFiring = this._firingFeatureFlagsCallbacks;
1127
+ this._firingFeatureFlagsCallbacks = true;
1117
1128
  this.featureFlagEventHandlers.forEach((handler)=>{
1118
1129
  try {
1119
1130
  handler(flags, flagVariants, {
@@ -1123,6 +1134,7 @@ class PostHogFeatureFlags {
1123
1134
  this._logger.error('Error while running feature flags callback', error);
1124
1135
  }
1125
1136
  });
1137
+ this._firingFeatureFlagsCallbacks = wasFiring;
1126
1138
  }
1127
1139
  setPersonPropertiesForFlags(properties, reloadFeatureFlags = true) {
1128
1140
  this._setPersonPropertiesForFlags(properties, reloadFeatureFlags);
@@ -170,6 +170,7 @@ class PostHogFeatureFlags {
170
170
  this._requestGeneration = 0;
171
171
  this._reloadingDisabled = false;
172
172
  this._additionalReloadRequested = false;
173
+ this._firingFeatureFlagsCallbacks = false;
173
174
  this._flagsLoadedFromRemote = false;
174
175
  this._staleCacheRefreshTriggered = false;
175
176
  this._consecutiveStatusZeroFailures = 0;
@@ -562,6 +563,7 @@ class PostHogFeatureFlags {
562
563
  reloadFeatureFlags() {
563
564
  if (this._reloadingDisabled || this._config.featureFlagsDisabled) return;
564
565
  if (this._hasStatusZeroCircuitBreakerTripped()) return;
566
+ if (this._firingFeatureFlagsCallbacks) this._logger.warn("reloadFeatureFlags() was called inside an onFeatureFlags callback. This callback also runs when flags change in other tabs, so this can cause a request loop.");
565
567
  if (this._requestInFlight) this._additionalReloadRequested = true;
566
568
  if (this._reloadDebouncer) return;
567
569
  this._reloadingHandlers.slice().forEach((handler)=>{
@@ -773,46 +775,53 @@ class PostHogFeatureFlags {
773
775
  [FLAG_CALL_REPORTED]: flagCallReported,
774
776
  [FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
775
777
  });
776
- } else if (key in flagCallReported && flagCallReported[key].includes(flagReportValue)) {
777
- if (sessionIdToPersist) this._set({
778
- [FLAG_CALL_REPORTED]: flagCallReported,
779
- [FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
780
- });
781
778
  } else {
782
- if (isArray(flagCallReported[key])) flagCallReported[key].push(flagReportValue);
783
- else flagCallReported[key] = [
784
- flagReportValue
785
- ];
786
- this._set({
779
+ const canCapture = this._client?.canCapture;
780
+ if (!canCapture && Object.keys(flagCallReported).length > 0) {
781
+ flagCallReported = {};
782
+ this._set({
783
+ [FLAG_CALL_REPORTED]: flagCallReported
784
+ });
785
+ }
786
+ if (canCapture && !flagCallReported[key]?.includes(flagReportValue)) {
787
+ if (isArray(flagCallReported[key])) flagCallReported[key].push(flagReportValue);
788
+ else flagCallReported[key] = [
789
+ flagReportValue
790
+ ];
791
+ this._set({
792
+ [FLAG_CALL_REPORTED]: flagCallReported,
793
+ ...sessionIdToPersist ? {
794
+ [FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
795
+ } : {}
796
+ });
797
+ const flagDetails = this.getFeatureFlagDetails(key);
798
+ const errors = [
799
+ ...this._prop(PERSISTENCE_FEATURE_FLAG_ERRORS) ?? []
800
+ ];
801
+ if (isUndefined(flagValue)) errors.push(FeatureFlagError.FLAG_MISSING);
802
+ const properties = {
803
+ $feature_flag: key,
804
+ $feature_flag_response: flagValue,
805
+ $feature_flag_payload: payload ?? null,
806
+ $feature_flag_request_id: requestId,
807
+ $feature_flag_evaluated_at: evaluatedAt,
808
+ $feature_flag_bootstrapped_response: this._config.bootstrap?.featureFlags?.[key] ?? null,
809
+ $feature_flag_bootstrapped_payload: this._config.bootstrap?.featureFlagPayloads?.[key] ?? null,
810
+ $used_bootstrap_value: !this._flagsLoadedFromRemote
811
+ };
812
+ if (!isUndefined(flagDetails?.metadata?.has_experiment)) properties.$feature_flag_has_experiment = flagDetails.metadata.has_experiment;
813
+ if (!isUndefined(flagDetails?.metadata?.version)) properties.$feature_flag_version = flagDetails.metadata.version;
814
+ const reason = flagDetails?.reason?.description ?? flagDetails?.reason?.code;
815
+ if (reason) properties.$feature_flag_reason = reason;
816
+ if (flagDetails?.metadata?.id) properties.$feature_flag_id = flagDetails.metadata.id;
817
+ if (!isUndefined(flagDetails?.original_variant) || !isUndefined(flagDetails?.original_enabled)) properties.$feature_flag_original_response = isUndefined(flagDetails.original_variant) ? flagDetails.original_enabled : flagDetails.original_variant;
818
+ if (flagDetails?.metadata?.original_payload) properties.$feature_flag_original_payload = flagDetails?.metadata?.original_payload;
819
+ if (errors.length) properties.$feature_flag_error = errors.join(',');
820
+ this._captureFeatureFlagCalled(properties);
821
+ } else if (sessionIdToPersist) this._set({
787
822
  [FLAG_CALL_REPORTED]: flagCallReported,
788
- ...sessionIdToPersist ? {
789
- [FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
790
- } : {}
823
+ [FLAG_CALL_REPORTED_SESSION_ID]: sessionIdToPersist
791
824
  });
792
- const flagDetails = this.getFeatureFlagDetails(key);
793
- const errors = [
794
- ...this._prop(PERSISTENCE_FEATURE_FLAG_ERRORS) ?? []
795
- ];
796
- if (isUndefined(flagValue)) errors.push(FeatureFlagError.FLAG_MISSING);
797
- const properties = {
798
- $feature_flag: key,
799
- $feature_flag_response: flagValue,
800
- $feature_flag_payload: payload ?? null,
801
- $feature_flag_request_id: requestId,
802
- $feature_flag_evaluated_at: evaluatedAt,
803
- $feature_flag_bootstrapped_response: this._config.bootstrap?.featureFlags?.[key] ?? null,
804
- $feature_flag_bootstrapped_payload: this._config.bootstrap?.featureFlagPayloads?.[key] ?? null,
805
- $used_bootstrap_value: !this._flagsLoadedFromRemote
806
- };
807
- if (!isUndefined(flagDetails?.metadata?.has_experiment)) properties.$feature_flag_has_experiment = flagDetails.metadata.has_experiment;
808
- if (!isUndefined(flagDetails?.metadata?.version)) properties.$feature_flag_version = flagDetails.metadata.version;
809
- const reason = flagDetails?.reason?.description ?? flagDetails?.reason?.code;
810
- if (reason) properties.$feature_flag_reason = reason;
811
- if (flagDetails?.metadata?.id) properties.$feature_flag_id = flagDetails.metadata.id;
812
- if (!isUndefined(flagDetails?.original_variant) || !isUndefined(flagDetails?.original_enabled)) properties.$feature_flag_original_response = isUndefined(flagDetails.original_variant) ? flagDetails.original_enabled : flagDetails.original_variant;
813
- if (flagDetails?.metadata?.original_payload) properties.$feature_flag_original_payload = flagDetails?.metadata?.original_payload;
814
- if (errors.length) properties.$feature_flag_error = errors.join(',');
815
- this._captureFeatureFlagCalled(properties);
816
825
  }
817
826
  if (!flagExists) return;
818
827
  return {
@@ -1078,6 +1087,8 @@ class PostHogFeatureFlags {
1078
1087
  _fireFeatureFlagsCallbacks(errorsLoading) {
1079
1088
  this._rebuildEventProperties();
1080
1089
  const { flags, flagVariants } = this._prepareFeatureFlagsForCallbacks();
1090
+ const wasFiring = this._firingFeatureFlagsCallbacks;
1091
+ this._firingFeatureFlagsCallbacks = true;
1081
1092
  this.featureFlagEventHandlers.forEach((handler)=>{
1082
1093
  try {
1083
1094
  handler(flags, flagVariants, {
@@ -1087,6 +1098,7 @@ class PostHogFeatureFlags {
1087
1098
  this._logger.error('Error while running feature flags callback', error);
1088
1099
  }
1089
1100
  });
1101
+ this._firingFeatureFlagsCallbacks = wasFiring;
1090
1102
  }
1091
1103
  setPersonPropertiesForFlags(properties, reloadFeatureFlags = true) {
1092
1104
  this._setPersonPropertiesForFlags(properties, reloadFeatureFlags);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/browser-common",
3
- "version": "0.9.6",
3
+ "version": "0.9.8",
4
4
  "description": "Internal shared browser utilities and extension primitives for PostHog Browser SDKs",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -218,8 +218,8 @@
218
218
  },
219
219
  "dependencies": {
220
220
  "preact": "^10.29.3",
221
- "@posthog/core": "^1.59.3",
222
- "@posthog/types": "^1.416.0"
221
+ "@posthog/types": "^1.416.0",
222
+ "@posthog/core": "^1.59.4"
223
223
  },
224
224
  "devDependencies": {
225
225
  "@rsbuild/plugin-react": "^2.1.0",