Skip to content

Commit

Permalink
Fix up loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 3, 2024
1 parent c32f364 commit 6ff863f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/__tests__/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Decide } from '../decide'
import { PostHogPersistence } from '../posthog-persistence'
import { RequestRouter } from '../utils/request-router'
import { PostHog } from '../posthog-core'
import { DecideResponse, PostHogConfig, Properties } from '../types'
import { DecideResponse, PostHogConfig, Properties, RemoteConfig } from '../types'
import '../entrypoints/external-scripts-loader'
import { assignableWindow } from '../utils/globals'

const expectDecodedSendRequest = (
send_request: PostHog['_send_request'],
Expand Down Expand Up @@ -246,4 +247,63 @@ describe('Decide', () => {
expect(posthog.featureFlags.receivedFeatureFlags).not.toHaveBeenCalled()
})
})

describe('remote config', () => {
const config = { surveys: true } as RemoteConfig

beforeEach(() => {
posthog.config.__preview_remote_config = true
assignableWindow._POSTHOG_CONFIG = undefined
assignableWindow.POSTHOG_DEBUG = true

assignableWindow.__PosthogExtensions__.loadExternalDependency = jest.fn(
(_ph: PostHog, _name: string, cb: (err?: any) => void) => {
assignableWindow._POSTHOG_CONFIG = config as RemoteConfig
cb()
}
)

posthog._send_request = jest.fn().mockImplementation(({ callback }) => callback?.({ json: config }))
})

it('properly pulls from the window and uses it if set', () => {
assignableWindow._POSTHOG_CONFIG = config as RemoteConfig
decide().call()

expect(assignableWindow.__PosthogExtensions__.loadExternalDependency).not.toHaveBeenCalled()
expect(posthog._send_request).not.toHaveBeenCalled()

expect(posthog._onRemoteConfig).toHaveBeenCalledWith(config)
})

it('loads the script if window config not set', () => {
decide().call()

expect(assignableWindow.__PosthogExtensions__.loadExternalDependency).toHaveBeenCalledWith(
posthog,
'remote-config',
expect.any(Function)
)
expect(posthog._send_request).not.toHaveBeenCalled()
expect(posthog._onRemoteConfig).toHaveBeenCalledWith(config)
})

it('loads the json if window config not set and js failed', () => {
assignableWindow.__PosthogExtensions__.loadExternalDependency = jest.fn(
(_ph: PostHog, _name: string, cb: (err?: any) => void) => {
cb()
}
)

decide().call()

expect(assignableWindow.__PosthogExtensions__.loadExternalDependency).toHaveBeenCalled()
expect(posthog._send_request).toHaveBeenCalledWith({
method: 'GET',
url: 'https://test.com/array/testtoken/config',
callback: expect.any(Function),
})
expect(posthog._onRemoteConfig).toHaveBeenCalledWith(config)
})
})
})
5 changes: 5 additions & 0 deletions src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export class Decide {
assignableWindow.__PosthogExtensions__?.loadExternalDependency?.(this.instance, 'remote-config', () => {
return cb(assignableWindow._POSTHOG_CONFIG)
})
} else {
logger.error('PostHog Extensions not found. Cannot load remote config.')
cb()
}
}

Expand All @@ -43,13 +46,15 @@ export class Decide {
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) {
logger.info('Using preloaded remote config', assignableWindow._POSTHOG_CONFIG)
this.onRemoteConfig(assignableWindow._POSTHOG_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) {
logger.info('No config found after loading remote JS config. Falling back to JSON.')
// Attempt 3 Load the config json instead of the script - we won't get site apps etc. but we will get the config
this._loadRemoteConfigJSON((config) => {
this.onRemoteConfig(config)
Expand Down

0 comments on commit 6ff863f

Please sign in to comment.