Tagged: CSS, javascript, performance, unload
-
AuthorPosts
-
July 30, 2024 at 3:55 pm #1463342
Hi – I hope you can help.
I am using a chld theme based on Enfold and on the performance tab I can set it only to load page builder elements that are being used on the pages. Looking at the page source, I can see that the theme is still loading all JS and CSS, even for elements that are not being used.
For the sake of performance and bandwidth, how can I get the theme to exclude any unused JS/CSS automatically? Is there a way to do this?Many thanks in advance,
Anthony
July 30, 2024 at 6:16 pm #1463359this is true – but the setting of enfold does not support it for each page/post.
“By default the theme will only load elements that are used on your posts and pages. You can disable the feature or manually manage loaded elements if you run into trouble”.
“on your posts and pages” – means every element that has been used for your whole installation is loaded. If you use a table on page A – that script will be loaded on page B too.
But you are right – if that would be possible to look what elements are used on a certain page – and loads only these files loading that page will be a good thing. I do not know if this is possible.Or if it is possible – maybe the performance benefit would be so great that checking on loading the page which elements are used would take away this advantage.
July 30, 2024 at 6:25 pm #1463360For example – i got this check for my contact form 7 element.
I do want to load all css and scripts only if i use the cf7 on my page:function contactform_dequeue_scripts(){ $load_scripts = false; $post = get_post(); if(has_shortcode($post->post_content,'contact-form-7')){$load_scripts = true;} if(!$load_scripts){wp_dequeue_script('contact-form-7');wp_dequeue_style('contact-form-7');} } add_action( 'wp_enqueue_scripts', 'contactform_dequeue_scripts', 99 );
I then implemented a similar behavior for Google Recaptcha. Where there are no form fields to be filled in, I do not include the Recaptcha.
over the filter avf_load_google_recaptcha_api_prohibitedThis has the similar basic idea of not unnecessarily burdening the performance of the other pages.
August 3, 2024 at 4:20 pm #1463701Hi,
Thanks for sharing Guenni007, I have not seen this solution before.Best regards,
MikeAugust 3, 2024 at 9:57 pm #1463727But the idea would be worth considering.
To see which enfold shortcodes are used on the page and then only load these scripts/styles.
I don’t know whether there is a performance gain, because this check also takes time.In the examples mentioned above, it is not a problem because a contact form only needs its scripts after the form has been filled in. The same applies to a ReCaptcha.
August 3, 2024 at 10:08 pm #1463730Hi,
This could be helpful to remove some css on certain pages, such as a conflicting plugin on the woo cart page, or a plugin form css on a contact page. I have not been able to do this, but your code seems to be on the right path.Best regards,
MikeAugust 3, 2024 at 10:18 pm #1463732here is the code i use for google recaptcha:
function prohibit_google_recaptcha( $prohibited ){ global $post; if( ! $post instanceof WP_Post ){ return $prohibited; } $content = Avia_Builder()->get_post_content( $post->ID ); $prohibited = ( false !== strpos( $content, '[contact-form-7 ' ) || false !== strpos( $content, '[av_contact ' ) ) ? false : true; return $prohibited; } add_filter( 'avf_load_google_recaptcha_api_prohibited', 'prohibit_google_recaptcha', 10, 1 );
so – only if contact-form-7 or enfold contact shortcode is found the recaptcha is loaded.
August 27, 2024 at 12:29 pm #1465489That all seems to be moving in the right direction. One issue in particular that I would like to sort is the loading of the Google Maps api on all pages – we don’t use Google maps anywhere on the website so that would be a good start. Do you have some code that would allow me to dequeue that unconditionally on all pages?
Many thanks in advance,
Anthony
August 28, 2024 at 10:09 am #1465548if you do not use google maps – do not enter the google maps api key in enfold options.
i guess that will be enough.if not you can try this in your child-theme functions.php:
function ava_disable_gmap() { add_filter('avf_load_google_map_api', function($load_google_map_api) { $load_google_map_api = false; return $load_google_map_api; },10,1); add_filter('avia_google_maps_widget_load_api', function($load_google_map_api) { $load_google_map_api = false; return $load_google_map_api; },10,1); } add_action('after_setup_theme', 'ava_disable_gmap');
September 2, 2024 at 3:17 pm #1466052That’s great, thank you – I can adapt that code block to unload other libraries too so it’s really useful.
September 4, 2024 at 6:50 am #1466182Going back to your original question and a solution for you:
It would be an excellent feature for Enfold to Automatically do its own version of Remove unused CSS to bring the number of JS and CSS files down per page.
However if you wanted to do this using a plugin WP-Rocket and others come with remove unused CSS or RUCSS as it is known. I have had good results with Enfold and RUCSS in WP-Rocket. Seems to work without specifying any fallback css or exceptions.
RUCSS is not perfect however. So if Enfold could add an option to only load the CSS for used elements per page it would be a 100% compatible way of doing RUCSS without the unreliability of systems that attempts to work it out after css file generation.
I’d be keen to know what Gunter thinks of this idea. It is essentially an extension of the current only load used elements function.
Performance advice and how it works in reality
All of this said – Once a browser has downloaded the files – Assuming you have a long cache life on your CSS and JS files in your HT Access file (Or set this value with a plugin like WP-Rocket or WP Super cache, etc.) then those files wont be re-downloaded by the visitors browser per page visit. So even if they have to download 100kb worth of CSS and JS to view page 1. page 2,3,4,5,6 etc onwards will use the cached css loading instantaneously.The minimum requirement for this to happen is page caching with a good cache life.
If you want any more advice on this sort of thing let me know.
https://www.hirekaraoke.co.uk/
The site above is one of ours and passes web vitals. They are the UKs largest Karaoke Hire company. So a national business. This site uses our performance stack including page caching and some bespoke settings. The actual booking pages are completely uncached (Which is why the TTFB score is a little higher on desktops – brings the average response time up). The site just serves minified CSS. It is not necessary to RUCSS to pass web vitals if everything else is done correctly.- This reply was modified 3 months, 2 weeks ago by thinkjarvis.
-
AuthorPosts
- You must be logged in to reply to this topic.