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/functions.wp-scripts.php

    r58200 r60704  
    159159 * @since 4.3.0 A return value was added.
    160160 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.
     161 * @since 6.9.0 The $fetchpriority parameter of type string was added to the $args parameter of type array.
    161162 *
    162163 * @param string           $handle    Name of the script. Should be unique.
     
    172173 *     Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.
    173174 *
    174  *     @type string    $strategy     Optional. If provided, may be either 'defer' or 'async'.
    175  *     @type bool      $in_footer    Optional. Whether to print the script in the footer. Default 'false'.
     175 *     @type string    $strategy      Optional. If provided, may be either 'defer' or 'async'.
     176 *     @type bool      $in_footer     Optional. Whether to print the script in the footer. Default 'false'.
     177 *     @type string    $fetchpriority Optional. The fetch priority for the script. Default 'auto'.
    176178 * }
    177179 * @return bool Whether the script has been registered. True on success, false on failure.
     
    193195    if ( ! empty( $args['strategy'] ) ) {
    194196        $wp_scripts->add_data( $handle, 'strategy', $args['strategy'] );
     197    }
     198    if ( ! empty( $args['fetchpriority'] ) ) {
     199        $wp_scripts->add_data( $handle, 'fetchpriority', $args['fetchpriority'] );
    195200    }
    196201    return $registered;
     
    340345 * @since 2.1.0
    341346 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.
     347 * @since 6.9.0 The $fetchpriority parameter of type string was added to the $args parameter of type array.
    342348 *
    343349 * @param string           $handle    Name of the script. Should be unique.
     
    353359 *     Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.
    354360 *
    355  *     @type string    $strategy     Optional. If provided, may be either 'defer' or 'async'.
    356  *     @type bool      $in_footer    Optional. Whether to print the script in the footer. Default 'false'.
     361 *     @type string    $strategy      Optional. If provided, may be either 'defer' or 'async'.
     362 *     @type bool      $in_footer     Optional. Whether to print the script in the footer. Default 'false'.
     363 *     @type string    $fetchpriority Optional. The fetch priority for the script. Default 'auto'.
    357364 * }
    358365 */
     
    379386            $wp_scripts->add_data( $_handle[0], 'strategy', $args['strategy'] );
    380387        }
     388        if ( ! empty( $args['fetchpriority'] ) ) {
     389            $wp_scripts->add_data( $_handle[0], 'fetchpriority', $args['fetchpriority'] );
     390        }
    381391    }
    382392
Note: See TracChangeset for help on using the changeset viewer.