Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: prevent Billboard.js null reference errors on chart destroy

Blue Mousecd0eb92110b7
diff --git a/web/app/pages/Project/View/ViewProject.helpers.tsx b/web/app/pages/Project/View/ViewProject.helpers.tsx
index cc7503d1c..4c3c3736f 100644
--- a/web/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/web/app/pages/Project/View/ViewProject.helpers.tsx
@@ -1011,7 +1011,13 @@ const getSettings = (
// - when refunds value is 0, remove stroke to avoid "cap" lines
const chart = this as any
- if (chart?.$ && chart.$.bar?.bars) {
+ // Guard against accessing chart internals after destruction
+ // This prevents "Cannot read properties of null" errors
+ if (!chart || !chart.$ || !chart.$.bar?.bars) {
+ return
+ }
+
+ try {
chart.$.bar.bars.each(function (this: SVGPathElement, d: any) {
const value = Number(d?.value || 0)
@@ -1039,6 +1045,8 @@ const getSettings = (
}
}
})
+ } catch {
+ // Ignore errors during render if chart is being destroyed
}
}
: undefined,
diff --git a/web/app/pages/Project/View/components/ChartManager.tsx b/web/app/pages/Project/View/components/ChartManager.tsx
index 7f12d2a1a..96b628bed 100644
--- a/web/app/pages/Project/View/components/ChartManager.tsx
+++ b/web/app/pages/Project/View/components/ChartManager.tsx
@@ -55,12 +55,12 @@ export const ChartManagerProvider = ({
const destroyChart = useCallback((id: string) => {
const chart = chartsRef.current.get(id)
if (chart) {
+ chartsRef.current.delete(id)
try {
chart.destroy()
} catch {
// ignore destroy errors
}
- chartsRef.current.delete(id)
}
}, [])
diff --git a/web/app/ui/BillboardChart.tsx b/web/app/ui/BillboardChart.tsx
index dd93391fc..4cc75e078 100644
--- a/web/app/ui/BillboardChart.tsx
+++ b/web/app/ui/BillboardChart.tsx
@@ -18,6 +18,7 @@ const BillboardChart = ({
}: BillboardChartProps) => {
const containerRef = useRef<HTMLDivElement | null>(null)
const chartRef = useRef<Chart | null>(null)
+ const isDestroyingRef = useRef(false)
const mergedDeps = useMemo(
() => deps || [options, dataNames],
@@ -29,21 +30,89 @@ const BillboardChart = ({
return
}
+ // Destroy previous chart synchronously if exists (for deps changes)
if (chartRef.current) {
+ isDestroyingRef.current = true
try {
chartRef.current.destroy()
} catch {
- // ignore
+ // ignore destroy errors
}
chartRef.current = null
+ isDestroyingRef.current = false
if (onReady) onReady(null)
}
- const opts: ChartOptions = {
+ // Wrap callbacks to guard against destroyed chart access
+ const wrappedOptions: ChartOptions = {
...options,
bindto: containerRef.current as unknown as HTMLElement,
}
- const chart = billboard.generate(opts)
+
+ // Wrap onrendered callback to prevent access after destroy
+ if (options.onrendered) {
+ const originalOnRendered = options.onrendered
+ wrappedOptions.onrendered = function (this: Chart) {
+ if (isDestroyingRef.current || !chartRef.current) {
+ return
+ }
+ try {
+ originalOnRendered.call(this)
+ } catch {
+ // ignore errors during render if chart is being destroyed
+ }
+ }
+ }
+
+ // Wrap onresize callback
+ if (options.onresize) {
+ const originalOnResize = options.onresize
+ wrappedOptions.onresize = function (this: Chart) {
+ if (isDestroyingRef.current || !chartRef.current) {
+ return
+ }
+ try {
+ originalOnResize.call(this)
+ } catch {
+ // ignore
+ }
+ }
+ }
+
+ // Wrap onresized callback
+ if (options.onresized) {
+ const originalOnResized = options.onresized
+ wrappedOptions.onresized = function (this: Chart) {
+ if (isDestroyingRef.current || !chartRef.current) {
+ return
+ }
+ try {
+ originalOnResized.call(this)
+ } catch {
+ // ignore
+ }
+ }
+ }
+
+ // Wrap zoom.onzoom callback to prevent access after destroy
+ if (options.zoom?.onzoom) {
+ const originalOnZoom = options.zoom.onzoom
+ wrappedOptions.zoom = {
+ ...options.zoom,
+ onzoom: function (this: Chart, domain: [Date, Date]) {
+ if (isDestroyingRef.current || !chartRef.current) {
+ return
+ }
+ try {
+ originalOnZoom.call(this, domain)
+ } catch {
+ // ignore errors if chart is being destroyed
+ }
+ },
+ }
+ }
+
+ const chart = billboard.generate(wrappedOptions)
chartRef.current = chart
if (dataNames) {
@@ -57,13 +126,21 @@ const BillboardChart = ({
if (onReady) onReady(chart)
return () => {
- if (!chartRef.current) return
+ const chartToDestroy = chartRef.current
+ if (!chartToDestroy) return
+
+ isDestroyingRef.current = true
+ chartRef.current = null
+
+ // Destroy synchronously - the wrapped callbacks will check isDestroyingRef
+ // to prevent accessing chart internals during destruction
try {
- chartRef.current.destroy()
+ chartToDestroy.destroy()
} catch {
- // ignore
+ // ignore destroy errors
}
- chartRef.current = null
+
+ isDestroyingRef.current = false
if (onReady) onReady(null)
}
// eslint-disable-next-line react-hooks/exhaustive-deps