Title: WP REST Cache + Cloudinary not working
Last modified: June 15, 2022

---

# WP REST Cache + Cloudinary not working

 *  Resolved [mateogarcia](https://wordpress.org/support/users/mateogarcia/)
 * (@mateogarcia)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/wp-rest-cache-cloudinary-not-working/)
 * Good morning support team.
 * The WP REST Cache plugin doesn’t seem to be working with Cloudinary URLs. Apparently,
   there is a race condition that causes the cache record to save the images from
   the WordPress storage and not from Cloudinary.
 * WP REST Cache 2021.4.1 + Cloudinary 2.1.9: The cache record is saved with the
   Cloudinary path properly.
 * WP REST Cache 2021.4.1 + Cloudinary 2.7.7 onwards: The first request works properly
   then in cache it saves the URLs as if they were files uploaded to WordPress.
 * WordPress version 5.9.3
 * I hope you can help me and thanks in advance.

Viewing 1 replies (of 1 total)

 *  Plugin Author [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * (@rockfire)
 * [3 years, 10 months ago](https://wordpress.org/support/topic/wp-rest-cache-cloudinary-not-working/#post-15818339)
 * Hi [@mateogarcia](https://wordpress.org/support/users/mateogarcia/)
 * Sorry for the late reply.
 * I did some investigation of Cloudinary to understand the issue. The problem is
   caused by the way they do the string replacement of the url’s. They use the output
   buffer to capture the output from WordPress and do a string replace on this buffer.
   See: [https://plugins.trac.wordpress.org/browser/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/trunk/php/class-string-replace.php#L129](https://plugins.trac.wordpress.org/browser/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/trunk/php/class-string-replace.php#L129)
 * Now when you call an endpoint the first time when no cache is available our plugin
   lets WordPress do its magic and just before it outputs the REST response, using
   the [rest_pre_echo_response](https://developer.wordpress.org/reference/hooks/rest_pre_echo_response/)
   filter we catch the output to cache it. Cloudinary however does its string replace
   magic after this filter and therefore its changes to the REST API aren’t cached.(
   N.B. I don’t think anyone should use output buffering unless there really isn’t
   any other way. This describes why you shouldn’t use it: [https://wordpress.stackexchange.com/a/41014](https://wordpress.stackexchange.com/a/41014)).
   
   Well now we know the problem, can we fix it? Short answer: no we can’t, Cloudinary
   can.
 * Small technical explanation of how our plugin works: if there isn’t a cache reord
   available WordPress can do it’s magic along with all plugins and the theme. Now
   if a cache record is available we abort execution of WordPress and all plugins
   and the theme and return the cache record.
 * Since Cloudinary uses output buffering there isn’t any filter after that we can
   hook into so we can also cache their changes to the REST API. And since we did
   create a cache (without their changes) any following call will result in the 
   cache being returned and Cloudinary’s code not being executed. Cloudinary however
   could change their code to not use output buffering (at least for the REST API)
   and also hook into the `rest_pre_echo_response` filter to do their magic. Since
   we have set the priority of our use of the filter very low, our code will probably
   be executed after theirs and that way we will cache their changes.
 * So to sum things up: in order for this problem to be resolved Cloudinary should
   change the way they replace the url’s.

Viewing 1 replies (of 1 total)

The topic ‘WP REST Cache + Cloudinary not working’ is closed to new replies.

 * ![](https://ps.w.org/wp-rest-cache/assets/icon-256x256.png?rev=3328849)
 * [WP REST Cache](https://wordpress.org/plugins/wp-rest-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-rest-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-rest-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-rest-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-rest-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-rest-cache/reviews/)

## Tags

 * [cache](https://wordpress.org/support/topic-tag/cache/)
 * [cloudinary](https://wordpress.org/support/topic-tag/cloudinary/)

 * 1 reply
 * 2 participants
 * Last reply from: [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * Last activity: [3 years, 10 months ago](https://wordpress.org/support/topic/wp-rest-cache-cloudinary-not-working/#post-15818339)
 * Status: resolved