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

Implement 'js_concat_script_attributes' to allow users to add extra tag attributes #38

Conversation

rinatkhaziev
Copy link
Contributor

The filter gets passed an empty array and has to return an associative or regular array with attributes as keys, additional arguments for the filter are: $href - current batch url, $js_array - additional data for current batch of scripts.

This filter runs on each batch of files; extra arguments are needed to better control attributes being assigned to a resulting tag.

For example, we want to set async/defer for either a specific group of files being concatenated or maybe we want to do a blanket change for every resulting script tag.

A minimal example would be

add_filter( 'js_concat_script_attributes', function( $attrs, $href, $js_array, $jsconcat ) {
	return stristr( $href, '_static' ) !== false ? [ 'async' => true, 'defer' ] : [];
}, 10, 4 );

Fixes #33, supersedes #36

…ag attributes, the filter gets passed an empty array and has to return an associative or regular array with attributes as keys, additional arguments for the filter are: $href - current batch url, $js_array - additional data for current batch of scripts.

This filter runs on each batch of files; extra arguments are needed to better control attributes being assigned to a resulting tag.

For example, we want to set async/defer for either a specific group of files being concatenated or maybe we want to do a blanket change for every resulting script tag.

Fixes Automattic#33, supersedes Automattic#36
@rinatkhaziev
Copy link
Contributor Author

@mjangda @sboisvert check this out.

@sboisvert
Copy link

I might be missing something, but isn't the grouping already done at this point? If so is there a way for folks to group some to defer, some to async etc? Perhaps I'm just not seeing it.

@rinatkhaziev
Copy link
Contributor Author

@sboisvert, right, handles are grouped, but there might be several groups and corresponding script tags on the page:

Here's an example for wp-admin request:

array (size=3)
  'type' => string 'concat' (length=6)
  'paths' => 
    array (size=5)
      0 => string '/wp-includes/js/jquery/jquery.js' (length=32)
      1 => string '/wp-includes/js/jquery/jquery-migrate.min.js' (length=44)
      2 => string '/wp-includes/js/utils.min.js' (length=28)
      3 => string '/wp-includes/js/jquery/ui/core.min.js' (length=37)
      4 => string '/wp-includes/js/underscore.min.js' (length=33)
  'handles' => 
    array (size=5)
      0 => string 'jquery-core' (length=11)
      1 => string 'jquery-migrate' (length=14)
      2 => string 'utils' (length=5)
      3 => string 'jquery-ui-core' (length=14)
      4 => string 'underscore' (length=10)
/srv/www/wp-content/mu-plugins/local-dev-helper-ignored.php:47:string 'http://example.com/_static/??-eJzTLy/QzcxLzilNSS3WzwKiwtLUokoopZdVrKOPT4FubmZ6UWJJql5uZh42xaUlmTnFuCShJpVm6ifnF+E2Ii8ltagYSYV9rq2hiYWhgamZmYlpFgAj50YY' (length=151)
/srv/www/wp-content/mu-plugins/local-dev-helper-ignored.php:47:
array (size=4)
  'type' => string 'concat' (length=6)
  'paths' => 
    array (size=1)
      0 => string '/wp-includes/js/jquery/ui/datepicker.min.js' (length=43)
  'handles' => 
    array (size=1)
      0 => string 'jquery-ui-datepicker' (length=20)
  'extras' => 
    array (size=1)
      'after' => 
        array (size=1)
          0 => string '<script type='text/javascript'>
jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({"closeText":"Close","currentText":"Today","monthNames":["January","February","March","April","May","June","July","August","September","October","November","December"],"monthNamesShort":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"nextText":"Next","prevText":"Previous","dayNames":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"dayNamesShort":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"dayNamesMin":["S","M","T","W","T","F","S"],"dateFormat":"MM d, yy","firstDay":1,"isRTL":false});});
</script>
' (length=657)
/srv/www/wp-content/mu-plugins/local-dev-helper-ignored.php:47:string 'http://example.com/wp-includes/js/jquery/ui/datepicker.min.js?m=1481056645g' (length=76)

These get concatenated, but we might not want them to have any extra attributes set. So, for example, we'd check whether this group contains jquery-core and based on that we won't assign any extra args, like that:

add_filter( 'js_concat_script_attributes', function( $attrs, $href, $js_array, $jsconcat ) {
	return in_array( 'jquery-core', $js_array['handles'], true ) ? [] : ['async'];
}, 10, 4 );

@sboisvert
Copy link

Just to post a bit of our slack convo. I got confused and started thinking about re-grouping the scripts which is totally out of scope and not the original intention.

@sboisvert
Copy link

Looks good to me.

@mboynes @mjangda any feedback?

@jenkoian
Copy link

jenkoian commented Mar 4, 2021

Any update on this? Would like to use defer on concatenated JS if possible 🤞

@mikeyarce
Copy link
Member

While doing some code review this came up as a valid reason to do add_action( 'js_do_concat', '__return_false' );.
What are your thoughts about this ... 3 years later, @rinatkhaziev ? 😄

@rinatkhaziev
Copy link
Contributor Author

rinatkhaziev commented Apr 29, 2021

@mikeyarce

What are your thoughts about this ... 3 years later,

Time sure flies :)

This mostly looks good, 3 years later I'd explicitly checked against the list of allowed script attributes per MDN

If you want to take a swing at it, either by taking over the PR and resolving the merge conflict or coming up with a newer and cooler version, we can review and merge.

@rinatkhaziev
Copy link
Contributor Author

Superseded by #64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding Async/Defer to the JSConcat script
4 participants