Skip to content

Commit 9ba920a

Browse files
committed
Use get_sorted_dependencies() when printing preloads and only for static imports
1 parent aeb22f0 commit 9ba920a

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ private function print_script_module( string $id ) {
472472
* @since 6.5.0
473473
*/
474474
public function print_script_module_preloads() {
475-
foreach ( $this->get_dependencies( array_unique( $this->queue ), array( 'static' ) ) as $id ) {
475+
$dependency_ids = $this->get_sorted_dependencies( array_unique( $this->queue ), array( 'static' ) );
476+
foreach ( $dependency_ids as $id ) {
476477
// Don't preload if it's marked for enqueue.
477478
if ( ! in_array( $id, $this->queue, true ) ) {
478479
$src = $this->get_src( $id );
@@ -661,14 +662,16 @@ private function get_recursive_dependents( string $id ): array {
661662
*
662663
* @since 6.9.0
663664
*
664-
* @param non-empty-string[] $ids The identifiers of the script modules to sort.
665+
* @param non-empty-string[] $ids The identifiers of the script modules to sort.
666+
* @param non-empty-string[] $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
667+
* Default is both.
665668
* @return non-empty-string[] Sorted list of script module identifiers.
666669
*/
667-
private function get_sorted_dependencies( array $ids ): array {
670+
private function get_sorted_dependencies( array $ids, array $import_types = array( 'static', 'dynamic' ) ): array {
668671
$sorted = array();
669672

670673
foreach ( $ids as $id ) {
671-
$this->sort_item_dependencies( $id, $sorted );
674+
$this->sort_item_dependencies( $id, $import_types, $sorted );
672675
}
673676

674677
return array_unique( $sorted );
@@ -679,11 +682,12 @@ private function get_sorted_dependencies( array $ids ): array {
679682
*
680683
* @since 6.9.0
681684
*
682-
* @param non-empty-string $id The identifier of the script module to sort.
683-
* @param non-empty-string[] &$sorted The array of sorted identifiers, passed by reference.
685+
* @param non-empty-string $id The identifier of the script module to sort.
686+
* @param non-empty-string[] $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
687+
* @param non-empty-string[] &$sorted The array of sorted identifiers, passed by reference.
684688
* @return bool True on success, false on failure (e.g., missing dependency).
685689
*/
686-
private function sort_item_dependencies( string $id, array &$sorted ): bool {
690+
private function sort_item_dependencies( string $id, array $import_types, array &$sorted ): bool {
687691
// If already processed, don't do it again.
688692
if ( in_array( $id, $this->done, true ) || in_array( $id, $sorted, true ) ) {
689693
return true;
@@ -694,7 +698,12 @@ private function sort_item_dependencies( string $id, array &$sorted ): bool {
694698
return false;
695699
}
696700

697-
$dependency_ids = array_column( $this->registered[ $id ]['dependencies'], 'id' );
701+
$dependency_ids = array();
702+
foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) {
703+
if ( in_array( $dependency['import'], $import_types, true ) ) {
704+
$dependency_ids[] = $dependency['id'];
705+
}
706+
}
698707

699708
// If the item requires dependencies that do not exist, fail.
700709
if ( count( array_diff( $dependency_ids, array_keys( $this->registered ) ) ) > 0 ) {
@@ -703,7 +712,7 @@ private function sort_item_dependencies( string $id, array &$sorted ): bool {
703712

704713
// Recursively process dependencies.
705714
foreach ( $dependency_ids as $dependency_id ) {
706-
if ( ! $this->sort_item_dependencies( $dependency_id, $sorted ) ) {
715+
if ( ! $this->sort_item_dependencies( $dependency_id, $import_types, $sorted ) ) {
707716
// A dependency failed to resolve, so this branch fails.
708717
return false;
709718
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,15 +1448,15 @@ public function data_provider_to_test_fetchpriority_bumping(): array {
14481448
'enqueues' => array( 'alto' ),
14491449
'expected' => array(
14501450
'preload_links' => array(
1451-
'auto' => array(
1452-
'url' => '/auto.js',
1453-
'fetchpriority' => 'high',
1454-
),
14551451
'bajo' => array(
14561452
'url' => '/bajo.js',
14571453
'fetchpriority' => 'high',
14581454
'data-wp-fetchpriority' => 'low',
14591455
),
1456+
'auto' => array(
1457+
'url' => '/auto.js',
1458+
'fetchpriority' => 'high',
1459+
),
14601460
),
14611461
'script_tags' => array(
14621462
'alto' => array(
@@ -1576,12 +1576,12 @@ public function test_fetchpriority_bumping_a_to_z() {
15761576
$actual = get_echo( array( wp_script_modules(), 'print_script_module_preloads' ) );
15771577
$actual .= get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
15781578
$expected = '
1579-
<link rel="modulepreload" href="/b.js" id="b-js-modulepreload" fetchpriority="low">
1580-
<link rel="modulepreload" href="/d.js" id="d-js-modulepreload" fetchpriority="high">
1581-
<link rel="modulepreload" href="/y.js" id="y-js-modulepreload" fetchpriority="high">
1582-
<link rel="modulepreload" href="/c.js" id="c-js-modulepreload" fetchpriority="low">
15831579
<link rel="modulepreload" href="/z.js" id="z-js-modulepreload" fetchpriority="high">
1580+
<link rel="modulepreload" href="/d.js" id="d-js-modulepreload" fetchpriority="high">
15841581
<link rel="modulepreload" href="/e.js" id="e-js-modulepreload" fetchpriority="low">
1582+
<link rel="modulepreload" href="/c.js" id="c-js-modulepreload" fetchpriority="low">
1583+
<link rel="modulepreload" href="/b.js" id="b-js-modulepreload" fetchpriority="low">
1584+
<link rel="modulepreload" href="/y.js" id="y-js-modulepreload" fetchpriority="high">
15851585
<script type="module" src="/a.js" id="a-js-module" fetchpriority="low"></script>
15861586
<script type="module" src="/x.js" id="x-js-module" fetchpriority="high"></script>
15871587
';
@@ -1621,12 +1621,12 @@ public function test_fetchpriority_propagation() {
16211621
$actual = get_echo( array( wp_script_modules(), 'print_script_module_preloads' ) );
16221622
$actual .= get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
16231623
$expected = '
1624-
<link rel="modulepreload" href="/a.js" id="a-js-modulepreload" fetchpriority="low" data-wp-fetchpriority="high">
1625-
<link rel="modulepreload" href="/b.js" id="b-js-modulepreload">
1626-
<link rel="modulepreload" href="/c.js" id="c-js-modulepreload" fetchpriority="high">
16271624
<link rel="modulepreload" href="/d.js" id="d-js-modulepreload" fetchpriority="low">
16281625
<link rel="modulepreload" href="/e.js" id="e-js-modulepreload" fetchpriority="high" data-wp-fetchpriority="low">
1626+
<link rel="modulepreload" href="/a.js" id="a-js-modulepreload" fetchpriority="low" data-wp-fetchpriority="high">
1627+
<link rel="modulepreload" href="/b.js" id="b-js-modulepreload">
16291628
<link rel="modulepreload" href="/f.js" id="f-js-modulepreload" fetchpriority="high">
1629+
<link rel="modulepreload" href="/c.js" id="c-js-modulepreload" fetchpriority="high">
16301630
<script type="module" src="/x.js" id="x-js-module" fetchpriority="low"></script>
16311631
<script type="module" src="/y.js" id="y-js-module"></script>
16321632
<script type="module" src="/z.js" id="z-js-module" fetchpriority="high"></script>
@@ -1683,8 +1683,8 @@ public function test_dependent_of_default_script_modules() {
16831683

16841684
$expected = '
16851685
<link rel="modulepreload" href="/wp-includes/js/dist/script-modules/a11y/index.min.js" id="@wordpress/a11y-js-modulepreload" fetchpriority="high" data-wp-fetchpriority="low">
1686-
<link rel="modulepreload" href="/wp-includes/js/dist/script-modules/block-library/navigation/view.min.js" id="@wordpress/block-library/navigation/view-js-modulepreload" fetchpriority="high" data-wp-fetchpriority="low">
16871686
<link rel="modulepreload" href="/wp-includes/js/dist/script-modules/interactivity/debug.min.js" id="@wordpress/interactivity-js-modulepreload" fetchpriority="high" data-wp-fetchpriority="low">
1687+
<link rel="modulepreload" href="/wp-includes/js/dist/script-modules/block-library/navigation/view.min.js" id="@wordpress/block-library/navigation/view-js-modulepreload" fetchpriority="high" data-wp-fetchpriority="low">
16881688
<script type="module" src="/super-important-module.js" id="super-important-js-module" fetchpriority="high"></script>
16891689
';
16901690
$this->assertEqualHTML( $expected, $actual, '<body>', "Snapshot:\n$actual" );

0 commit comments

Comments
 (0)