Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(proxy): make configure automatically work in mobile safari (#120427)

Meikel Ratz0e261e72b091
diff --git a/frontend/src/lib/components/DomainConnect/domainConnectLogic.test.ts b/frontend/src/lib/components/DomainConnect/domainConnectLogic.test.ts
new file mode 100644
index 000000000000..5127735b9e6f
--- /dev/null
+++ b/frontend/src/lib/components/DomainConnect/domainConnectLogic.test.ts
@@ -0,0 +1,74 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { domainConnectLogic } from './domainConnectLogic'
+
+const APPLY_URL = 'https://dns.example.com/v2/domainTemplates/providers/posthog.com/services/proxy/apply?sig=fake'
+const APPLY_URL_ENDPOINT = '/api/environments/:team_id/integrations/domain-connect/apply-url/'
+
+interface FakeProviderTab {
+ opener: unknown
+ location: { href: string }
+ close: jest.Mock
+}
+
+describe('domainConnectLogic', () => {
+ const originalLocation = window.location
+ let logic: ReturnType<typeof domainConnectLogic.build>
+ let providerTab: FakeProviderTab
+
+ function useApplyUrlResponse(status: number, body: Record<string, unknown>): void {
+ useMocks({ post: { [APPLY_URL_ENDPOINT]: () => [status, body] } })
+ }
+
+ beforeEach(() => {
+ useApplyUrlResponse(200, { url: APPLY_URL })
+ initKeaTests()
+ providerTab = { opener: window, location: { href: '' }, close: jest.fn() }
+ jest.spyOn(window, 'open').mockReturnValue(providerTab as unknown as Window)
+ Object.defineProperty(window, 'location', {
+ value: { ...originalLocation, assign: jest.fn() },
+ configurable: true,
+ })
+ logic = domainConnectLogic({ logicKey: 'test', domain: null, context: 'proxy', proxyRecordId: 'record-1' })
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic.unmount()
+ Object.defineProperty(window, 'location', { value: originalLocation, configurable: true })
+ jest.restoreAllMocks()
+ })
+
+ it('opens the provider tab during the tap, because mobile Safari blocks a tab opened after a request', async () => {
+ logic.actions.openDomainConnect()
+ expect(window.open).toHaveBeenCalledTimes(1)
+
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(providerTab.location.href).toEqual(APPLY_URL)
+ expect(providerTab.opener).toBeNull()
+ expect(window.location.assign).not.toHaveBeenCalled()
+ })
+
+ it('falls back to the current tab when the browser refuses to open a new one', async () => {
+ jest.mocked(window.open).mockReturnValue(null)
+
+ logic.actions.openDomainConnect()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(window.location.assign).toHaveBeenCalledWith(APPLY_URL)
+ })
+
+ it('closes the blank provider tab when the apply URL request fails', async () => {
+ useApplyUrlResponse(400, { detail: 'Proxy record not found' })
+
+ logic.actions.openDomainConnect()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(providerTab.close).toHaveBeenCalled()
+ expect(providerTab.location.href).toEqual('')
+ })
+})
diff --git a/frontend/src/lib/components/DomainConnect/domainConnectLogic.ts b/frontend/src/lib/components/DomainConnect/domainConnectLogic.ts
index e1e2e4472048..2ccdb7449648 100644
--- a/frontend/src/lib/components/DomainConnect/domainConnectLogic.ts
+++ b/frontend/src/lib/components/DomainConnect/domainConnectLogic.ts
@@ -85,6 +85,22 @@ export type domainConnectLogicType = MakeLogicType<
domainConnectLogicMeta
>
+function openBlankProviderTab(): Window | null {
+ const providerTab = window.open('', '_blank')
+ if (providerTab) {
+ providerTab.opener = null
+ }
+ return providerTab
+}
+
+function sendProviderTabTo(providerTab: Window | null, url: string): void {
+ if (providerTab) {
+ providerTab.location.href = url
+ } else {
+ window.location.assign(url)
+ }
+}
+
export const domainConnectLogic = kea<domainConnectLogicType>([
path((key) => ['lib', 'components', 'DomainConnect', 'domainConnectLogic', key]),
props({} as DomainConnectLogicProps),
@@ -131,6 +147,7 @@ export const domainConnectLogic = kea<domainConnectLogicType>([
}),
listeners(({ props }) => ({
openDomainConnect: async ({ providerEndpoint }) => {
+ const providerTab = openBlankProviderTab()
const currentUrl = window.location.href.split('?')[0]
const redirectUri = `${currentUrl}?domain_connect=${props.context}`
@@ -142,8 +159,9 @@ export const domainConnectLogic = kea<domainConnectLogicType>([
redirect_uri: redirectUri,
provider_endpoint: providerEndpoint,
})
- window.open(url, '_blank', 'noopener,noreferrer')
+ sendProviderTabTo(providerTab, url)
} catch (e) {
+ providerTab?.close()
if (e instanceof ApiError) {
lemonToast.error(`Failed to generate Domain Connect URL: ${e.detail || 'Please try again.'}`)
} else {