From 509f8eee3e674882dd4bc6975ee4c3a1940ea0a0 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 1 Jan 2024 20:03:50 +0000 Subject: [PATCH] [core] separate init.js for code which needs to run before page load --- core/basepage.php | 27 ++++++++++++++++++++++++++- ext/static_files/init.js | 6 ++++++ ext/static_files/script.js | 9 --------- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 ext/static_files/init.js diff --git a/core/basepage.php b/core/basepage.php index bd41e826..f151e2f9 100644 --- a/core/basepage.php +++ b/core/basepage.php @@ -380,6 +380,9 @@ class BasePage $css_cache_file = $this->get_css_cache_file($theme_name, $config_latest); $this->add_html_header("", 43); + $initjs_cache_file = $this->get_initjs_cache_file($theme_name, $config_latest); + $this->add_html_header("", 44); + $js_cache_file = $this->get_js_cache_file($theme_name, $config_latest); $this->add_html_header("", 44); } @@ -407,6 +410,29 @@ class BasePage return $css_cache_file; } + private function get_initjs_cache_file(string $theme_name, int $config_latest): string + { + $js_latest = $config_latest; + $js_files = array_merge( + zglob("ext/{" . Extension::get_enabled_extensions_as_string() . "}/init.js"), + zglob("themes/$theme_name/init.js") + ); + foreach ($js_files as $js) { + $js_latest = max($js_latest, filemtime($js)); + } + $js_md5 = md5(serialize($js_files)); + $js_cache_file = data_path("cache/initscript/{$theme_name}.{$js_latest}.{$js_md5}.js"); + if (!file_exists($js_cache_file)) { + $mcss = new \MicroBundler\MicroBundler(); + foreach($js_files as $js) { + $mcss->addSource($js, file_get_contents($js)); + } + $mcss->save($js_cache_file); + } + + return $js_cache_file; + } + private function get_js_cache_file(string $theme_name, int $config_latest): string { $js_latest = $config_latest; @@ -435,7 +461,6 @@ class BasePage return $js_cache_file; } - /** * @return array A list of stylesheets relative to the theme root. */ diff --git a/ext/static_files/init.js b/ext/static_files/init.js new file mode 100644 index 00000000..d45f34e3 --- /dev/null +++ b/ext/static_files/init.js @@ -0,0 +1,6 @@ +function shm_cookie_set(name, value) { + Cookies.set(name, value, {expires: 365, samesite: "lax", path: "/"}); +} +function shm_cookie_get(name) { + return Cookies.get(name); +} diff --git a/ext/static_files/script.js b/ext/static_files/script.js index 61eb16b3..889e1483 100644 --- a/ext/static_files/script.js +++ b/ext/static_files/script.js @@ -1,12 +1,3 @@ -/*jshint bitwise:false, curly:true, eqeqeq:true, evil:true, forin:false, noarg:true, noempty:true, nonew:true, undef:false, strict:false, browser:true */ - -function shm_cookie_set(name, value) { - Cookies.set(name, value, {expires: 365, samesite: "lax", path: "/"}); -} -function shm_cookie_get(name) { - return Cookies.get(name); -} - document.addEventListener('DOMContentLoaded', () => { /** Load jQuery extensions **/ //Code via: https://stackoverflow.com/a/13106698