Autocomplete feature causing js error when website under cloudflare rocket script configuration

Dear all,
I spotted a bug being caused by the javascript inserted in the autocomplete.php file of the (very well written) Algolia plugin for wordpress.

Apparently, when used in combination with Cloudflare rocket loader, sometimes the inline script is called before wp-util script, which casuses the error: wp.template is not a function.

I applied a small workaround to prevent the js error, which was blocking other scripts in my website, adding:
if( wp.template == null ) return;

right after the firing of the script:
jQuery(function () {

In this way, my following scripts are fired correctly, but sometimes (let’s say 1 time every 4 page loads) the autocomplete search is not fired.
I think a good solution would be to assure that wp-util script is loaded before executing this code. What do you think?
Thanks,
p.s: this is the website, for your referecen: EUI personal websites

Emanuele Strano
Web Developer @ European University Institute

1 Like

Wow @eui nice catch!

Currently we use the wp_footer hook to inject the autocomplete scripts but we plan to change it because some themes don’t use that and it can also make it load after wp-utils was loaded.

I opened an issue here: https://github.com/algolia/algoliasearch-wordpress/issues/467

Thanks for sharing your workaround, but we indeed need to resolve this issue to make sure h-the autocomplete dropdown always works as expected.