Skip to content

Commit 5f7cef7

Browse files
committed
Script Loader: Fix script module fetchpriority calculation when dependent with higher priority is not enqueued.
Developed in #10651 Follow-up to [60931], [60704]. Props westonruter, jonsurrell, youknowriad. See #61734. Fixes #64429. git-svn-id: https://develop.svn.wordpress.org/trunk@61401 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fcf6a84 commit 5f7cef7

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

src/wp-includes/class-wp-script-modules.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ private function print_script_module( string $id ) {
461461
'id' => $id . '-js-module',
462462
);
463463

464-
$script_module = $this->registered[ $id ];
465-
$dependents = $this->get_recursive_dependents( $id );
466-
$fetchpriority = $this->get_highest_fetchpriority( array_merge( array( $id ), $dependents ) );
464+
$script_module = $this->registered[ $id ];
465+
$queued_dependents = array_intersect( $this->queue, $this->get_recursive_dependents( $id ) );
466+
$fetchpriority = $this->get_highest_fetchpriority( array_merge( array( $id ), $queued_dependents ) );
467467
if ( 'auto' !== $fetchpriority ) {
468468
$attributes['fetchpriority'] = $fetchpriority;
469469
}

src/wp-includes/class-wp-scripts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ private function filter_eligible_strategies( $handle, $eligible_strategies = nul
10921092
*
10931093
* @since 6.9.0
10941094
* @see self::filter_eligible_strategies()
1095-
* @see WP_Script_Modules::get_highest_fetchpriority_with_dependents()
1095+
* @see WP_Script_Modules::get_highest_fetchpriority()
10961096
*
10971097
* @param string $handle Script module ID.
10981098
* @param array<string, true> $checked Optional. An array of already checked script handles, used to avoid recursive loops.

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,30 @@ public function test_highest_fetchpriority_with_dependents_uses_cached_result()
15161516
);
15171517
}
15181518

1519+
/**
1520+
* Tests expected priority is used when a dependent is registered but not enqueued.
1521+
*
1522+
* @ticket 64429
1523+
*
1524+
* @covers WP_Scripts::print_scripts
1525+
* @covers WP_Scripts::get_highest_fetchpriority_with_dependents
1526+
*/
1527+
public function test_priority_of_dependency_for_non_enqueued_dependent() {
1528+
$wp_scripts = wp_scripts();
1529+
wp_default_scripts( $wp_scripts );
1530+
1531+
$wp_scripts->add( 'not-enqueued', 'https://example.com/not-enqueued.js', array( 'comment-reply' ), null, array( 'priority' => 'high' ) );
1532+
$wp_scripts->enqueue( 'comment-reply' );
1533+
1534+
$actual = $this->normalize_markup_for_snapshot( get_echo( array( $wp_scripts, 'print_scripts' ) ) );
1535+
$this->assertEqualHTML(
1536+
'<script type="text/javascript" src="/wp-includes/js/comment-reply.js" id="comment-reply-js" async="async" data-wp-strategy="async" fetchpriority="low"></script>',
1537+
$actual,
1538+
'<body>',
1539+
"Snapshot:\n$actual"
1540+
);
1541+
}
1542+
15191543
/**
15201544
* Tests that printing a script without enqueueing has the same output as when it is enqueued.
15211545
*
@@ -4256,4 +4280,26 @@ public function data_varying_versions_handle_args() {
42564280
),
42574281
);
42584282
}
4283+
4284+
/**
4285+
* Normalizes markup for snapshot.
4286+
*
4287+
* @param string $markup Markup.
4288+
* @return string Normalized markup.
4289+
*/
4290+
private function normalize_markup_for_snapshot( string $markup ): string {
4291+
$processor = new WP_HTML_Tag_Processor( $markup );
4292+
$clean_url = static function ( string $url ): string {
4293+
$url = preg_replace( '#^https?://[^/]+#', '', $url );
4294+
return remove_query_arg( 'ver', $url );
4295+
};
4296+
while ( $processor->next_tag() ) {
4297+
if ( 'LINK' === $processor->get_tag() && is_string( $processor->get_attribute( 'href' ) ) ) {
4298+
$processor->set_attribute( 'href', $clean_url( $processor->get_attribute( 'href' ) ) );
4299+
} elseif ( 'SCRIPT' === $processor->get_tag() && is_string( $processor->get_attribute( 'src' ) ) ) {
4300+
$processor->set_attribute( 'src', $clean_url( $processor->get_attribute( 'src' ) ) );
4301+
}
4302+
}
4303+
return $processor->get_updated_html();
4304+
}
42594305
}

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,24 +1553,46 @@ public function test_set_fetchpriority_with_invalid_value() {
15531553
*/
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' ),
1558+
'expected' => array(
1559+
'preload_links' => array(),
1560+
'script_tags' => array(
1561+
'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' ),
15581574
'expected' => array(
15591575
'preload_links' => array(),
15601576
'script_tags' => array(
15611577
'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(
15761598
'preload_links' => array(
@@ -1582,10 +1604,9 @@ public function data_provider_to_test_fetchpriority_bumping(): array {
15821604
),
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
),
15911612
'import_map' => array(
@@ -1594,7 +1615,7 @@ public function data_provider_to_test_fetchpriority_bumping(): array {
15941615
),
15951616
),
15961617
),
1597-
'enqueue_alto' => array(
1618+
'enqueue_alto' => array(
15981619
'enqueues' => array( 'alto' ),
15991620
'expected' => array(
16001621
'preload_links' => array(
@@ -1829,6 +1850,29 @@ public function test_default_script_modules() {
18291850
);
18301851
}
18311852

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"
1873+
);
1874+
}
1875+
18321876
/**
18331877
* Tests that a dependent with high priority for default script modules with a low fetch priority are printed as expected.
18341878
*

0 commit comments

Comments
 (0)