Make WordPress Core

Changeset 61506 for branches/6.9


Ignore:
Timestamp:
01/21/2026 04:42:31 PM (8 months ago)
Author:
jonsurrell
Message:

Script Loader: Fix script module fetchpriority calculation when dependent with higher priority is not enqueued.

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

Follow-up to [60931], [60704].

Reviewed by jonsurrell.
Merges [61401] to the 6.9 branch.

Props westonruter, jonsurrell, youknowriad.
Fixes #64429. See #61734.

Location:
branches/6.9
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/6.9

  • branches/6.9/src/wp-includes/class-wp-script-modules.php

    r61073 r61506  
    451451                );
    452452
    453                 $script_module = $this->registered[ $id ];
    454                 $dependents    = $this->get_recursive_dependents( $id );
    455                 $fetchpriority = $this->get_highest_fetchpriority( array_merge( array( $id ), $dependents ) );
     453                $script_module     = $this->registered[ $id ];
     454                $queued_dependents = array_intersect( $this->queue, $this->get_recursive_dependents( $id ) );
     455                $fetchpriority     = $this->get_highest_fetchpriority( array_merge( array( $id ), $queued_dependents ) );
    456456                if ( 'auto' !== $fetchpriority ) {
    457457                        $attributes['fetchpriority'] = $fetchpriority;
  • branches/6.9/src/wp-includes/class-wp-scripts.php

    r61260 r61506  
    10781078         * @since 6.9.0
    10791079         * @see self::filter_eligible_strategies()
    1080          * @see WP_Script_Modules::get_highest_fetchpriority_with_dependents()
     1080         * @see WP_Script_Modules::get_highest_fetchpriority()
    10811081         *
    10821082         * @param string                $handle         Script module ID.
  • branches/6.9/tests/phpunit/tests/dependencies/scripts.php

    r61260 r61506  
    14681468                        $result,
    14691469                        'Expected "high" indicates that the cached `$stored_results` entry for D was used instead of recalculating.'
     1470                );
     1471        }
     1472
     1473        /**
     1474         * Tests expected priority is used when a dependent is registered but not enqueued.
     1475         *
     1476         * @ticket 64429
     1477         *
     1478         * @covers WP_Scripts::print_scripts
     1479         * @covers WP_Scripts::get_highest_fetchpriority_with_dependents
     1480         */
     1481        public function test_priority_of_dependency_for_non_enqueued_dependent() {
     1482                $wp_scripts = wp_scripts();
     1483                wp_default_scripts( $wp_scripts );
     1484
     1485                $wp_scripts->add( 'not-enqueued', 'https://example.com/not-enqueued.js', array( 'comment-reply' ), null, array( 'priority' => 'high' ) );
     1486                $wp_scripts->enqueue( 'comment-reply' );
     1487
     1488                $actual = $this->normalize_markup_for_snapshot( get_echo( array( $wp_scripts, 'print_scripts' ) ) );
     1489                $this->assertEqualHTML(
     1490                        '<script type="text/javascript" src="/wp-includes/js/comment-reply.js" id="comment-reply-js" async="async" data-wp-strategy="async" fetchpriority="low"></script>',
     1491                        $actual,
     1492                        '<body>',
     1493                        "Snapshot:\n$actual"
    14701494                );
    14711495        }
     
    40944118                $this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
    40954119        }
     4120
     4121        /**
     4122         * Normalizes markup for snapshot.
     4123         *
     4124         * @param string $markup Markup.
     4125         * @return string Normalized markup.
     4126         */
     4127        private function normalize_markup_for_snapshot( string $markup ): string {
     4128                $processor = new WP_HTML_Tag_Processor( $markup );
     4129                $clean_url = static function ( string $url ): string {
     4130                        $url = preg_replace( '#^https?://[^/]+#', '', $url );
     4131                        return remove_query_arg( 'ver', $url );
     4132                };
     4133                while ( $processor->next_tag() ) {
     4134                        if ( 'LINK' === $processor->get_tag() && is_string( $processor->get_attribute( 'href' ) ) ) {
     4135                                $processor->set_attribute( 'href', $clean_url( $processor->get_attribute( 'href' ) ) );
     4136                        } elseif ( 'SCRIPT' === $processor->get_tag() && is_string( $processor->get_attribute( 'src' ) ) ) {
     4137                                $processor->set_attribute( 'src', $clean_url( $processor->get_attribute( 'src' ) ) );
     4138                        }
     4139                }
     4140                return $processor->get_updated_html();
     4141        }
    40964142}
  • branches/6.9/tests/phpunit/tests/script-modules/wpScriptModules.php

    r61073 r61506  
    15541554        public function data_provider_to_test_fetchpriority_bumping(): array {
    15551555                return array(
    1556                         'enqueue_bajo' => array(
     1556                        'enqueue_bajo'          => array(
    15571557                                'enqueues' => array( 'bajo' ),
    15581558                                'expected' => array(
     
    15601560                                        'script_tags'   => array(
    15611561                                                'bajo' => array(
     1562                                                        'url'           => '/bajo.js',
     1563                                                        'fetchpriority' => 'low', // Priority of 'low' not 'high' because the 'auto' dependent was not enqueued.
     1564                                                        'in_footer'     => false,
     1565                                                ),
     1566                                        ),
     1567                                        'import_map'    => array(
     1568                                                'dyno' => '/dyno.js',
     1569                                        ),
     1570                                ),
     1571                        ),
     1572                        'enqueue_bajo_and_auto' => array(
     1573                                'enqueues' => array( 'bajo', 'auto' ),
     1574                                'expected' => array(
     1575                                        'preload_links' => array(),
     1576                                        'script_tags'   => array(
     1577                                                'bajo' => array(
    15621578                                                        'url'                   => '/bajo.js',
    1563                                                         'fetchpriority'         => 'high',
     1579                                                        'fetchpriority'         => 'auto',
    15641580                                                        'in_footer'             => false,
    15651581                                                        'data-wp-fetchpriority' => 'low',
    15661582                                                ),
     1583                                                'auto' => array(
     1584                                                        'url'           => '/auto.js',
     1585                                                        'fetchpriority' => 'auto',
     1586                                                        'in_footer'     => false,
     1587                                                ),
    15671588                                        ),
    15681589                                        'import_map'    => array(
    15691590                                                'dyno' => '/dyno.js',
     1591                                                'bajo' => '/bajo.js',
    15701592                                        ),
    15711593                                ),
    15721594                        ),
    1573                         'enqueue_auto' => array(
     1595                        'enqueue_auto'          => array(
    15741596                                'enqueues' => array( 'auto' ),
    15751597                                'expected' => array(
     
    15831605                                        'script_tags'   => array(
    15841606                                                'auto' => array(
    1585                                                         'url'                   => '/auto.js',
    1586                                                         'fetchpriority'         => 'high',
    1587                                                         'in_footer'             => false,
    1588                                                         'data-wp-fetchpriority' => 'auto',
     1607                                                        'url'           => '/auto.js',
     1608                                                        'fetchpriority' => 'auto', // Priority of 'auto' not 'high' because the 'alto' dependent was not enqueued.
     1609                                                        'in_footer'     => false,
    15891610                                                ),
    15901611                                        ),
     
    15951616                                ),
    15961617                        ),
    1597                         'enqueue_alto' => array(
     1618                        'enqueue_alto'          => array(
    15981619                                'enqueues' => array( 'alto' ),
    15991620                                'expected' => array(
     
    18271848                        '<body>',
    18281849                        "Snapshot:\n$actual_footer_script_modules"
     1850                );
     1851        }
     1852
     1853        /**
     1854         * Tests expected priority is used when a dependent is registered but not enqueued.
     1855         *
     1856         * @ticket 64429
     1857         *
     1858         * @covers ::wp_default_script_modules
     1859         * @covers WP_Script_Modules::print_enqueued_script_modules
     1860         * @covers WP_Script_Modules::get_highest_fetchpriority
     1861         */
     1862        public function test_priority_of_dependency_for_non_enqueued_dependent() {
     1863                wp_default_script_modules();
     1864                wp_register_script_module( 'not-enqueued', 'https://example.com/not-enqueued.js', array( '@wordpress/a11y' ), null, array( 'priority' => 'high' ) );
     1865                wp_enqueue_script_module( '@wordpress/a11y' );
     1866
     1867                $actual = $this->normalize_markup_for_snapshot( get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) ) );
     1868                $this->assertEqualHTML(
     1869                        '<script type="module" src="/wp-includes/js/dist/script-modules/a11y/index.min.js" id="@wordpress/a11y-js-module" fetchpriority="low"></script>',
     1870                        $actual,
     1871                        '<body>',
     1872                        "Snapshot:\n$actual"
    18291873                );
    18301874        }
Note: See TracChangeset for help on using the changeset viewer.