Script Modules API: Move the script modules to the footer in classic themes #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Trac ticket: https://core.trac.wordpress.org/ticket/60240
!! This is built on top of WordPress#5869. For that reason it's still a local PR. I will rebase it and open a PR in the
WordPress/wordpress-develop
once WordPress#5869 is merged.What
Moves the script module prints to the footer in classic themes. It also removes the ability to call the print functions more than once because it's now unnecessary.
Why
In the ticket that introduced the Script Modules API (#56313), I added a logic that prints as many enqueued and preloads as possible in the head, then prints the remaining ones in the footer and includes the import map. I explain my reasoning in this comment.
But @cbravobernal realized that import maps fail if they appear after the modules. That means that there's no other way but printing the import map first, and then all the modules and preloads (yes, preloads also fail!). So we need to check if we are on a classic or block theme, and if we are in a classic theme, move everything to the footer.
How
By leveraging
wp_is_block_theme()
and moving everything to the footer in classic themes:Additional comments
@westonruter mentioned that we could optimize this in the future if we have a way to filter the output before sending the response to the client: