-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script Modules API: Move the script modules to the footer in classic themes #5931
Script Modules API: Move the script modules to the footer in classic themes #5931
Conversation
…ints functionality
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Includes this PR WordPress/wordpress-develop#5931
FWIW, as I removed the ability to call the print functions more than once because it's now unnecessary, I also removed the |
Committed in https://core.trac.wordpress.org/changeset/57345 |
Thanks, folks! |
…ss-6.5 folder (#57778) * Move modules api to 6.5 folder * Update functions to use wp_modules instead of gutenberg_modules * Prevent class redeclaration * Add classic themes conditional * Yet another script position movement * Add gutenberg_*_module deprecations * Add correct version deprecations * Add correct version deprecations * Use correct order for module hooks * Rename module functions to script_module * Rename module functions to script_module * Update files from WP Core Includes this PR WordPress/wordpress-develop#5931 * Fix deprecated version and rename experimental file * Add warning to upcoming deleted file * Refactor file to use only modules * Refactor search block to use only modules * Refactor query block to use only modules * Refactor image block to use only modules * Refactor navigation block to use only modules * Fix some params and add missing DocBlock * Update DEWP readme * Remove tests This code is now tested in WordPress Core. * Update enqueue in new e2e tests * Fix wrong print function names * Remove unnecessary extra argument * Add missing function * Fix redeclared class problem Fixes an issue in PHP 7.0 where we'd see a redeclared class fatal error. * Remove we from docs --------- Co-authored-by: Luis Herranz <[email protected]> Co-authored-by: Jon Surrell <[email protected]>
Trac ticket: https://core.trac.wordpress.org/ticket/60240
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:
Testing instructions
To check that this fixes the order of the import map:
"bar"
was printed in the console.To check that this fixes the positioning of the scripts/link in the classic themes:
type="importmap"
andtype="module"
, and the link withrel="modulepreload"
are printed in the head.type="importmap"
andtype="module"
, and the link withrel="modulepreload"
are printed in the footer.