<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56933] trunk: Script Loader: Move delayed head script to footer when there is a blocking footer dependent.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56933">56933</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56933","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-13 18:44:12 +0000 (Fri, 13 Oct 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Script Loader: Move delayed head script to footer when there is a blocking footer dependent.
This prevents a performance regression when a blocking script is enqueued in the footer which depends on a delayed script in the `head` (with `async` or `defer`). In order to preserve the execution order, a delayed dependency must fall back to blocking when there is a blocking dependent. But since it was originally delayed (and thus executes similarly to a footer script), it does not need to be in the head and can be moved to the footer. This prevents blocking the critical rendering path.
Props adamsilverstein, westonruter, flixos90.
Fixes <a href="https://core.trac.wordpress.org/ticket/59599">#59599</a>.
See <a href="https://core.trac.wordpress.org/ticket/12009">#12009</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpscriptsphp">trunk/src/wp-includes/class-wp-scripts.php</a></li>
<li><a href="#trunktestsphpunittestsdependenciesscriptsphp">trunk/tests/phpunit/tests/dependencies/scripts.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-scripts.php 2023-10-13 17:19:31 UTC (rev 56932)
+++ trunk/src/wp-includes/class-wp-scripts.php 2023-10-13 18:44:12 UTC (rev 56933)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -232,6 +232,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks whether all dependents of a given handle are in the footer.
+ *
+ * If there are no dependents, this is considered the same as if all dependents were in the footer.
+ *
+ * @since 6.4.0
+ *
+ * @param string $handle Script handle.
+ * @return bool Whether all dependents are in the footer.
+ */
+ private function are_all_dependents_in_footer( $handle ) {
+ foreach ( $this->get_dependents( $handle ) as $dep ) {
+ if ( isset( $this->groups[ $dep ] ) && 0 === $this->groups[ $dep ] ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Processes a script dependency.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -281,6 +300,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $intended_strategy = '';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * Move this script to the footer if:
+ * 1. The script is in the header group.
+ * 2. The current output is the header.
+ * 3. The intended strategy is delayed.
+ * 4. The actual strategy is not delayed.
+ * 5. All dependent scripts are in the footer.
+ */
+ if (
+ 0 === $group &&
+ 0 === $this->groups[ $handle ] &&
+ $intended_strategy &&
+ ! $this->is_delayed_strategy( $strategy ) &&
+ $this->are_all_dependents_in_footer( $handle )
+ ) {
+ $this->in_footer[] = $handle;
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $conditional ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $cond_before = "<!--[if {$conditional}]>\n";
</span><span class="cx" style="display: block; padding: 0 10px"> $cond_after = "<![endif]-->\n";
</span></span></pre></div>
<a id="trunktestsphpunittestsdependenciesscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/dependencies/scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/dependencies/scripts.php 2023-10-13 17:19:31 UTC (rev 56932)
+++ trunk/tests/phpunit/tests/dependencies/scripts.php 2023-10-13 18:44:12 UTC (rev 56933)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3065,4 +3065,291 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected function add_html5_script_theme_support() {
</span><span class="cx" style="display: block; padding: 0 10px"> add_theme_support( 'html5', array( 'script' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Test that a script is moved to the footer if it is made non-deferrable, was in the header and
+ * all scripts that depend on it are in the footer.
+ *
+ * @ticket 58599
+ *
+ * @dataProvider data_provider_script_move_to_footer
+ *
+ * @param callable $set_up Test setup.
+ * @param string $expected_header Expected output for header.
+ * @param string $expected_footer Expected output for footer.
+ * @param string[] $expected_in_footer Handles expected to be in the footer.
+ * @param array $expected_groups Expected groups.
+ */
+ public function test_wp_scripts_move_to_footer( $set_up, $expected_header, $expected_footer, $expected_in_footer, $expected_groups ) {
+ $set_up();
+
+ // Get the header output.
+ ob_start();
+ wp_scripts()->do_head_items();
+ $header = ob_get_clean();
+
+ // Print a script in the body just to make sure it doesn't cause problems.
+ ob_start();
+ wp_print_scripts( array( 'jquery' ) );
+ ob_end_clean();
+
+ // Get the footer output.
+ ob_start();
+ wp_scripts()->do_footer_items();
+ $footer = ob_get_clean();
+
+ $this->assertEqualMarkup( $expected_header, $header, 'Expected header script markup to match.' );
+ $this->assertEqualMarkup( $expected_footer, $footer, 'Expected footer script markup to match.' );
+ $this->assertEqualSets( $expected_in_footer, wp_scripts()->in_footer, 'Expected to have the same handles for in_footer.' );
+ $this->assertEquals( $expected_groups, wp_scripts()->groups, 'Expected groups to match.' );
+ }
+
+ /**
+ * Data provider for test_wp_scripts_move_to_footer.
+ *
+ * @return array[]
+ */
+ public function data_provider_script_move_to_footer() {
+ return array(
+ 'footer-blocking-dependent-of-defer-head-script' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'defer' ) );
+ wp_enqueue_script( 'script-b', 'https://example.com/script-b.js', array( 'script-a' ), null, array( 'in_footer' => true ) );
+ },
+ 'expected_header' => '',
+ 'expected_footer' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js"></script>
+ ',
+ 'expected_in_footer' => array(
+ 'script-a',
+ 'script-b',
+ ),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 1,
+ 'jquery' => 0,
+ ),
+ ),
+
+ 'footer-blocking-dependent-of-async-head-script' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'async' ) );
+ wp_enqueue_script( 'script-b', 'https://example.com/script-b.js', array( 'script-a' ), null, array( 'in_footer' => true ) );
+ },
+ 'expected_header' => '',
+ 'expected_footer' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" data-wp-strategy="async"></script>
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js"></script>
+ ',
+ 'expected_in_footer' => array(
+ 'script-a',
+ 'script-b',
+ ),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 1,
+ 'jquery' => 0,
+ ),
+ ),
+
+ 'head-blocking-dependent-of-delayed-head-script' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'defer' ) );
+ wp_enqueue_script( 'script-b', 'https://example.com/script-b.js', array( 'script-a' ), null, array( 'in_footer' => false ) );
+ },
+ 'expected_header' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js"></script>
+ ',
+ 'expected_footer' => '',
+ 'expected_in_footer' => array(),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 0,
+ 'jquery' => 0,
+ ),
+ ),
+
+ 'delayed-footer-dependent-of-delayed-head-script' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'defer' ) );
+ wp_enqueue_script(
+ 'script-b',
+ 'https://example.com/script-b.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => true,
+ )
+ );
+ },
+ 'expected_header' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_footer' => '
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_in_footer' => array(
+ 'script-b',
+ ),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 1,
+ 'jquery' => 0,
+ ),
+ ),
+
+ 'delayed-dependent-in-header-and-delayed-dependents-in-footer' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'defer' ) );
+ wp_enqueue_script(
+ 'script-b',
+ 'https://example.com/script-b.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => false,
+ )
+ );
+ wp_enqueue_script(
+ 'script-c',
+ 'https://example.com/script-c.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => true,
+ )
+ );
+ wp_enqueue_script(
+ 'script-d',
+ 'https://example.com/script-d.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => true,
+ )
+ );
+ },
+ 'expected_header' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" defer="defer" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_footer' => '
+ <script type="text/javascript" src="https://example.com/script-c.js" id="script-c-js" defer="defer" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-d.js" id="script-d-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_in_footer' => array(
+ 'script-c',
+ 'script-d',
+ ),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 0,
+ 'script-c' => 1,
+ 'script-d' => 1,
+ 'jquery' => 0,
+ ),
+ ),
+
+ 'all-dependents-in-footer-with-one-blocking' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'defer' ) );
+ wp_enqueue_script(
+ 'script-b',
+ 'https://example.com/script-b.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => true,
+ )
+ );
+ wp_enqueue_script( 'script-c', 'https://example.com/script-c.js', array( 'script-a' ), null, true );
+ wp_enqueue_script(
+ 'script-d',
+ 'https://example.com/script-d.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => true,
+ )
+ );
+ },
+ 'expected_header' => '',
+ 'expected_footer' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-c.js" id="script-c-js"></script>
+ <script type="text/javascript" src="https://example.com/script-d.js" id="script-d-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_in_footer' => array(
+ 'script-a',
+ 'script-b',
+ 'script-c',
+ 'script-d',
+ ),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 1,
+ 'script-c' => 1,
+ 'script-d' => 1,
+ 'jquery' => 0,
+
+ ),
+ ),
+
+ 'blocking-dependents-in-head-and-footer' => array(
+ 'set_up' => static function () {
+ wp_enqueue_script( 'script-a', 'https://example.com/script-a.js', array(), null, array( 'strategy' => 'defer' ) );
+ wp_enqueue_script(
+ 'script-b',
+ 'https://example.com/script-b.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => false,
+ )
+ );
+ wp_enqueue_script( 'script-c', 'https://example.com/script-c.js', array( 'script-a' ), null, true );
+ wp_enqueue_script(
+ 'script-d',
+ 'https://example.com/script-d.js',
+ array( 'script-a' ),
+ null,
+ array(
+ 'strategy' => 'defer',
+ 'in_footer' => true,
+ )
+ );
+ },
+ 'expected_header' => '
+ <script type="text/javascript" src="https://example.com/script-a.js" id="script-a-js" data-wp-strategy="defer"></script>
+ <script type="text/javascript" src="https://example.com/script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_footer' => '
+ <script type="text/javascript" src="https://example.com/script-c.js" id="script-c-js"></script>
+ <script type="text/javascript" src="https://example.com/script-d.js" id="script-d-js" defer="defer" data-wp-strategy="defer"></script>
+ ',
+ 'expected_in_footer' => array(
+ 'script-c',
+ 'script-d',
+ ),
+ 'expected_groups' => array(
+ 'script-a' => 0,
+ 'script-b' => 0,
+ 'script-c' => 1,
+ 'script-d' => 1,
+ 'jquery' => 0,
+ ),
+ ),
+
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>