diff --git a/readme.txt b/readme.txt index 5d77ddc24..c013aefee 100644 --- a/readme.txt +++ b/readme.txt @@ -259,6 +259,7 @@ You can report security bugs through the Patchstack Vulnerability Disclosure Pro = 8.0 - Coming soon 2026 = * 🌱**OptiMax** OptiMax to maximize the page score. +* **Optimize** Fixed JS Delay mode not executing `load` event listeners. (#629) = 7.9 - Aug 5 2026 = * **Cloud** Changed Health service to run asynchronously, resuming through WP cron when the cloud defers the request. diff --git a/src/optimize.cls.php b/src/optimize.cls.php index 8b50f0d63..ce1557a52 100644 --- a/src/optimize.cls.php +++ b/src/optimize.cls.php @@ -88,15 +88,14 @@ public function init() { if ($this->cfg_js_defer == 2) { add_filter( 'litespeed_optm_cssjs', - function ( $con, $file_type ) { - if ($file_type == 'js') { - $con = str_replace('DOMContentLoaded', 'DOMContentLiteSpeedLoaded', $con); - // $con = str_replace( 'addEventListener("load"', 'addEventListener("litespeedLoad"', $con ); + function ( $con, $file_type, $src ) { + if ($file_type == 'js' && !Utility::str_hit_array($src, $this->cfg_js_defer_exc)) { + $con = $this->_replace_js_events( $con ); } return $con; }, 20, - 2 + 3 ); } @@ -1005,8 +1004,8 @@ private function _js_inline_defer( $con, $attrs = false, $minified = false ) { if ($this->cfg_js_defer === 2) { // Drop type attribute from $attrs $attrs = Utility::remove_attr( $attrs, 'type' ); - // Replace DOMContentLoaded - $con = str_replace('DOMContentLoaded', 'DOMContentLiteSpeedLoaded', $con); + // Replace DOMContentLoaded and load event listeners + $con = $this->_replace_js_events( $con ); return '' . $con . ''; // return ''; // return '' . $con . ''; @@ -1015,6 +1014,22 @@ private function _js_inline_defer( $con, $attrs = false, $minified = false ) { return ''; } + /** + * Replace JS DOMContentLoaded and load event listeners for JS Delay mode + * + * @since 8.0 + */ + private function _replace_js_events( $con ) { + if ( empty( $con ) || ! is_string( $con ) ) { + return $con; + } + + $con = str_replace( 'DOMContentLoaded', 'DOMContentLiteSpeedLoaded', $con ); + $con = preg_replace( '/(?