Skip to content

Commit

Permalink
Add option to use passive event listeners to improve scroll performance
Browse files Browse the repository at this point in the history
  • Loading branch information
riklewis committed Mar 21, 2022
1 parent c08819c commit 851d857
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ This plugin will allow you to easily remove bloat and turn off unused features,
It also includes the following additional functionality:
- Include [instant.page](https://instant.page) library (v5.1.0) with settings
- Add Server-Timing headers to enable better debugging
- Use passive event listengers to improve scroll performance

This plugin is NOT a caching plugin, but should play well with any caching plugin you decide to use.
20 changes: 18 additions & 2 deletions svn/trunk/better-speed.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ function better_speed_wp_enqueue_scripts() {
wp_deregister_script('heartbeat');
}
}

//jQuery passive event listeners
if(better_speed_check_other_setting('others-passive')) {
wp_add_inline_script('jquery', 'jQuery.event.special.touchstart={setup:function(e,t,s){this.addEventListener("touchstart",s,{passive:!t.includes("noPreventDefault")})}},jQuery.event.special.touchmove={setup:function(e,t,s){this.addEventListener("touchmove",s,{passive:!t.includes("noPreventDefault")})}},jQuery.event.special.wheel={setup:function(e,t,s){this.addEventListener("wheel",s,{passive:!0})}},jQuery.event.special.mousewheel={setup:function(e,t,s){this.addEventListener("mousewheel",s,{passive:!0})}};', 'after');
}
}
add_action('wp_enqueue_scripts', 'better_speed_wp_enqueue_scripts');

Expand Down Expand Up @@ -396,7 +401,8 @@ function better_speed_settings() {
add_settings_field('better-speed-features-revisions', __('Revisions', 'whysoslow'), 'better_speed_features_revisions', 'better-speed', 'better-speed-section-features');

add_settings_section('better-speed-section-others', __('Settings', 'whysoslow'), 'better_speed_section_others', 'better-speed-others');
add_settings_field('better-speed-others-timings', __('Server Timings', 'whysoslow'), 'better_speed_others_timings', 'better-speed-others', 'better-speed-section-others');
add_settings_field('better-speed-others-timings', __('Add Server Timings', 'whysoslow'), 'better_speed_others_timings', 'better-speed-others', 'better-speed-section-others');
add_settings_field('better-speed-others-passive', __('Use Passive Listeners', 'whysoslow'), 'better_speed_others_passive', 'better-speed-others', 'better-speed-section-others');

add_settings_section('better-speed-section-instant', __('Instant Page', 'whysoslow'), 'better_speed_section_instant', 'better-speed-instant');
add_settings_field('better-speed-instant-page', __('Instant Page', 'whysoslow'), 'better_speed_instant_page', 'better-speed-instant', 'better-speed-section-instant');
Expand All @@ -422,6 +428,7 @@ function better_speed_settings() {
$whitelist_options['better-speed'][] = 'better-speed-features-blocks';
$whitelist_options['better-speed'][] = 'better-speed-features-revisions';
$whitelist_options['better-speed'][] = 'better-speed-others-timings';
$whitelist_options['better-speed'][] = 'better-speed-others-passive';
$whitelist_options['better-speed'][] = 'better-speed-instant-page';
$whitelist_options['better-speed'][] = 'better-speed-instant-intensity';
$whitelist_options['better-speed'][] = 'better-speed-instant-external';
Expand Down Expand Up @@ -896,7 +903,16 @@ function better_speed_others_timings() {
if(better_speed_check_other_setting('others-timings')) {
$checked = " checked";
}
echo '<label><input id="better-speed-others-timings" name="better-speed-settings[better-speed-others-timings]" type="checkbox" value="YES"' . $checked . '> ' . __('Add <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing" target="_blank" rel="noopener">Server Timing</a> headers to aid debugging', 'whysoslow');
echo '<label><input id="better-speed-others-timings" name="better-speed-settings[better-speed-others-timings]" type="checkbox" value="YES"' . $checked . '> ' . __('Add <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing" target="_blank" rel="noopener">Server Timing</a> headers - this won&apos;t speed things up but can aid debugging', 'whysoslow');
}

//defined output for settings
function better_speed_others_passive() {
$checked = "";
if(better_speed_check_other_setting('others-passive')) {
$checked = " checked";
}
echo '<label><input id="better-speed-others-passive" name="better-speed-settings[better-speed-others-passive]" type="checkbox" value="YES"' . $checked . '> ' . __('Use <a href="https://web.dev/uses-passive-event-listeners/" target="_blank" rel="noopener">passive listeners</a> to improve scrolling performance', 'whysoslow');
}

//define output for settings section
Expand Down

0 comments on commit 851d857

Please sign in to comment.