Skip to content

Commit 0244146

Browse files
committed
fix(pie-chart): make loadData SSR-safe and avoid repeated Chart.js warnings
1 parent 1dbe8f5 commit 0244146

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/app/component/pie-chart/pie-chart.component.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ declare const Chart: any;
1818
})
1919
export class PieChartComponent implements OnInit, OnChanges, AfterViewInit {
2020

21+
/** Prevents repeated logs when chart library is unavailable */
22+
private hasLoggedChartUnavailable = false;
23+
2124
/** Dark mode flag from user settings */
2225
isDarkMode: boolean = false;
2326

@@ -99,16 +102,25 @@ export class PieChartComponent implements OnInit, OnChanges, AfterViewInit {
99102

100103
/** Loads chart data based on the selected view type */
101104
loadData(): void {
102-
if (typeof window !== 'undefined' && (window as any).Chart) {
103-
if (this.viewType === 'month') {
104-
this.loadMonthData();
105-
} else if (this.viewType === 'year') {
106-
this.loadYearData();
107-
} else if (this.viewType === 'day') {
108-
this.loadDayData();
105+
// Skip chart rendering during SSR/prerender where `window` is unavailable.
106+
if (typeof window === 'undefined') {
107+
return;
108+
}
109+
110+
if (!(window as any).Chart) {
111+
if (!this.hasLoggedChartUnavailable) {
112+
console.warn('Chart.js is not loaded properly.');
113+
this.hasLoggedChartUnavailable = true;
109114
}
110-
} else {
111-
console.error("Chart.js is not loaded properly.");
115+
return;
116+
}
117+
118+
if (this.viewType === 'month') {
119+
this.loadMonthData();
120+
} else if (this.viewType === 'year') {
121+
this.loadYearData();
122+
} else if (this.viewType === 'day') {
123+
this.loadDayData();
112124
}
113125
}
114126

0 commit comments

Comments
 (0)