[wp-trac] [WordPress Trac] #54018: Allow scripts registered via block.json to be enqueued in the footer

WordPress Trac noreply at wordpress.org
Tue Feb 15 08:38:41 UTC 2022

#54018: Allow scripts registered via block.json to be enqueued in the footer
 Reporter:  jeremyfelt     |       Owner:  (none)
     Type:  enhancement    |      Status:  new
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  Script Loader  |     Version:
 Severity:  normal         |  Resolution:
 Keywords:  has-patch      |     Focuses:  performance

Comment (by gziolo):

 > I've attached a PR allows which what you asked by adding support to
 enqueueScriptFooter and enqueueEditorFooter fields in $metadata. I've
 thought to add the support for the editor script as well. You can see an
 example usage in the PR.

 @donmhico, thank you for the patch proposed. I checked the code and it
 might not be the best fit once we allow multiple scripts per type:

 "editorScript": [ "file:./editor-1.js", "file:./editor-2", 'block-editor-
 script-3" ],

 In theory, you could also allow array for newly proposed field in

 "editorScriptFooter": [ true, false, null ],

 However, the issue is that you can pass both paths to the asset file, but
 you can also pass the registered script handle like `block-editor-
 script-3` where you would already have that flag configured.

 @jeremyfelt, one solution that works today is to register the script
 separately and pass the handle to `block.json` file. The idea was to both
 cover some already registered scripts, but also to offer more control on
 how those scripts might get registered.

 > OR It should be possible to filter the wp_register_script() call in
 register_block_script_handle() so that $footer = true can be passed just
 in time based on the handle of the script being registered.

 This is also an option worth considering. If we do it, we should probably
 add a similar filter for `register_block_style`.

Ticket URL: <https://core.trac.wordpress.org/ticket/54018#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list