• Resolved oanishchik

    (@oanishchik)


    Is there a way to make it so that one of the scripts (jquery) is excluded from combining and deferring only on some (specific) pages? I assume this can be done with litespeed_optimise_js_excludes, litespeed_optm_js_defer_exc, and litespeed_optm_gm_js_exc. However, I couldn’t find the exact syntax anywhere. In particular, it is not described here: https://docs.litespeedtech.com/lscache/lscwp/api/. And Google didn’t help either.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support qtwrk

    (@qtwrk)

    function lscwp_custom_excludes($excludes) {
    $my_custom_excludes = array(
    'javascript_1.js',
    'javascript_2.js',
    'javascript_3.js',
    );
    return array_merge($excludes, $my_custom_excludes);
    }
    add_filter('litespeed_optimize_js_excludes', 'lscwp_custom_excludes');
    add_filter('litespeed_optm_js_defer_exc', 'lscwp_custom_excludes');
    add_filter('litespeed_optm_gm_js_exc', 'lscwp_custom_excludes');

    something like this ought do it.

    Thread Starter oanishchik

    (@oanishchik)

    I absolutely can not make it work together with

    if(!is_page(array( … , … ))) {

    }

    Various errors (depending on what I wrap in if).

    Plugin Support qtwrk

    (@qtwrk)

    add_action( 'template_redirect', 'my_lscwp_custom_setting' );
    function my_lscwp_custom_setting() {
    if ( is_page( array( 'about-us', 'contact', 'management' ) ) ) {
    add_filter('litespeed_optimize_js_excludes', 'my_custom_excludes');
    add_filter('litespeed_optm_js_defer_exc', 'my_custom_excludes');
    add_filter('litespeed_optm_gm_js_exc', 'my_custom_excludes');
    }
    }
    function my_custom_excludes($excludes) {
    $my_custom_excludes = array(
    'jquery.js',
    'jquery.min.js',
    );
    return array_merge($excludes, $my_custom_excludes);
    }

    depends on what you want to if test , you can also try to put it inside an action , for example template_redirect , the wp hook has firing sequence , run it too early or too late may not work, find a right timing hook sometimes could be challenging

    Thread Starter oanishchik

    (@oanishchik)

    I think that’s the problem.
    Formally, everything looks right: all scripts that should not be combined and deferred are loaded separately on the page, and the rest are combined and deferred. But the thing is that jquery, on which the other three scripts (also loaded separately) depend, is loaded later than them, according to the error in the console (jQuery is not defined). If I put all of these scripts in exceptions via Page Optimisation – Tuning – Exceptions, then this problem does not occur. Everything works exactly as I need it to. The problem is that I have several pages whose content is entirely formed by js (two contact forms 7 scripts and one additional script – all dependent on jquery). Full optimization causes Google to see these pages as empty, as they are considered to be fully loaded before any content is created by js. If I add all these scripts to Page optimisation – Tuning – Exclude, everything works fine. Except that jquery, which loads on every page of the site (unlike the other scripts I mentioned, which simply don’t load on unnecessary pages), is not optimized. And Google, of course, hates it.

    Plugin Support qtwrk

    (@qtwrk)

    you need to exclude the JQ as well.

    Thread Starter oanishchik

    (@oanishchik)

    Of course, I did.

    But my mistake seems to have been that I excluded some of the scripts via Page Optimisation – Tuning – Exceptions and only excluded jquery via functions.php. Now I’ve excluded them all via functions.php and everything seems to be working as it should.

    The only thing I don’t understand is why Litespeed leaves some scripts unoptimised/unmerged even though they are not listed in any exception lists. For example, in my case, it is one of the theme scripts that seems to load without any special tricks:

    wp_enqueue_script(
    hu-front-scripts’,
    sprintf(‘%1$s/assets/front/js/scripts%2$s.js’, get_template_directory_uri(), ( defined(‘WP_DEBUG’) && true === WP_DEBUG ) ? ” : ‘.min’ ),
    array(‘jquery’, ‘underscore’ ),
    ( defined(‘WP_DEBUG’) && true === WP_DEBUG ) ? time() : HUEMAN_VER,
    true
    );
    wp_script_add_data( ‘hu-front-scripts’, ‘defer’, true );
    }

    Also, it depends on jquery and underscore, which Litespeed will generally optimise and defer according to the settings.

    Thread Starter oanishchik

    (@oanishchik)

    I’ve recached the pages, checked everything with pagespeed and found that, unfortunately, when I exclude the scripts via functions.php, things don’t work the way I’d like. The page is considered to be fully loaded before the scripts have rendered the content on it (it is generated by the scripts of the contact form 7 plugin and another plugin that is additional to it). As a result, pagespeed sees the page as empty. This is unacceptable to me. It works differently when I exclude the same scripts using Page Optimisation – Tuning – Exceptions. My guess is that the scripts are loaded in such a way that they have time to do their work and render everything before the page is considered fully loaded. So I had to go back to the old configuration with exceptions via Page Optimisation – Tuning – Exceptions. But now I have jquery back in exceptions on all pages of the site, not optimized and not deterred( Why do scripts work differently depending on whether they are excluded via functions or Tuning?

    Plugin Support qtwrk

    (@qtwrk)

    if (strpos($_SERVER['REQUEST_URI'], "test") !== false){
    define( 'LITESPEED_CONF', true );
    $js_array = [ 'js1.min.js', 'js2.js', 'js3.js' ];
    define( 'LITESPEED_CONF__OPTM__JS_EXC', $js_array );
    define( 'LITESPEED_CONF__OPTM__JS_DEFER_EXC', $js_array );
    define( 'LITESPEED_CONF__OPTM__GM_JS_EXC', $js_array );
    }

    you can also try this way , add it at top of your wp-config.php , this define will force the conf , make it exact same way as in GUI setting

    Thread Starter oanishchik

    (@oanishchik)

    Since I only need it on a few pages, I tried doing it this way, but, unfortunately, it had no effect at all:

    if (strpos($_SERVER['REQUEST_URI'], "test") !== false && is_page( array( ... , ... ) )){
    define( 'LITESPEED_CONF', true );
    $js_array = [ 'https://DOMAIN.org/wp-includes/js/jquery/jquery.min.js',
    'https://DOMAIN.org/wp-content/plugins/contact-form-7/includes/swv/js/index.js',
    'https://DOMAIN.org/wp-content/plugins/contact-form-7/includes/js/index.js',
    'https://DOMAIN.org/wp-content/plugins/contact-form-7-conditional-fields-pro/js/scripts.js' ];
    define( 'LITESPEED_CONF__OPTM__JS_EXC', $js_array );
    define( 'LITESPEED_CONF__OPTM__JS_DEFER_EXC', $js_array );
    define( 'LITESPEED_CONF__OPTM__GM_JS_EXC', $js_array );
    }

    • This reply was modified 1 year, 4 months ago by oanishchik.
    Plugin Support qtwrk

    (@qtwrk)

    you can not use is_page() in wp-config.php , it’s very early stage, such function is not there yet , you can only use strpos($_SERVER[‘REQUEST_URI’] to match URL , and try use path as /wp-content/plugins/........... not full URL

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘What is the exact syntax of the _exc filters?’ is closed to new replies.