: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(heatmaps): don't crash the scene on zero-height canvas reads (#72446)
Steven Shults1b079f5fd4c5
diff --git a/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsx b/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsxnew file mode 100644index 000000000000..d92b118cc405--- /dev/null+++ b/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsx@@ -0,0 +1,59 @@+import { fireEvent, render } from '@testing-library/react'++import { initKeaTests } from '~/test/init'++import { HeatmapCanvas } from './HeatmapCanvas'++const mockSetData = jest.fn()+const mockConfigure = jest.fn()+const mockGetValueAt = jest.fn()++jest.mock('heatmap.js', () => ({+ __esModule: true,+ default: {+ create: () => ({+ setData: (...args: unknown[]) => mockSetData(...args),+ configure: (...args: unknown[]) => mockConfigure(...args),+ getValueAt: (...args: unknown[]) => mockGetValueAt(...args),+ }),+ },+}))++// When the heatmap container has zero height at create time (iframe/data still loading),+// heatmap.js's canvas reads throw: IndexSizeError from getImageData in Chromium, a raw+// NS_ERROR_FAILURE value in Firefox. These must not crash the scene.+describe('HeatmapCanvas', () => {+ beforeEach(() => {+ initKeaTests()+ mockSetData.mockReset()+ mockConfigure.mockReset()+ mockGetValueAt.mockReset()+ })++ it('does not crash when configure throws on a zero-height canvas', () => {+ mockConfigure.mockImplementation(() => {+ throw new DOMException(+ "Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source height is 0.",+ 'IndexSizeError'+ )+ })++ const { container } = render(<HeatmapCanvas context="in-app" />)++ expect(mockConfigure).toHaveBeenCalled()+ expect(container.querySelector('[data-attr="heatmap-canvas"]')).toBeTruthy()+ })++ it('does not crash when getValueAt throws during mouse tracking', () => {+ mockGetValueAt.mockImplementation(() => {+ // Firefox throws a raw non-Error value from a broken canvas+ throw 'NS_ERROR_FAILURE'+ })++ const { container } = render(<HeatmapCanvas context="in-app" />)+ fireEvent.mouseMove(window, { clientX: 10, clientY: 10 })++ expect(mockGetValueAt).toHaveBeenCalled()+ expect(container.querySelector('[data-attr="heatmap-canvas"]')).toBeTruthy()+ })+})diff --git a/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx b/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsxindex e66344c72b3e..964d92215f3a 100644--- a/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx+++ b/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx@@ -41,7 +41,15 @@ function HeatmapMouseInfo({const containerMousePosition = useMousePosition(containerRef?.current)const viewportMousePosition = useMousePosition()- const value = heatmapJsRef.current?.getValueAt(containerMousePosition)+ let value: number | undefined+ try {+ value = heatmapJsRef.current?.getValueAt(containerMousePosition)+ } catch {+ // heatmap.js throws reading its canvas if it was created while the container had+ // zero height (IndexSizeError in Chromium, raw NS_ERROR_FAILURE in Firefox);+ // this runs on every mouse move, so swallow rather than crash the scene+ value = undefined+ }const hasValue = !!(containerMousePosition && (value || shiftPressed)) && !heatmapTooltipSuppressed@@ -193,11 +201,16 @@ export function HeatmapCanvas({return}- heatmapsJsRef.current?.configure({- ...HEATMAP_CONFIG,- container: heatmapsJsContainerRef.current,- gradient: heatmapJSColorGradient,- })+ try {+ heatmapsJsRef.current?.configure({+ ...HEATMAP_CONFIG,+ container: heatmapsJsContainerRef.current,+ gradient: heatmapJSColorGradient,+ })+ } catch (e) {+ // configure re-renders the canvas, which throws if it was created zero-height+ console.error('error configuring heatmap', e)+ }}, [heatmapJSColorGradient])if (!heatmapFilters.enabled) {