Skip to content
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

Conversation

10upsimon
Copy link

Fixes #48

  • Aborts script concatenation per handle and resets $this->do_concat if the handle in question is of a defer or async 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.

@10upsimon 10upsimon added needs:code-review This requires code review. type:enhancement New feature or request. Script Loading Strategy Issues relating to the Script Loading Strategy (WPP) labels Mar 27, 2023
@10upsimon 10upsimon self-assigned this Mar 27, 2023
// 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;
Copy link
Collaborator

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.

Copy link
Author

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.

@joemcgill joemcgill linked an issue Mar 27, 2023 that may be closed by this pull request
1 task
Copy link
Collaborator

@joemcgill joemcgill left a 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
@kt-12 kt-12 changed the base branch from enhancement/wp-script-api-strategy-base to enhancement/implement_load_handlers March 28, 2023 15:22
@10upsimon
Copy link
Author

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?

@joemcgill this was addressed in a recent commit by @kt-12

Copy link
Collaborator

@joemcgill joemcgill left a 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!

Comment on lines 638 to 648
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&amp;load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&amp;ver={$ver}'></script>\n";
$expected .= "<script type='text/javascript' src='/main-script.js' id='main-defer-script-1-js' async></script>\n";
Copy link
Collaborator

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.

Suggested change
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&amp;load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&amp;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&amp;load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&amp;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' );
Copy link
Collaborator

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.

Copy link

@kt-12 kt-12 Mar 29, 2023

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;
Copy link
Collaborator

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.

Copy link

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.

Copy link
Collaborator

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/' );
Copy link
Collaborator

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.

Copy link
Author

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.

Copy link
Collaborator

@joemcgill joemcgill left a 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.

Copy link

@kt-12 kt-12 left a 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
Base automatically changed from enhancement/implement_load_handlers to enhancement/add_text_template April 3, 2023 05:48
Base automatically changed from enhancement/add_text_template to enhancement/wp-script-api-strategy-base April 3, 2023 05:50
…abort-script-strategy-logic-when-concat-scripts-present
@kt-12 kt-12 merged commit 90e6105 into enhancement/wp-script-api-strategy-base Apr 3, 2023
@kt-12 kt-12 deleted the add/issue-48-abort-script-strategy-logic-when-concat-scripts-present branch April 3, 2023 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:code-review This requires code review. Script Loading Strategy Issues relating to the Script Loading Strategy (WPP) type:enhancement New feature or request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Abort script strategy logical checks when CONCATENATE_SCRIPTS is true
4 participants