Reputation: 373
I am using the WebP Express plugin in WordPress, and it correctly replaces elements with tags when the page loads initially. However, when I load additional content via AJAX, the newly added images remain in their original format (.jpg or .png) and are not converted to WebP.
What I have tried:
setTimeout(() => {
document.dispatchEvent(new Event('DOMContentLoaded'));
}, 200);
echo apply_filters('the_content', wp_get_attachment_image($pic['ID'], 'medium_large'));
How AJAX works in my setup:
My WordPress AJAX function (wp_ajax_loadmore_reviews) returns pre-rendered HTML, which is then appended to the page via JavaScript.
WebP Express correctly converts images when the page is initially loaded, but does not process images appended dynamically.
My Question: How can I ensure that WebP Express processes dynamically loaded images after AJAX? Is there a way to force WebP Express to recognize and convert newly loaded images without manually modifying src attributes in JavaScript?
Any guidance would be greatly appreciated!
Upvotes: 1
Views: 41
Reputation: 373
I found that WebP Express works correctly even without the <picture>
tag. The reason is that my server is running Nginx with specific rules that serve WebP images directly at the server level, so WebP Express does not need to modify the HTML.
How WebP Express Works on My Setup
.webp
images accordingly..jpg/.png
, browsers actually receive .webp
files due to server-side handling.My Nginx WebP Rewrite Rules
# WebP Express rules
# --------------------
location ~* ^/?wp-content/.*\.(png|jpe?g)$ {
add_header Vary Accept;
expires 365d;
if ($http_accept !~* "webp"){
break;
}
try_files
/wp-content/webp-express/webp-images/doc-root/$uri.webp
$uri.webp
/wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content
;
}
# Route requests for non-existing webps to the converter
location ~* ^/?wp-content/.*\.(png|jpe?g)\.webp$ {
try_files
$uri
/wp-content/plugins/webp-express/wod/webp-realizer.php?xdestination=x$request_filename&wp-content=wp-content
;
}
# ------------------- (WebP Express rules ends here)
How I Verified This
.jpg/.png
, but the browser actually loads .webp
..jpg/.png
when WebP is not supported.Upvotes: 0