Make WordPress Core


Ignore:
Timestamp:
09/12/2023 12:04:15 AM (3 years 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/twentyfourteen/functions.php

    r56526 r56556  
    368368
    369369        if ( is_front_page() && 'slider' === get_theme_mod( 'featured_content_layout' ) ) {
    370                 wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20150120', array( 'in_footer' => true ) );
     370                wp_enqueue_script(
     371                        'twentyfourteen-slider',
     372                        get_template_directory_uri() . '/js/slider.js',
     373                        array( 'jquery' ),
     374                        '20150120',
     375                        array(
     376                                'in_footer' => false, // Because involves header.
     377                                'strategy'  => 'defer',
     378                        )
     379                );
    371380                wp_localize_script(
    372381                        'twentyfourteen-slider',
    … …  
    379388        }
    380389
    381         wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20230526', array( 'in_footer' => true ) );
     390        wp_enqueue_script(
     391                'twentyfourteen-script',
     392                get_template_directory_uri() . '/js/functions.js',
     393                array( 'jquery' ),
     394                '20230526',
     395                array(
     396                        'in_footer' => false, // Because involves header.
     397                        'strategy'  => 'defer',
     398                )
     399        );
    382400}
    383401add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
Note: See TracChangeset for help on using the changeset viewer.