WP-rocket aggressive purge

In some cases WP-rocket cache plugin empties all cached files although all you do is update a post or product. Or for me when a customer does a purchase that changes the stock value for a WooCommerce product. What I can see, this doesn’t happen for me when updating a product from within WP. So it’s not consistent.

I found this is caused in rocket_get_purge_urls in inc/common/purge.php. When checking for previous and next posts to clear it sometimes finds the front page and adds that url to $purge_urls. This doesn’t only clear the cache for the actual front page, but all the cached files in that directory, i.e. the complete site!

I’ve sent this bug to the WP-rocket team, hopefully they can find a way to fix it.

But until then one way to work around this is to add a filter that always ignores the home page url. This filter doesn’t affect the domain_clean, so no worries when updating WP options or theme settings that should empty all cache.

function disable_cache_clearing_files( $urls ){
  // urls to exclude
  $exclude_urls = [ get_site_url(), get_site_url() . "/" ]; //, $_SERVER['SERVER_NAME'] ];

  // get all urls except excluded urls
  $urls = array_diff( $urls, $exclude_urls);
  
  return $urls;
}
add_filter( 'rocket_post_purge_urls', 'disable_cache_clearing_files');

The issue is also explained here:

https://github.com/wp-media/wp-rocket/issues/3246

https://github.com/wp-media/wp-rocket/issues/2549

https://docs.wp-rocket.me/article/1491-using-wp-rocket-on-your-woocommerce-site#cache-purging

https://docs.wp-rocket.me/article/1640-cache-is-incomplete-cleared-too-frequently-or-randomly

https://docs.wp-rocket.me/article/78-how-often-is-the-cache-updated#full-cache-deletion

https://docs.wp-rocket.me/article/137-disable-all-automatic-cache-clearing