Make WordPress Core


Ignore:
Timestamp:
09/03/2025 10:15:31 PM (5 months ago)
Author:
westonruter
Message:

Script Loader: Introduce fetchpriority for Scripts and Script Modules.

  • Allow scripts and script modules to be registered with a fetchpriority of auto (default), high, low:
    • When registering a script, add a fetchpriority arg to go alongside the strategy arg which was added for loading scripts with the defer and async loading strategies. See #12009.
    • For script modules, introduce an $args array parameter with a fetchpriority key to the wp_register_script_module(), and wp_enqueue_script_module() functions (and their respective underlying WP_Script_Modules::register() and WP_Script_Modules::enqueue() methods). This $args parameter corresponds with the same parameter used when registering non-module scripts.
    • Also for script modules, introduce WP_Script_Modules::set_fetchpriority() to override the fetchpriority for what was previously registered.
    • Emit a _doing_it_wrong() warning when an invalid fetchpriority value is used, and when fetchpriority is added to a script alias.
    • Include fetchpriority as an attribute on printed SCRIPT tags as well as on preload LINK tags for static script module dependencies.
  • Use a fetchpriority of low by default for:
    • Script modules used with the Interactivity API. For overriding this default in blocks, see Gutenberg#71366.
    • The comment-reply script.
  • Improve type checks and type hints.

Developed in GitHub PR, with companion for Gutenberg.

Props westonruter, jonsurrell, swissspidy, luisherranz, kraftbj, audrasjb, dennysdionigi.
Fixes #61734.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-modules.php

    r59223 r60704  
    3636 *
    3737 * @since 6.5.0
     38 * @since 6.9.0 Added the $args parameter.
    3839 *
    3940 * @param string            $id      The identifier of the script module. Should be unique. It will be used in the
     
    6162 *                                   is set to false, the version number is the currently installed WordPress version.
    6263 *                                   If $version is set to null, no version is added.
     64 * @param array             $args    {
     65 *     Optional. An array of additional args. Default empty array.
     66 *
     67 *     @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
     68 * }
    6369 */
    64 function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) {
    65     wp_script_modules()->register( $id, $src, $deps, $version );
     70function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false, array $args = array() ) {
     71    wp_script_modules()->register( $id, $src, $deps, $version, $args );
    6672}
    6773
     
    7379 *
    7480 * @since 6.5.0
     81 * @since 6.9.0 Added the $args parameter.
    7582 *
    7683 * @param string            $id      The identifier of the script module. Should be unique. It will be used in the
     
    98105 *                                   is set to false, the version number is the currently installed WordPress version.
    99106 *                                   If $version is set to null, no version is added.
     107 * @param array             $args    {
     108 *     Optional. An array of additional args. Default empty array.
     109 *
     110 *     @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
     111 * }
    100112 */
    101 function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) {
    102     wp_script_modules()->enqueue( $id, $src, $deps, $version );
     113function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false, array $args = array() ) {
     114    wp_script_modules()->enqueue( $id, $src, $deps, $version, $args );
    103115}
    104116
     
    170182        }
    171183
     184        // The Interactivity API is designed with server-side rendering as its primary goal, so all of its script modules should be loaded with low fetch priority since they should not be needed in the critical rendering path.
     185        $args = array();
     186        if ( str_starts_with( $script_module_id, '@wordpress/interactivity' ) || str_starts_with( $script_module_id, '@wordpress/block-library' ) ) {
     187            $args['fetchpriority'] = 'low';
     188        }
     189
    172190        $path = includes_url( "js/dist/script-modules/{$file_name}" );
    173         wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
     191        wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'], $args );
    174192    }
    175193}
Note: See TracChangeset for help on using the changeset viewer.