fix(optimize): replace load event listeners in JS Delay mode - #1031
fix(optimize): replace load event listeners in JS Delay mode#1031faisalahammad wants to merge 3 commits into
Conversation
|
@faisalahammad please make sure there is just 1 change per PR. I see multiple changes(different bugs) added. |
- Replace DOMContentLoaded and load event listeners with DOMContentLiteSpeedLoaded for delayed scripts - Scope load event replacements to window/document to avoid affecting element-level image/XHR listeners - Preserves window/document prefixes and enforces string subject guard Addresses PR feedback. Refs litespeedtech#1031
dc223f3 to
996ba50
Compare
|
Updated PR to contain only 1 change for JS Delay load event listener replacement. Removed all other unrelated commits. |
|
@faisalahammad I will look over the code on Monday. Thank you |
- Change _replace_js_events @SInCE from 7.0 to 8.0 - Preserve jQuery event namespaces on rewritten load listeners - Add 8.0 changelog entry for JS Delay load event fix Addresses PR feedback. Refs litespeedtech#1031 Signed-off-by: Faisal Ahammad <faisalahammad24@gmail.com>
|
Confirmed. Kept this PR to one change. Also fixed the new method @SInCE to 8.0 and added an 8.0 changelog entry. |
|
Fixed the JS Delay exclusion regression in b02282e. The litespeed_optm_cssjs callback now accepts the third source argument and skips load-event rewriting when the source matches cfg_js_defer_exc. Non-JS content remains unchanged. Verified with PHP lint, PHPCS, focused exclusion/event rewrite tests, and an independent code review. No changes were made to the separate event-pattern coverage items. |
Summary
Fixes issue where scripts delayed by LiteSpeed Cache (
optm-js_defer = 2/ JS Delay mode) attaching event listeners toloadevent fail to execute because the native browserloadevent fires before delayed scripts run.Fixes #629
Changes
src/optimize.cls.phpBefore:
After:
Why: Replaces
DOMContentLoadedandloadevent listeners onwindowanddocumentwithDOMContentLiteSpeedLoadedin JS Delay mode. Preserves element-level listeners (such as imageloadorXMLHttpRequestloadlisteners).Testing
Test 1: JS Delay Event Listener Replacement
optm-js_defer = 2) in LiteSpeed Cache settings.window.addEventListener('load', ...)or$(window).on('load', ...)orwindow.onload = ....Result:
loadevent handlers execute cleanly upon dispatch ofDOMContentLiteSpeedLoaded.Test 2: Element-Level Load Listeners
img.addEventListener('load', ...)).Result: Element-level image and XHR load listeners are preserved untouched and function as expected.