Hi GD team,
im working on making my website slimmer by removing any unused assets that would cause a http request. I did notice that GD loads on all pages and im not that well-known on how to do it, but if you could somehow just load assets on pages that actually require styling and javacscripts it would improve performance for my website quite alot. Its so difficult to identify which css or js i can disable (i use perfmatters for disabling assets on each page),
I did find a blog were they state the issue and how they solved it but im not sure if that would work for you:
Source: https://www.cozmoslabs.com/58990-remove-unused-css-js-files-in-wordpress/
LOAD ASSETS ONLY IF SHORTCODE IS EXECUTED
We’ve been using this technique on almost all our plugin assets for quite some time.
Basically, we have a global variable that gets initiated in our shortcode, and if it’s set and true, we’re loading our assets like so:
// create a shortcode that registers a shortcode
function cozmoslabs_some_shortcode( $atts ) {
global $cozmoslabs_some_shortcode;
$cozmoslabs_some_shortcode = true;
return ‘We have a shortcode’;
}
add_shortcode(‘cozmoslabs_short’, ‘cozmoslabs_some_shortcode’);
function cozmoslabs_enqueue_script() {
global $cozmoslabs_some_shortcode;
if ( isset($cozmoslabs_some_shortcode) && $cozmoslabs_some_shortcode == true ) {
wp_enqueue_script( ‘my-js’, ‘filename.js’, true );
}
}
add_action( ‘wp_footer’, ‘cozmoslabs_enqueue_script’, 90000 );
Unfortunately, there are drawbacks to this as well:
the scripts need to be added to wp_footer with a late priority because if we add them to wp_enqueue_scripts, our global variable has a good chance not to be set
if you need something added in the header of the site, this won’t work
in wp_enqueue_script() you have to set $in_footer to true