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(insights): guard autofill sink focus during navigation (#86795)

posthog[bot]18a71acb77e4
diff --git a/frontend/src/lib/memory/autofillReleaseLogic.test.ts b/frontend/src/lib/memory/autofillReleaseLogic.test.ts
index 7ba41be1fd9f..a829bf0bbe51 100644
--- a/frontend/src/lib/memory/autofillReleaseLogic.test.ts
+++ b/frontend/src/lib/memory/autofillReleaseLogic.test.ts
@@ -1,5 +1,6 @@
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
+import posthog from 'posthog-js'
import { initKeaTests } from '~/test/init'
@@ -54,4 +55,21 @@ describe('autofillReleaseLogic', () => {
expect(focusSpy).toHaveBeenCalledTimes(shouldFocus ? 1 : 0)
})
+
+ it('completes the scene change and reports the error if focusing the sink throws', async () => {
+ await expectLogic(logic, () => router.actions.push('/insights')).toDispatchActions(['locationChanged'])
+
+ const sink = document.querySelector(SINK_SELECTOR) as HTMLInputElement
+ // a downstream blur handler on an unmounting form can throw when focus() moves off it
+ const error = new TypeError('y is not a function')
+ jest.spyOn(sink, 'focus').mockImplementation(() => {
+ throw error
+ })
+
+ await expectLogic(logic, () => router.actions.push('/dashboard')).toDispatchActions(['locationChanged'])
+
+ expect(router.values.location.pathname).toContain('/dashboard')
+ // the failure is swallowed for navigation, but must still reach error tracking
+ expect(posthog.captureException).toHaveBeenCalledWith(error, expect.any(Object))
+ })
})
diff --git a/frontend/src/lib/memory/autofillReleaseLogic.ts b/frontend/src/lib/memory/autofillReleaseLogic.ts
index a78adab7944e..c21155b3f870 100644
--- a/frontend/src/lib/memory/autofillReleaseLogic.ts
+++ b/frontend/src/lib/memory/autofillReleaseLogic.ts
@@ -1,6 +1,7 @@
import { MakeLogicType, afterMount, connect, kea, listeners, path } from 'kea'
import { router } from 'kea-router'
import type { LocationChangedPayload } from 'kea-router/lib/types'
+import posthog from 'posthog-js'
const SINK_ID = 'autofill-release-sink'
@@ -111,10 +112,25 @@ export const autofillReleaseLogic = kea<autofillReleaseLogicType>([
if (!sink) {
return
}
- // preventScroll: focusing must never scroll the (off-screen) sink
- // into view
- sink.focus({ preventScroll: true })
- sink.blur()
+ // Focusing the sink fires a native blur on whatever input the user
+ // had focused in the outgoing scene. When that input is a kea-forms
+ // <Field>, React runs its onBlur, which touches the field. The
+ // field's form logic is already unmounting with the scene, so the
+ // touch action is undefined and throws. That throw propagates out of
+ // focus(), escapes this listener, and aborts the navigation the user
+ // asked for. Releasing the autofill reference is best-effort, so
+ // swallow a downstream handler's failure and let navigation finish.
+ // preventScroll keeps focus() from scrolling the off-screen sink into
+ // view.
+ try {
+ sink.focus({ preventScroll: true })
+ sink.blur()
+ } catch (e) {
+ // report the downstream handler's failure so the underlying defect
+ // stays visible in error tracking; navigation must still complete,
+ // so we don't rethrow
+ posthog.captureException(e, { context: 'autofillReleaseLogic sink focus' })
+ }
},
})),
])