Make WordPress Core


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

    r56548 r56556  
    259259
    260260    if ( has_nav_menu( 'menu-1' ) ) {
    261         wp_enqueue_script( 'twentynineteen-priority-menu', get_theme_file_uri( '/js/priority-menu.js' ), array(), '20200129', array( 'in_footer' => true ) );
    262         wp_enqueue_script( 'twentynineteen-touch-navigation', get_theme_file_uri( '/js/touch-keyboard-navigation.js' ), array(), '20230621', array( 'in_footer' => true ) );
     261        wp_enqueue_script(
     262            'twentynineteen-priority-menu',
     263            get_theme_file_uri( '/js/priority-menu.js' ),
     264            array(),
     265            '20200129',
     266            array(
     267                'in_footer' => false, // Because involves header.
     268                'strategy'  => 'defer',
     269            )
     270        );
     271        wp_enqueue_script(
     272            'twentynineteen-touch-navigation',
     273            get_theme_file_uri( '/js/touch-keyboard-navigation.js' ),
     274            array(),
     275            '20230621',
     276            array(
     277                'in_footer' => true,
     278                'strategy'  => 'defer',
     279            )
     280        );
    263281    }
    264282
Note: See TracChangeset for help on using the changeset viewer.