-
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
is_valid_strategy and _doing_wrong check #56
is_valid_strategy and _doing_wrong check #56
Conversation
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.
@kt-12 minor feedback comments from my side, mostly looking good to me though!
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' ); |
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.
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.
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.
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 );
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 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.
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 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.
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.
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' ); |
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.
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() { |
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.
💖
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.
One question, but looks good to me. Thanks!
src/wp-includes/class-wp-scripts.php
Outdated
@@ -863,6 +863,18 @@ private function get_dependents( $handle ) { | |||
return $dependents; | |||
} | |||
|
|||
/** | |||
* Determines if a scripts defined loading strategy is valid. |
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.
* Determines if a scripts defined loading strategy is valid. | |
* Determines if a script loading strategy is valid. |
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 @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; |
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.
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?
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 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.
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.
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.
281aadc
into
feature/enhance-wp-scripts-api-with-a-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.