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

is_valid_strategy and _doing_wrong check #56

Conversation

kt-12
Copy link

@kt-12 kt-12 commented Apr 13, 2023

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.

@kt-12 kt-12 requested a review from 10upsimon April 13, 2023 15:35
@kt-12 kt-12 added the Script Loading Strategy Issues relating to the Script Loading Strategy (WPP) label Apr 13, 2023
@kt-12 kt-12 marked this pull request as ready for review April 14, 2023 04:16
Copy link

@10upsimon 10upsimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kt-12 minor feedback comments from my side, mostly looking good to me though!

src/wp-includes/class-wp-scripts.php Outdated Show resolved Hide resolved
src/wp-includes/class-wp-scripts.php Show resolved Hide resolved
src/wp-includes/class-wp-scripts.php Show resolved Hide resolved
src/wp-includes/class-wp-scripts.php Outdated Show resolved Hide resolved
src/wp-includes/class-wp-scripts.php Show resolved Hide resolved
src/wp-includes/class-wp-scripts.php Outdated Show resolved Hide resolved
tests/phpunit/tests/dependencies/scripts.php Outdated Show resolved Hide resolved
kt-12 and others added 4 commits April 14, 2023 11:57
Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com>
Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com>
Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com>
Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com>
* @return bool True if the strategy is of an allowed type, false otherwise.
*/
private function is_valid_strategy( $strategy ) {
$allowed_strategies = array( 'blocking', 'defer', 'async' );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a filter be applied here so plugins could add experimental strategies like 'worker'? Could also be added later, but I'd love to ensure this is extensible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd be ok with leaving this as is for the initial release and add the additional filter based on developer feedback rather than adding it already. I believe if someone really had a use case where they were wanting to support additional strategies, they would be better off extending the whole class and replacing the global WP_Scripts object, rather than trying to filter Core's implementation.

Agree that a filter here could be useful, but we would also need to consider the side effects if someone were to filter this list to remove some/all of our supported strategies as a defensive move. Perhaps only make the filter additive, like this pseudocode example?

$additional_strategies = apply_filter( 'wp_add_script_loading_strategy', array() )

$allowed_strategies  = array_merge( array( 'blocking', 'defer', 'async' ), $additional_strategies );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I share @joemcgill's sentiment here, but from a slightly different perspective of what does it mean for the rest of the process? We do a lot of deciding on what the most eligible/applicable strategy for a script is based on the known strategies of defer, async or blocking. What does it means for the dependency tree when a developer introduces a custom/other strategy? Does it mean we need to abort any logical checks and simply output the custom strategy? I have the same question for inline script handling.

Copy link
Author

@kt-12 kt-12 Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @joemcgill here. There were many instances in the past where people created a plugin to stop a feature; the classic example is Classic editor plugins for Gutenberg. Even though this is not the same case, there will be some people who want to seize this opportunity and build custom plugins to do so. Core strategies should not be removed at all, as they can opt-out by not using any strategy. Again, as @10upsimon rightly suggested, there is much more to consider in terms of expected behaviour before we add this filter.

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.

Looks good to me pending resolution of whether we want to add a filter. That could also be handled in a follow-up task, in my opinion

* @return bool True if the strategy is of an allowed type, false otherwise.
*/
private function is_valid_strategy( $strategy ) {
$allowed_strategies = array( 'blocking', 'defer', 'async' );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd be ok with leaving this as is for the initial release and add the additional filter based on developer feedback rather than adding it already. I believe if someone really had a use case where they were wanting to support additional strategies, they would be better off extending the whole class and replacing the global WP_Scripts object, rather than trying to filter Core's implementation.

Agree that a filter here could be useful, but we would also need to consider the side effects if someone were to filter this list to remove some/all of our supported strategies as a defensive move. Perhaps only make the filter additive, like this pseudocode example?

$additional_strategies = apply_filter( 'wp_add_script_loading_strategy', array() )

$allowed_strategies  = array_merge( array( 'blocking', 'defer', 'async' ), $additional_strategies );

*
* @ticket 12009
*/
public function test_script_strategy_doing_it_wrong() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💖

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.

One question, but looks good to me. Thanks!

@@ -863,6 +863,18 @@ private function get_dependents( $handle ) {
return $dependents;
}

/**
* Determines if a scripts defined loading strategy is valid.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Determines if a scripts defined loading strategy is valid.
* Determines if a script loading strategy is valid.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joemcgill @kt-12 I addressed this as part of a recent commit. I also changed the param description to be more reflective of your suggestion @joemcgill

@@ -871,8 +883,24 @@ private function get_dependents( $handle ) {
*/
private function get_intended_strategy( $handle ) {
$script_args = $this->get_data( $handle, 'script_args' );
$strategy = isset( $script_args['strategy'] ) ? $script_args['strategy'] : false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here is someone passes an empty string or false as a strategy? Do we ever hit this codepath or do we bail beforehand?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joemcgill passing an empty or otherwise illegal value will have no ill bearing on the process. If no strategy is passed, it will still proceed to the next part of logic, which calls $this->is_valid_strategy( $strategy ). At this point, it will fail and return false, and the default blocking strategy will be used as part of the get_eligible_loading_strategy() method call.

Arguably, we are probably better off with a ! empty( $script_args['strategy'] ) as opposed to the current isset( $script_args['strategy'] ) check, as an empty string would still default to true for an isset() check, but either one is not changing the final outcome 2 lines down from the check.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only side effect of using ! empty() would be if 'strategy' as a key was never set, but that is pretty much impossible in our logic given that we normalize the args. Either way though, what we have now is logical so I'm going to proceed with this merge, so that we have a final codebase in the base PR to do one final test against.

@10upsimon 10upsimon merged commit 281aadc into feature/enhance-wp-scripts-api-with-a-loading-strategy Apr 21, 2023
@10upsimon 10upsimon deleted the enhancement/add_valid_strategy_function branch April 21, 2023 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Script Loading Strategy Issues relating to the Script Loading Strategy (WPP)
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

4 participants