Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 22 additions & 7 deletions src/optimize.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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 '<script' . $attrs . ' type="litespeed/javascript">' . $con . '</script>';
// return '<script' . $attrs . ' type="litespeed/javascript" src="data:text/javascript;base64,' . base64_encode( $con ) . '"></script>';
// return '<script' . $attrs . ' type="litespeed/javascript">' . $con . '</script>';
Expand All @@ -1015,6 +1014,22 @@ private function _js_inline_defer( $con, $attrs = false, $minified = false ) {
return '<script' . $attrs . ' src="data:text/javascript;base64,' . base64_encode($con) . '" defer></script>';
}

/**
* 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( '/(?<!\.)\b(window\.|document\.)?addEventListener\(\s*([\'"])load\2/', '$1addEventListener($2DOMContentLiteSpeedLoaded$2', $con );
$con = preg_replace( '/((?:\$|jQuery)\(\s*(?:window|document)\s*\)\s*\.on\(\s*)([\'"])load(?=[.\s\'"])/', '$1$2DOMContentLiteSpeedLoaded', $con );
return $con;
}

/**
* Replace ESI to JS inline var (mainly used to avoid nonce timeout)
*
Expand Down
Loading