-
Notifications
You must be signed in to change notification settings - Fork 34
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
Implement 'js_concat_script_attributes' to allow users to add extra tag attributes #38
Conversation
…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
@mjangda @sboisvert check this out. |
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. |
@sboisvert, right, handles are grouped, but there might be several groups and corresponding script tags on the page: Here's an example for 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 add_filter( 'js_concat_script_attributes', function( $attrs, $href, $js_array, $jsconcat ) {
return in_array( 'jquery-core', $js_array['handles'], true ) ? [] : ['async'];
}, 10, 4 ); |
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. |
Any update on this? Would like to use defer on concatenated JS if possible 🤞 |
While doing some code review this came up as a valid reason to do |
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. |
Superseded by #64 |
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
Fixes #33, supersedes #36