-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add / 48 Abort Script Strategy Logic When Concat Scripts Present #51
Add / 48 Abort Script Strategy Logic When Concat Scripts Present #51
Conversation
…fer or async loading strategy.
src/wp-includes/class-wp-scripts.php
Outdated
// Get the most eligible loading strategy for said script handle. | ||
// Used as a conditional to prevent script concatenation. | ||
$strategy = $this->get_eligible_loading_strategy( $handle ); | ||
$is_deferred_or_async_handle = '' !== $strategy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe check explicitly for 'async' or defer' here since other strategies could be added in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamsilverstein this has been resolved in a recent commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks right but it would be good to confirm this expected behavior with a unit test or two. Is that something you can add?
…abort-script-strategy-logic-when-concat-scripts-present
@joemcgill this was addressed in a recent commit by @kt-12 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good. A couple of suggestions, but it's nice to see this working as expected. Nice work!
wp_enqueue_script( 'main-defer-script-1', '/main-script.js', array(), null, array( 'strategy' => 'async' ) ); | ||
|
||
wp_print_scripts(); | ||
$print_scripts = get_echo( '_print_scripts' ); | ||
|
||
// reset global before asserting. | ||
$concatenate_scripts = $old_value; | ||
|
||
$ver = get_bloginfo( 'version' ); | ||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&ver={$ver}'></script>\n"; | ||
$expected .= "<script type='text/javascript' src='/main-script.js' id='main-defer-script-1-js' async></script>\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the handle name so it matches the loading strategy.
wp_enqueue_script( 'main-defer-script-1', '/main-script.js', array(), null, array( 'strategy' => 'async' ) ); | |
wp_print_scripts(); | |
$print_scripts = get_echo( '_print_scripts' ); | |
// reset global before asserting. | |
$concatenate_scripts = $old_value; | |
$ver = get_bloginfo( 'version' ); | |
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&ver={$ver}'></script>\n"; | |
$expected .= "<script type='text/javascript' src='/main-script.js' id='main-defer-script-1-js' async></script>\n"; | |
wp_enqueue_script( 'main-async-script-1', '/main-script.js', array(), null, array( 'strategy' => 'async' ) ); | |
wp_print_scripts(); | |
$print_scripts = get_echo( '_print_scripts' ); | |
// reset global before asserting. | |
$concatenate_scripts = $old_value; | |
$ver = get_bloginfo( 'version' ); | |
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&ver={$ver}'></script>\n"; | |
$expected .= "<script type='text/javascript' src='/main-script.js' id='main-async-script-1-js' async></script>\n"; |
$wp_scripts->do_concat = true; | ||
$wp_scripts->default_dirs = array( '/directory/' ); | ||
|
||
wp_enqueue_script( 'one-concat-dep-2', '/directory/script.js' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me curious about what we would expect to happen if there's a mix of concatenated scripts that depend on a deferred script or if only some of these concatenated scripts were dependencies of the deferred script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any script depends on a deferred script, the deferred script itself will be loaded in a blocking way. In this case, it will get concatenated.get_strategy
takes care of that.
public function test_concatenate_with_async_strategy() { | ||
global $wp_scripts, $concatenate_scripts; | ||
|
||
$old_value = $concatenate_scripts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are modifying the global $wp_scripts
variable directly, I wonder if we even need to modify the global $concatenate_scripts
variable? We may also need to reset the global $wp_scripts
variable before running or assertions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joemcgill This is the case with almost every test case. Infact, most of the test case contains handles from previous test cases. I followed this rule ->
as long as this doesn't affect other test cases, not resetting global should be fine.
This function was created by replicating an old concatenation test function, setting of the directory was also taken from it.
cc: @10upsimon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the context Karthik. I’ll make a note to review our use of that global in all the tests. Not something we need to address here.
$concatenate_scripts = true; | ||
|
||
$wp_scripts->do_concat = true; | ||
$wp_scripts->default_dirs = array( '/directory/' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: we could make the default dir path a variable so it can easily be updated everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joemcgill solved in a recent commit.
…ripts-present' of github.com:10up/wordpress-develop into add/issue-48-abort-script-strategy-logic-when-concat-scripts-present
…instead reference new protected property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kt-12 and @10upsimon for getting tests added and fixing up the formatting issue that we discovered in the process of debugging. I went ahead and updated the handle name after confirming in Slack that you were both ok with this change, and also fixed an additional CS issue with spacing that I discovered locally. This is good to merge from my view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @10upsimon, Looks good to me!
…abort-script-strategy-logic-when-concat-scripts-present
…abort-script-strategy-logic-when-concat-scripts-present
Fixes #48
$this->do_concat
if the handle in question is of adefer
orasync
loading strategy.Trac ticket:
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.