Quick Fix for Featherlight JQuery Plugin Causing Jumbled Text On Image Preview.

3 min read

The solution to this problem is quick and easy. There’s a particular line in the minified code that must be revised by the developer to accommodate all image formats. However, for the immediate issue concerning WEBP images, a simple tweak is all that’s needed. This alteration can be applied to any image type that is not currently recognized by the file extension pattern in the regex. By updating this regex expression, we will resolve the issue until a more permanant fix can be implemented.

Original Line Of Code for Featherlight jQuery Plugin:

image:{regex:/\.(png|jpg|jpeg|gif|tiff?|bmp|svg)

WEB Image Fix For Featherlight jQuery Plugin:

image:{regex:/\.(png|jpg|jpeg|gif|tiff?|bmp|svg|webp)

The jumbled text you’re seeing is occurring because the current settings do not accommodate WEBP images or any other image formats that the search tool—referred to as a regex—cannot recognize. However, you can fix this by applying the solution above in the featherlight minified code, enabling your Featherlight jQuery plugin to function correctly with your image file extension.

A More Advanced Fix:

This fix below actually remove the use of REGEX and uses the mime type to determine if it’s an image or not. This would actually be the preferred method to support any image type, but it may not be compatible in all systems, so use the below code with caution as it may not work in every environment. you would replace the first line with this code below.

Advanced Featherlight Jquery Plugin Fix For Images:

image: {
    process: function(url){
        var self = this;
        var deferred = jQuery.Deferred();

        fetch(url, { method: 'HEAD' })
        .then(function(response) {
            if(response.ok && response.headers.get('Content-Type').startsWith('image/')) {
                var img = jQuery('<img>').attr('src', url).on('load', function() {
                    deferred.resolve(img);
                }).on('error', function() {
                    deferred.reject('Image load error');
                });
            } else {
                deferred.reject('Not an image MIME type');
            }
        })
        .catch(function(error) {
            deferred.reject('Error fetching image: ' + error.message);
        });

        return deferred.promise();
    }
}

Disclaimer: Please note that the Advanced Fix, while useful, is not a silver bullet. It should be used with caution as it may not address all issues and could potentially introduce new ones. Always back up your files before using automated tools to modify them. The use of this tool is at your own risk, and it is recommended to consult with a professional if you are not familiar with the code involved.

Location Of Feathelight jQuery Plugin:

This will vary based on how featherlight was implemented in your system. Many theme providers will use this for woocommerce, so there is no one specific place to find the version your website is using, unless you implemented the library yourself. Typically a quick file search for featherlight.min should return good results when searching your main directories.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Carlos Baez

Subscribe now to keep reading and get access to the full archive.

Continue Reading