Skip to content

Commit 5ddd644

Browse files
authored
Improve charts JS (hestiacp#3519)
* Improve charts JS * Remove unused support for page-specific JS * Update deprecated flag * Tidy CSRF error page * Only load Chart.js bundle when needed
1 parent 8df92ef commit 5ddd644

File tree

7 files changed

+25
-22
lines changed

7 files changed

+25
-22
lines changed

docs/docs/contributing/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ multipass mount $HOME/projects/hestiacp hestia-dev:/home/ubuntu/hestiacp
3535
1. Create an Ubuntu VM with at least 2G of memory and 15G of disk space
3636

3737
```bash
38-
multipass launch --name hestia-dev --mem 2G --disk 15G
38+
multipass launch --name hestia-dev --memory 2G --disk 15G
3939
```
4040

4141
1. Map your cloned repository to the VM's home directory

web/inc/main.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ function check_return_code_redirect($return_var, $output, $location) {
180180

181181
function render_page($user, $TAB, $page) {
182182
$__template_dir = dirname(__DIR__) . "/templates/";
183-
$__pages_js_dir = dirname(__DIR__) . "/js/pages/";
184183

185184
// Extract global variables
186185
// I think those variables should be passed via arguments
@@ -198,11 +197,6 @@ function render_page($user, $TAB, $page) {
198197
// Body
199198
include $__template_dir . "pages/" . $page . ".php";
200199

201-
// Including page specific js file
202-
if (file_exists($__pages_js_dir . $page . ".js")) {
203-
echo '<script defer src="/js/pages/' . $page . ".js?" . JS_LATEST_UPDATE . '"></script>';
204-
}
205-
206200
// Footer
207201
include $__template_dir . "footer.php";
208202
}

web/inc/prevent_csrf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ function checkStrictness($level) {
2929
return true;
3030
} else {
3131
http_response_code(400);
32-
echo "<h1>Potential use CSRF detected</h1>\n" .
32+
echo "<h1>Potential CSRF use detected</h1>\n" .
3333
"<p>Please disable any plugins/add-ons inside your browser or contact your system administrator. If you are the system administrator you can run v-change-sys-config-value 'POLICY_CSRF_STRICTNESS' '0' as root to disable this check.<p>" .
34-
"<p>If you followed a bookmark or an static link <a href='/'>please click here</a>";
34+
"<p>If you followed a bookmark or an static link please <a href='/'>navigate to root</a>";
3535
die();
3636
}
3737
}

web/js/dist/main.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/dist/main.min.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import handleSyncEmailValues from './syncEmailValues';
2323
import handleTabPanels from './tabPanels';
2424
import handleToggleAdvanced from './toggleAdvanced';
2525
import handleUnlimitedInput from './unlimitedInput';
26+
import initRrdCharts from './rrdCharts';
2627
import * as helpers from './helpers';
2728

2829
window.Hestia = { helpers };
@@ -48,6 +49,7 @@ function initListeners() {
4849
handleSyncEmailValues();
4950
handleTabPanels();
5051
handleToggleAdvanced();
52+
initRrdCharts();
5153
}
5254

5355
document.addEventListener('alpine:init', () => {
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
async function init() {
2-
const Chart = await loadChartJs();
1+
// Create Chart.js charts from in-page data on Task Monitor page
2+
export default async function initRrdCharts() {
33
const chartCanvases = document.querySelectorAll('.js-rrd-chart');
44

5+
if (!chartCanvases.length) {
6+
return;
7+
}
8+
9+
const Chart = await loadChartJs();
10+
511
for (const chartCanvas of chartCanvases) {
612
const service = chartCanvas.dataset.service;
713
const period = chartCanvas.dataset.period;
@@ -18,8 +24,11 @@ async function init() {
1824
}
1925

2026
async function loadChartJs() {
21-
const module = await import('/js/dist/chart.js-auto.min.js');
22-
return module.Chart;
27+
// NOTE: String expression used to prevent ESBuild from resolving
28+
// the import on build (Chart.js is a separate bundle)
29+
const chartJsBundlePath = '/js/dist/chart.js-auto.min.js';
30+
const chartJsModule = await import(`${chartJsBundlePath}`);
31+
return chartJsModule.Chart;
2332
}
2433

2534
async function fetchRrdData(service, period) {
@@ -103,5 +112,3 @@ function getChartOptions(unit) {
103112
},
104113
};
105114
}
106-
107-
init();

0 commit comments

Comments
 (0)