I’m working on a site with a performance budget, so I want to be sure to only load certain assets as needed (e.g. via IntersectionObserver, etc).
I know I can omit, say, jQuery, by setting head_js(false), but is there any way I can selectively choose which assets to return with fire_plugin_hook('public_head')?
I’ve tried to get the output using the methods below but they both return empty strings from header.php, so I might not be understanding the documentation.
The trouble is that generally things that are adding scripts are using the “queue” functions, so they don’t actually output anything themselves.
You should be able to use get_specific_plugin_hook_output anyway, but you won’t actually use its output. Just making the call(s) before head_js/head_css actually runs should let things get added to the queue.
… which would allow me to asynchronously micro-manage when/if/how each asset is loaded.
Right now, I’m just omitting the call to fire_plugin_hook('public_head') and loading select plugin assets manually, but obviously whitelisting doesn’t scale very well. A blacklist would work fine, though (e.g. load all the stuff except for Geolocation scripts).
Is there any way to view/modify the contents of the queue before head_js() and head_css() are called?
The headScript and headStyle helpers are what store the queue. They implement IteratorAggregate and ArrayAccess so you should be get at their contents with foreach or similar, and index/unset on them like an array.
Ok, so this looks like the way to do it. For whatever reason, the index on the array of assets was getting reset with each unset(), which was sort of confusing/unexpected, so rather than dealing with that, I just set all the properties for each asset to null.