• I seem to be in an endless loop trying to get complete coverage of webp images and using Cloudflare.

    Here’s my predicament:

    I use Cloudflare, and according to your docs, I should use “CDN Friendly” mode and alter the HTML. But, this option doesn’t allow background images in CSS to be updated. For that to work, I need to use “Varied Image Responses.” But, I can’t use that setting because I use Cloudflare.

    The one solution (I wouldn’t call it a solution) is to set Cloudflare to bypass caching images, but then I lose the benefit of having a cache policy for assets, which also works against what I’m trying to accomplish with page load speeds.

    My last-ditch option seems to be setting up a separate CDN for images, at which point I’m bordering on over-engineering a hypercomplex website all so that my local small business can provide a passable user experience. I absolutely love what this plugin does, but it makes the websites it on at extreme risk of being broken by any administrators who are not technically inclined. I’m not sure what to do at this point.

    Is there any workaround to this?

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi @objektivdigital – I usually use Cache Enabler in my setup for CloudFlare + WebP. Give it a try.

    Thread Starter objektivdigital

    (@objektivdigital)

    Thanks @ashucg — do you simply add Cache Enabler and that’s it, or are there specific configurations that you use? Also, are you directing image traffic through your .htaccess/nginx.conf, or are you using different settings?

    Thread Starter objektivdigital

    (@objektivdigital)

    Okay, so I get this error message on WebP Express settings:

    You are using Cache Enabler, but have not enabled the webp option, so Cache Enabler is not operating with a separate cache for webp-enabled browsers.

    The problem is, I absolutely do have that setting checked off. Am I missing something?

    Hi @objektivdigital,

    Also, are you directing image traffic through your .htaccess/nginx.conf, or are you using different settings?

    No, I am not using Apache or nginx rules to server WebP because Cloudflare only supports varied response in the business plan (as far as I remember but could be wrong.)

    WebP Express:
    Operation Mode: CDN Friendly
    Destination folder: Mingled (required to use Cache Enabler)
    File ext: Append

    Cache Enabler:
    Under Cache Behavior > Variants > [Enable] Create an additional cached version for WebP image support

    And in case I need support for external CSS files I install Fast Velocity Minify to inline the CSS so that Cache Enabler can update URLs. I understand that is not always possible specially if you have a lot of CSS on a large website, so that leaves us with 2 options:

    1. Either we use FVM filter to update URLs in CSS – but then we will have to create 2 files for WebP supported and non-supported browsers.
    2. Use service worker to return WebP whenever JPG/PNG is requested – service workers do not work in Firefox Private window.

    If you are looking for the quickest method and you can not use inline CSS option:
    1. Uninstall FVM (you don’t need it) – you can still keep Cache Enabler to replace some URLs in cached file or not.
    2. Create a service worker in the WP root/installation directory – here is an example
    3. Add following to your functions.php:

    
    add_action('wp_head', function() {
    	?>
    <script>
    if ('serviceWorker' in navigator) {
    	navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
        	console.log('ServiceWorker registration successful with scope: ', registration.scope);
    	}).catch(function(err) {
        	console.log('ServiceWorker registration failed: ', err);
    	});
    }
    </script>
    <?php
    });
    

    Where /service-worker.js is the file that you created in step 2 in the WP root directory.

    Note: You can use the exact service worker from the example but you need to replace line 20 with this: let url = new URL(request.url + '.webp')

    That’s because we need to append .webp to the original file name instead of replacing .jpg or .png.

    If you are just curious or want to use a non-JS based solution:

    – You can use FVM to create different files for webp and non-webp browsers
    – Take a look at fast-velocity-minify/fvm.php and fast-velocity-minify/functions.php
    – Filter to modify code: fvm_after_download_and_minify_code

    You will have to modify stylesheet handlers in order to generate separate cache files. So, it is possible to use Cache Enabler + FVM + external CSS setup as well but that is a bit complex and takes more time.

    I hope this is helpful.

    Thread Starter objektivdigital

    (@objektivdigital)

    @ashucg thank you very much for your comprehensive reply.

    @objektivdigital – just wondering, did it work for you?

    Thread Starter objektivdigital

    (@objektivdigital)

    @ashucg while I have no doubt your solution works, I am trying to find a simpler solution. I want a webp solution that requires as little steps and configurations/code as possible because I have employees whose technical skills are not as good as mine and I need them to be able to implement a solution without my help and without breaking my clients’ production sites.

    At this point, I’ve decided to settle for lower page-load scores for now until there’s either a simpler solution available or until 99% of browsers support .webp. Whichever comes first. I don’t like doing it this way, but I feel like I have no other choice. 🙁

    @objektivdigital – you can create a custom plugin which copies the service worker JS file to WP root and inserts the inline-JS required using wp_head hook. You wouldn’t have to worry about anything, just ask your employees to install that plugin and activate it. Personally I don’t think there are many options for you other than that. You would still have to support old browser versions even if browsers (Safari) adds WebP support in their latest versions.

    Thread Starter objektivdigital

    (@objektivdigital)

    @ashucg creating a plugin sounds like a good idea. Thanks again for your help.

    @objektivdigital happy to help! 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Using Cloudflare = can’t have webp background images in CSS?’ is closed to new replies.