[wp-trac] [WordPress Trac] #61657: Script "in_footer" => false with "defer" will make all dependencies that are not deferred load in head

WordPress Trac noreply at wordpress.org
Tue Jul 16 02:03:52 UTC 2024


#61657: Script "in_footer" => false with "defer" will make all dependencies that
are not deferred load in head
---------------------------+-----------------------------
 Reporter:  kkmuffme       |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Script Loader  |    Version:
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 {{{
 wp_register_script( 'foo', get_template_directory_uri() . '/foo.js',
 array(), '1.0.0', array( 'in_footer' => true ) );

 wp_register_script( 'bar', get_template_directory_uri() . '/bar.js',
 array( 'foo' ), '1.0.0', array( 'in_footer' => false, 'strategy' =>
 'defer' ) );
 }}}

 This will output both scripts in the head.
 However, this isn't necessary and leads to worse performance.
 Because "bar" which isn't loaded in footer, is loaded using the "defer"
 attribute, the browser would execute it after "foo", even though it's in
 the HTML before it.

 The only exception is browsers that do not support the "defer" attribute
 (which currently there are none to consider https://caniuse.com/script-
 defer), where this wouldn't work.

 A practical use case is wp-admin: these pages are usually not page cached,
 and it takes considerable amount of time from the time the header is
 flushed to the browser to the time PHP generated the page up to the footer
 and flushed it to the browser.
 This time can be used by the browser to download these JS files in the
 head; the "defer" attribute ensures it will only execute once the whole
 page has loaded.

 Secondly, I suggest that if a script that has footer false with defer and
 an "after" inline script is added to it, it will not just drop the "defer"
 but instead convert the footer false to footer true internally (since this
 was the original intention of the execution point when it was registered)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61657>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list