Make WordPress Core


Ignore:
Timestamp:
09/12/2023 12:04:15 AM (9 months ago)
Author:
westonruter
Message:

Bundled Themes: Use defer loading strategy for theme scripts.

  • Add defer loading strategy for all frontend end-user theme scripts (excluding Customizer preview).
  • Move scripts to the head which relate to the initial page viewport to ensure they start loading earlier and execute sooner while still not blocking rendering.
  • Update Twenty Twenty's script loader (TwentyTwenty_Script_Loader) to support core's built-in script loading strategies (#12009), while also retaining backwards-compatibility for child themes that may set async and defer script data.
  • Update the main script loading strategy in Twenty Twenty from async to defer for better performance on repeat page views, since when an async script is cached it will block rendering.

Props westonruter, flixos90, sabernhardt.
Fixes #59316.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentytwenty/classes/class-twentytwenty-script-loader.php

    r56547 r56556  
    2121
    2222        /**
     23         * Migrates legacy async/defer script data which might be used by child themes.
     24         *
     25         * This method is used on the `print_scripts_array` filter.
     26         *
     27         * @since Twenty Twenty 2.0
     28         *
     29         * @param string[] $to_do An array of script dependency handles.
     30         * @return string[] Unchanged array of script dependency handles.
     31         */
     32        public function migrate_legacy_strategy_script_data( $to_do ) {
     33            foreach ( $to_do as $handle ) {
     34                foreach ( array( 'async', 'defer' ) as $strategy ) {
     35                    if ( wp_scripts()->get_data( $handle, $strategy ) ) {
     36                        wp_script_add_data( $handle, 'strategy', $strategy );
     37                    }
     38                }
     39            }
     40            return $to_do;
     41        }
     42
     43        /**
    2344         * Adds async/defer attributes to enqueued / registered scripts.
    2445         *
    25          * If #12009 lands in WordPress, this function can no-op since it would be handled in core.
     46         * Now that #12009 has landed in WordPress 6.3, this method is only used for older versions of WordPress.
     47         * This method is used on the `script_loader_tag` filter.
    2648         *
    2749         * @since Twenty Twenty 1.0
     
    3456         */
    3557        public function filter_script_loader_tag( $tag, $handle ) {
    36             foreach ( array( 'async', 'defer' ) as $attr ) {
    37                 if ( ! wp_scripts()->get_data( $handle, $attr ) ) {
    38                     continue;
    39                 }
     58            $strategies = array(
     59                'async' => (bool) wp_scripts()->get_data( $handle, 'async' ),
     60                'defer' => (bool) wp_scripts()->get_data( $handle, 'defer' ),
     61            );
     62            $strategy   = wp_scripts()->get_data( $handle, 'strategy' );
     63            if ( $strategy && isset( $strategies[ $strategy ] ) ) {
     64                $strategies[ $strategy ] = true;
     65            }
     66
     67            foreach ( array_keys( array_filter( $strategies ) ) as $attr ) {
     68
    4069                // Prevent adding attribute when already added in #12009.
    4170                if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) {
Note: See TracChangeset for help on using the changeset viewer.