Make WordPress Core

Opened 4 months ago

#61657 new defect (bug)

Script "in_footer" => false with "defer" will make all dependencies that are not deferred load in head

Reported by: kkmuffme's profile kkmuffme Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Script Loader Keywords:
Focuses: Cc:

Description

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)

Change History (0)

Note: See TracTickets for help on using tickets.