Fix Chart.js loading issue in weather charts

- Added check for Chart.js availability before rendering
- Updated chart hooks to wait for Chart.js to load completely
- Prevents 'window.Chart is not a constructor' errors

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2025-07-27 16:35:24 -05:00
parent 387a10b8eb
commit 02d9879b79
No known key found for this signature in database
2 changed files with 27 additions and 5 deletions

View file

@ -189,11 +189,26 @@ Object.keys(WeatherChartHooks).forEach(hookName => {
Hooks[hookName] = {
...originalHook,
mounted() {
if (window.VendorLoader) {
const self = this;
if (window.VendorLoader && !window.chartBundleLoaded) {
// Load chart bundle and wait for it to complete
window.VendorLoader.loadCharts();
}
if (originalHook.mounted) {
originalHook.mounted.call(this);
// Wait a bit for charts to load, then call mounted
const checkChartLoaded = () => {
if (window.Chart) {
if (originalHook.mounted) {
originalHook.mounted.call(self);
}
} else {
setTimeout(checkChartLoaded, 50);
}
};
setTimeout(checkChartLoaded, 100);
} else {
// Chart bundle already loaded or loading, call mounted
if (originalHook.mounted) {
originalHook.mounted.call(this);
}
}
}
};

View file

@ -1,5 +1,5 @@
// Chart.js and date adapter are loaded globally from vendor bundle
const Chart = window.Chart;
// We'll access it later when it's actually loaded
import type { ChartConfiguration, ChartType } from 'chart.js';
import type { WeatherChartDataset, YAxisOptions } from '../types/chart-types';
@ -301,6 +301,13 @@ function createChartHook(configKey: string): Hook {
}
};
// Check if Chart.js is loaded
if (!window.Chart) {
console.warn('Chart.js not loaded yet, retrying...');
setTimeout(() => self.renderChart(), 100);
return;
}
self.chart = new window.Chart(canvas, chartConfig);
};