Skip to content

Commit 1b2a381

Browse files
committed
Script Loader: Add support for printing script modules at wp_footer.
This brings API parity with `WP_Scripts` by implementing opt-in support for printing in the footer via an `in_footer` argument. This argument can be supplied via the `$args` array passed to `wp_enqueue_script_module()` or `wp_register_script_module()`, alongside the existing `fetchpriority` key introduced in #61734. It can also be set for previously-registered script modules via `WP_Script_Modules::set_in_footer()`. This is not applicable to classic themes since modules are enqueued while blocks are rendered after `wp_head` has completed, so all script modules are printed in the footer anyway; the `importmap` script must be printed after all script modules have been enqueued. Script modules used for interactive blocks (with the Interactivity API) are automatically printed in the footer. Such script modules should be deprioritized because they are not in the critical rendering path due to interactive blocks using server-side rendering. Script modules remain printed at `wp_head` by default, although this default should be revisited since they have deferred execution (and they are printed in the footer for classic themes already, as previously noted). Moving a script module to the footer ensures that its loading does not contend with the loading of critical resources, such as the LCP element's image resource, and LCP is improved as a result. This also improves specificity of some PHP types, it ensures that script modules can't be registered with an empty ID, and it prevents printing script modules with empty `src` URLs. Developed in WordPress/wordpress-develop#9867 Follow-up to [60704]. Props b1ink0, westonruter, jonsurrell, peterwilsoncc, vipulpatil, mindctrl. See #61734. Fixes #63486. Built from https://develop.svn.wordpress.org/trunk@60999 git-svn-id: http://core.svn.wordpress.org/trunk@60335 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent db69108 commit 1b2a381

File tree

4 files changed

+315
-97
lines changed

4 files changed

+315
-97
lines changed

wp-includes/blocks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
182182
( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] )
183183
) {
184184
$args['fetchpriority'] = 'low';
185+
$args['in_footer'] = true;
185186
}
186187

187188
wp_register_script_module(

0 commit comments

Comments
 (0)