Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 3, 2024
1 parent 6ff863f commit e5c506a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe('Decide', () => {
get_distinct_id: jest.fn().mockImplementation(() => 'distinctid'),
_send_request: jest.fn().mockImplementation(({ callback }) => callback?.({ config: {} })),
featureFlags: {
resetRequestQueue: jest.fn(),
reloadFeatureFlags: jest.fn(),
receivedFeatureFlags: jest.fn(),
setReloadingPaused: jest.fn(),
_startReloadTimer: jest.fn(),
Expand Down Expand Up @@ -305,5 +307,20 @@ describe('Decide', () => {
})
expect(posthog._onRemoteConfig).toHaveBeenCalledWith(config)
})

it.each([
[true, true],
[false, false],
[undefined, true],
])('conditionally reloads feature flags - hasFlags: %s, shouldReload: %s', (hasFeatureFlags, shouldReload) => {
assignableWindow._POSTHOG_CONFIG = { hasFeatureFlags } as RemoteConfig
decide().call()

if (shouldReload) {
expect(posthog.featureFlags.reloadFeatureFlags).toHaveBeenCalled()
} else {
expect(posthog.featureFlags.reloadFeatureFlags).not.toHaveBeenCalled()
}
})
})
})
22 changes: 21 additions & 1 deletion src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export class Decide {
}

call(): void {
// Call decide to get what features are enabled and other settings.
// As a reminder, if the /decide endpoint is disabled, feature flags, toolbar, session recording, autocapture,
// and compression will not be available.
const disableRemoteCalls = !!this.instance.config.advanced_disable_decide

if (!disableRemoteCalls) {
// TRICKY: Reset any decide reloads queued during config.loaded because they'll be
// covered by the decide call right above.
this.instance.featureFlags.resetRequestQueue()
}

if (this.instance.config.__preview_remote_config) {
// Attempt 1 - use the pre-loaded config if it came as part of the token-specific array.js
if (assignableWindow._POSTHOG_CONFIG) {
Expand All @@ -51,6 +62,11 @@ export class Decide {
return
}

if (disableRemoteCalls) {
logger.warn('Remote config is disabled. Falling back to local config.')
return
}

// Attempt 2 - if we have the external deps loader then lets load the script version of the config that includes site apps
this._loadRemoteConfigJs((config) => {
if (!config) {
Expand Down Expand Up @@ -138,6 +154,10 @@ export class Decide {

this.instance._onRemoteConfig(config)

// Additionally trigger loading of flags if necessary
if (config.hasFeatureFlags !== false) {
// If the config has feature flags, we need to call decide to get the feature flags
// This completely separates it from the config logic which is good in terms of separation of concerns
this.instance.featureFlags.reloadFeatureFlags()
}
}
}
11 changes: 1 addition & 10 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,7 @@ export class PostHog {
}, 1)
}

// Call decide to get what features are enabled and other settings.
// As a reminder, if the /decide endpoint is disabled, feature flags, toolbar, session recording, autocapture,
// and compression will not be available.
if (!disableDecide) {
new Decide(this).call()

// TRICKY: Reset any decide reloads queued during config.loaded because they'll be
// covered by the decide call right above.
this.featureFlags.resetRequestQueue()
}
new Decide(this).call()
}

_start_queue_if_opted_in(): void {
Expand Down

0 comments on commit e5c506a

Please sign in to comment.