diff --git a/src/__tests__/utils/request-router.test.ts b/src/__tests__/utils/request-router.test.ts index 9116b2116..887f16307 100644 --- a/src/__tests__/utils/request-router.test.ts +++ b/src/__tests__/utils/request-router.test.ts @@ -19,11 +19,19 @@ describe('request-router', () => { ['https://us.posthog.com', 'ui', 'https://us.posthog.com'], ['https://us.posthog.com', 'assets', 'https://us-assets.i.posthog.com'], ['https://us.posthog.com', 'api', 'https://us.i.posthog.com'], + ['https://us.i.posthog.com', 'api', 'https://us.i.posthog.com'], + ['https://us.i.posthog.com', 'assets', 'https://us-assets.i.posthog.com'], + ['https://us-assets.i.posthog.com', 'assets', 'https://us-assets.i.posthog.com'], + ['https://us-assets.i.posthog.com', 'api', 'https://us.i.posthog.com'], // EU domain ['https://eu.posthog.com', 'ui', 'https://eu.posthog.com'], ['https://eu.posthog.com', 'assets', 'https://eu-assets.i.posthog.com'], ['https://eu.posthog.com', 'api', 'https://eu.i.posthog.com'], + ['https://eu.i.posthog.com', 'api', 'https://eu.i.posthog.com'], + ['https://eu.i.posthog.com', 'assets', 'https://eu-assets.i.posthog.com'], + ['https://eu-assets.i.posthog.com', 'assets', 'https://eu-assets.i.posthog.com'], + ['https://eu-assets.i.posthog.com', 'api', 'https://eu.i.posthog.com'], // custom domain ['https://my-custom-domain.com', 'ui', 'https://my-custom-domain.com'], diff --git a/src/utils/request-router.ts b/src/utils/request-router.ts index 7838c37f4..83f50d808 100644 --- a/src/utils/request-router.ts +++ b/src/utils/request-router.ts @@ -16,6 +16,7 @@ export type RequestRouterTarget = 'api' | 'ui' | 'assets' export class RequestRouter { instance: PostHog + private _regionCache: Record = {} constructor(instance: PostHog) { this.instance = instance @@ -29,15 +30,17 @@ export class RequestRouter { } get region(): RequestRouterRegion { - switch (this.apiHost) { - case 'https://app.posthog.com': - case 'https://us.posthog.com': - return RequestRouterRegion.US - case 'https://eu.posthog.com': - return RequestRouterRegion.EU - default: - return RequestRouterRegion.CUSTOM + // We don't need to compute this every time so we cache the result + if (!this._regionCache[this.apiHost]) { + if (/https:\/\/(app|us|us-assets)(\.i)?\.posthog\.com/i.test(this.apiHost)) { + this._regionCache[this.apiHost] = RequestRouterRegion.US + } else if (/https:\/\/(eu|eu-assets)(\.i)?\.posthog\.com/i.test(this.apiHost)) { + this._regionCache[this.apiHost] = RequestRouterRegion.EU + } else { + this._regionCache[this.apiHost] = RequestRouterRegion.CUSTOM + } } + return this._regionCache[this.apiHost] } endpointFor(target: RequestRouterTarget, path: string = ''): string {