Make WordPress Core


Ignore:
Timestamp:
10/14/2025 05:45:17 AM (3 months ago)
Author:
westonruter
Message:

Script Loader: Propagate fetchpriority from dependents to dependencies.

This introduces a "fetchpriority bumping" mechanism for both classic scripts (WP_Scripts) and script modules (WP_Script_Modules). When a script with a higher fetchpriority is enqueued, any of its dependencies will have their fetchpriority elevated to match that of the highest-priority dependent. This ensures that all assets in a critical dependency chain are loaded with the appropriate priority, preventing a high-priority script from being blocked by a low-priority dependency. This is similar to logic used in script loading strategies to ensure that a blocking dependent causes delayed (async/defer) dependencies to also become blocking. See #12009.

When a script's fetchpriority is escalated, its original, registered priority is added to the tag via a data-wp-fetchpriority attribute. This matches the addition of the data-wp-strategy parameter added when the resulting loading strategy does not match the original.

Developed in https://github.com/WordPress/wordpress-develop/pull/9770.

Follow-up to [60704].

Props westonruter, jonsurrell.
Fixes #61734.

File:
1 edited

Legend:

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

    r60704 r60931  
    182182        }
    183183
    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.
     184        /*
     185         * The Interactivity API is designed with server-side rendering as its primary goal, so all of its script modules
     186         * should be loaded with low fetchpriority since they should not be needed in the critical rendering path.
     187         * Also, the @wordpress/a11y script module is intended to be used as a dynamic import dependency, in which case
     188         * the fetchpriority is irrelevant. See <https://make.wordpress.org/core/2024/10/14/updates-to-script-modules-in-6-7/>.
     189         * However, in case it is added as a static import dependency, the fetchpriority is explicitly set to be 'low'
     190         * since the module should not be involved in the critical rendering path, and if it is, its fetchpriority will
     191         * be bumped to match the fetchpriority of the dependent script.
     192         */
    185193        $args = array();
    186         if ( str_starts_with( $script_module_id, '@wordpress/interactivity' ) || str_starts_with( $script_module_id, '@wordpress/block-library' ) ) {
     194        if (
     195            str_starts_with( $script_module_id, '@wordpress/interactivity' ) ||
     196            str_starts_with( $script_module_id, '@wordpress/block-library' ) ||
     197            '@wordpress/a11y' === $script_module_id
     198        ) {
    187199            $args['fetchpriority'] = 'low';
    188200        }
Note: See TracChangeset for help on using the changeset viewer.