Skip to content

Commit

Permalink
Leverage Script API strategy feature to defer front end scripts in WP…
Browse files Browse the repository at this point in the history
… >=6.3 (#40686)

* Leverage strategy API to defer scripts from header rather than placing in footer

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
adamsilverstein and github-actions committed Oct 19, 2023
1 parent e302d4c commit fe6e43b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/add-script-strategy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Use the Script API strategy feature to defer front-end scripts in WordPress 6.3+
4 changes: 2 additions & 2 deletions plugins/woocommerce/includes/class-wc-frontend-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static function get_asset_url( $path ) {
* @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
* @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
*/
private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = array( 'strategy' => 'defer' ) ) {
self::$scripts[] = $handle;
wp_register_script( $handle, $path, $deps, $version, $in_footer );
}
Expand All @@ -135,7 +135,7 @@ private static function register_script( $handle, $path, $deps = array( 'jquery'
* @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
* @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
*/
private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = array( 'strategy' => 'defer' ) ) {
if ( ! in_array( $handle, self::$scripts, true ) && $path ) {
self::register_script( $handle, $path, $deps, $version, $in_footer );
}
Expand Down

0 comments on commit fe6e43b

Please sign in to comment.