0

I'm trying to disable Cloudflare's Rocket Loader for specific scripts. At first I'm trying to disable it for mobile slider. I tried various combinations of this (and every other I found on StackOverflow) code:

add_filter( 'script_loader_tag', function ( $tag, $handle ) {

    if ( 'writeup-owl-carousel' !== $handle )
        return $tag;

    return str_replace( "type='text/javascript' src", ' data-cfasync="false" src', $tag     );
}, 10, 2 );

This script is responsible for the element that I'm trying to exclude from rocket loader:

if ( ! function_exists( 'writeup_scripts' ) ) {

    function writeup_scripts() {

        wp_enqueue_script( 'writeup-owl-carousel', get_template_directory_uri() . '/js/owl.carousel.min.js', array( 'jquery' ),'', true );
    }  
}

When I paste this code in functions.php, the whole element (slider) dissapears. Does anyone know what's wrong with this code? I feel like I've tried everything...

cabrerahector
  • 3,653
  • 4
  • 16
  • 27
kacper3355
  • 67
  • 1
  • 8
  • 1
    Try removing `type='text/javascript'` from the first code snippet and check again. – cabrerahector Mar 28 '19 at 20:15
  • Unfortunately it didn't changed anything. The slider still dissapears :( – kacper3355 Mar 28 '19 at 20:37
  • Got any other ideas? – kacper3355 Mar 28 '19 at 20:51
  • Is the script being enqueued on your site? If so, is it getting the `data-cfasync` property or not? – cabrerahector Mar 28 '19 at 21:11
  • Yes, the script is being enqueued on my site. How can I check if its getting the data-cfasync property? – kacper3355 Mar 28 '19 at 21:51
  • Just check the source code of the page :P – cabrerahector Mar 28 '19 at 21:55
  • I think so then :) I found this in my source code: type='text/javascript' data-cfasync="false" src='https://example.com/min/b3895.js'> - there's the slider code inside that minified file. I dont know why there's still "type='text/javascript' - I removed it from that code, just like you said. That's strange. – kacper3355 Mar 28 '19 at 22:02
  • If I had to guess: caching. Do you have a caching plugin installed on your site? If so, it might be a good idea to disable it while you're developing stuff. – cabrerahector Mar 28 '19 at 22:59
  • This is not cache-related. I purge cache every time I make any change. I use LiteSpeed cache. Do you have any other ideas? This is really strange - I really feel like I've already tried everything. When I paste the same code on a website that doesn't use Cloudflare (same theme, same plugins etc - clone of my main site), the slider works fine so the code must be affecting with Cloudflare. – kacper3355 Mar 28 '19 at 23:13
  • Apparently, this should help you out: https://stackoverflow.com/a/42888777/1836697 – peterkodermac Aug 27 '19 at 16:32
  • You may prefer to create a page rule... https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts- – Ricardo Martins Mar 30 '20 at 01:07

0 Answers0