Make WordPress Core


Ignore:
Timestamp:
09/03/2025 10:15:31 PM (14 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/class-wp-scripts.php

    r60690 r60704  
    425425                if ( $intended_strategy ) {
    426426                        $attr['data-wp-strategy'] = $intended_strategy;
     427                }
     428                if ( isset( $obj->extra['fetchpriority'] ) && 'auto' !== $obj->extra['fetchpriority'] && $this->is_valid_fetchpriority( $obj->extra['fetchpriority'] ) ) {
     429                        $attr['fetchpriority'] = $obj->extra['fetchpriority'];
    427430                }
    428431                $tag  = $translations . $ie_conditional_prefix . $before_script;
    … …  
    832835                                return false;
    833836                        }
     837                } elseif ( 'fetchpriority' === $key ) {
     838                        if ( empty( $value ) ) {
     839                                $value = 'auto';
     840                        }
     841                        if ( ! $this->is_valid_fetchpriority( $value ) ) {
     842                                _doing_it_wrong(
     843                                        __METHOD__,
     844                                        sprintf(
     845                                                /* translators: 1: $fetchpriority, 2: $handle */
     846                                                __( 'Invalid fetchpriority `%1$s` defined for `%2$s` during script registration.' ),
     847                                                is_string( $value ) ? $value : gettype( $value ),
     848                                                $handle
     849                                        ),
     850                                        '6.9.0'
     851                                );
     852                                return false;
     853                        } elseif ( ! $this->registered[ $handle ]->src ) {
     854                                _doing_it_wrong(
     855                                        __METHOD__,
     856                                        sprintf(
     857                                                /* translators: 1: $fetchpriority, 2: $handle */
     858                                                __( 'Cannot supply a fetchpriority `%1$s` for script `%2$s` because it is an alias (it lacks a `src` value).' ),
     859                                                is_string( $value ) ? $value : gettype( $value ),
     860                                                $handle
     861                                        ),
     862                                        '6.9.0'
     863                                );
     864                                return false;
     865                        }
    834866                }
    835867                return parent::add_data( $handle, $key, $value );
    … …  
    870902         * @since 6.3.0
    871903         *
    872          * @param string $strategy The strategy to check.
     904         * @param string|mixed $strategy The strategy to check.
    873905         * @return bool True if $strategy is one of the delayed strategies, otherwise false.
    874906         */
    875         private function is_delayed_strategy( $strategy ) {
     907        private function is_delayed_strategy( $strategy ): bool {
    876908                return in_array(
    877909                        $strategy,
    … …  
    879911                        true
    880912                );
     913        }
     914
     915        /**
     916         * Checks if the provided fetchpriority is valid.
     917         *
     918         * @since 6.9.0
     919         *
     920         * @param string|mixed $priority Fetch priority.
     921         * @return bool Whether valid fetchpriority.
     922         */
     923        private function is_valid_fetchpriority( $priority ): bool {
     924                return in_array( $priority, array( 'auto', 'low', 'high' ), true );
    881925        }
    882926
Note: See TracChangeset for help on using the changeset viewer.