• Resolved Generosus

    (@generosus)


    Good Day,

    We have partially covered this topic here and here.

    After careful evaluation, once again, we need to ask you a favor. We need a code snippet that will help us dequeue and deregister your “dynamic” JS files (e.g.,/jc.js and /base.js) on mobile devices and tablets for your new plugin version, V3.+

    Based on one of your previous comments, we don’t see a need for the dynamic JS files (nor corresponding dynamic data-handles) since the odds for a website to have more than one language selector on one page is practically nil.

    So, in short, it would be awesome if you can update your plugin’s code to help with this request. Yours is the only plugin that we know of that uses dynamic JS files (i.e., data-handles). Heck, it didn’t before V3.+!

    If you can change them to them to static, we can easily dequeue and deregister your JS files with the PHP code snippet provided below, which we already use for other plugins.

    add_action( 'wp_print_scripts', 'dequeue_unnecessary_scripts' );
    
    function dequeue_unnecessary_scripts() {
       
    	if(window.innerWidth < 760) {
    		
            wp_dequeue_script( 'js-data-handle-1' );
            wp_deregister_script( 'js-data-handle-1' );
    		
    	wp_dequeue_script( 'js-data-handle-2' );
            wp_deregister_script( 'js-data-handle-2' );
    	   
        }
    }

    As you know, every bit of payload saved helps improve website performance.

    Here’s a great corollary:

    "Even the greatest ships can sink one leak at a time." -- Thomas A. Edison.

    Thanks for reading. Thanks for your help!

Viewing 1 replies (of 1 total)
  • Plugin Author edo888

    (@edo888)

    Hi,

    TLDR: Adding a feature which I will not be able to support is a bad idea.

    I’m not sure you really understand what you are doing, since I see a JS code inside PHP context, also I’m not sure the code you use is going to do what you intend it to do because of the order of execution and the context.

    Before optimizing the performance you need to be able to measure it correctly and I do not see that you are doing it or understand how much gain you will get by doing what you plan to do. You need to measure the gain and the effort you will spend to get it done to be able to make conclusions. My opinion is that it does not worth the effort and can cause a lot of headaches than do good.

    In theory PHP offloading is the best, however it is going to cause issues with cache plugins, since when you have a cache plugin enabled usually PHP code you are using to do offloading is not going to be executed with every page load (depends on the cache plugin). You must be sure that cache plugin is keeping 2 separate caches for mobile and for desktop. And the condition used in that plugin to separate the device types is exactly the same in your code (now you have JS code in if statement). So you need to make sure that your code and cache act in sync.

    You do not need the handle to be static to be able to offload it. I’m providing you a code sample, so you can get the idea:

    global $wp_scripts;
    $registered_handles = array_keys($wp_scripts->registered);
    foreach($registered_handles as $handle) {
    if(strpos($handle, 'gt_widget_script') !== false)
         // do whatever you need with the $handle, for example dequeue
    }

    The other option is to use javascript to load the scripts conditionally, but you are going to replace one script with another.

    Thanks! ??

Viewing 1 replies (of 1 total)
  • The topic ‘Help Appreciated | Need Code to Dequeue and Deregister JS Files On Mobile’ is closed to new replies.