wp-rocket icon indicating copy to clipboard operation
wp-rocket copied to clipboard

Make Cache Lifespan also purge Cloudflare APO Cache

Open joejoe04 opened this issue 2 years ago • 7 comments

Is your feature request related to a problem? Please describe. Per the discussion here: https://wp-media.slack.com/archives/C056ZJMHG0P/p1694706892420009 https://wp-media.slack.com/archives/C056ZJMHG0P/p1694706892657559

Currently, the Cache Lifespan option is not purging the Cloudflare APO cache, as we don't have the Cloudflare APO purge hooked on it.

Describe the solution you'd like When Cache Lifespan clears cache, it should also clear the Cloudflare APO cache for the related pages.

This was brought to attention by a user with Cloudflare APO who is having errors caused due to expired nonces that are still present in the APO cache because it's not being cleared by the Cache Lifespan.

Additional context https://secure.helpscout.net/conversation/2344380208/439669/ https://www.siaonlinestore.com/

joejoe04 avatar Sep 15 '23 16:09 joejoe04

In doing some additional testing to create a helper plugin to add this functionality until the issue is resolved, I noticed that WP Rocket functions like rocket_clean_home() are also not clearing the APO cache, so I think it would be good to apply the fix for cases when those functions are used as well.

Discussion thread for more info about this: https://wp-media.slack.com/archives/C056ZJMHG0P/p1695747439445229

joejoe04 avatar Sep 28 '23 15:09 joejoe04

https://secure.helpscout.net/conversation/2659734118/504084?folderId=377611

webtrainingwheels avatar Jul 23 '24 17:07 webtrainingwheels

Sorry to barge in but why is this not standard behavior? I've wasted so many hours trying to figure out why the two caches didn't match, until I dove deep into the docs and found this bit:

The automatic cache purging for Cloudflare APO Cache won't be triggered when Cache Lifespan runs the cache clearing for expired cache files.

Which leads me to some questions:

  • Why would one want APO's cache to clear on manual purge but not on auto purge? This is going to lead to cache mismatch 100% of the time, I really don't see the use case here
  • Why isn't this at least made obvious on the settings page?
  • Is there any workaround besides setting Cache Lifespan to 0?
  • If not, would a script like this work as a workaround?
// Hook into WP Rocket's full cache clearing action
add_action( 'after_rocket_clean_domain', 'purge_cloudflare_cache' );

function purge_cloudflare_cache() {

		error_log( 'Cloudflare cache purge function triggered on live site.' );

		$apiKey = '<REDACTED>';
		$zoneId = '<REDACTED>';
		$url = "https://api.cloudflare.com/client/v4/zones/{$zoneId}/purge_cache";
		$data = json_encode(
			array(
				'purge_everything' => true,
			)
		);
		$headers = array(
			'Authorization: Bearer ' . $apiKey,
			'Content-Type: application/json',
		);
		$ch = curl_init( $url );

		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
		curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

		$response = curl_exec( $ch );


		curl_close( $ch );


		error_log( 'Cloudflare API Response: ' . $response );

		$responseData = json_decode( $response, true );
		if ( $responseData['success'] ) {
			error_log( 'Cloudflare cache purged successfully' );
		} else {
			error_log( 'Cloudflare cache purging failed: ' . $responseData['errors'][0]['message'] );
		}

}

alessandro-newzoo avatar Oct 11 '24 10:10 alessandro-newzoo

@alessandro-newzoo Sorry this has caused you a lot of trouble!

We may be able to provide a helper plugin for you that will trigger the APO cache to clear for pages cleared by the Cache Lifespan.

Please open a support ticket here: https://wp-rocket.me/support/

Ask that the ticket be transferred to Joseph and we can work on it together (normally, this can't be done, but since I've already been working on the helper plugin to do this, it makes sense in this case).

If we confirm the helper works for your case, then I'll post it here as well for anyone else with similar issues.

joejoe04 avatar Oct 11 '24 16:10 joejoe04

Related: https://secure.helpscout.net/conversation/2734724693/517877

joejoe04 avatar Oct 15 '24 19:10 joejoe04

Related: https://secure.helpscout.net/conversation/2872346810/545288

  • Home page cache would not clear when the Purge This URL option used on the Home Page specifically.

  • Temporary solution is to apply this snippet to functions.php file:

add_action( 'after_rocket_clean_home', function( $root, $lang ) {

// Clear Cloudflare APO cache for home page
function wpr_clear_cloudflare_apo_cache_home_page() {
  if ( defined( 'WP_ROCKET_VERSION' ) ) {
    $container = apply_filters( 'rocket_container', null );
    $subscriber = $container->get( 'cloudflare_plugin_subscriber' );
    $subscriber->purge_cloudflare_partial( array( get_home_url() ) );
  }
}
wpr_clear_cloudflare_apo_cache_home_page();

}, 9999, 2 );

joejoe04 avatar Mar 27 '25 15:03 joejoe04

Related: https://secure.helpscout.net/conversation/2893309903/549927

joejoe04 avatar Mar 31 '25 20:03 joejoe04

AC: Links Cloudflare APO cache clearing with WP Rocket cache clearing behavior

DahmaniAdame avatar Nov 14 '25 10:11 DahmaniAdame

Testing is blocked till we have APO license

Mai-Saad avatar Nov 18 '25 13:11 Mai-Saad

Related test plan https://wpmediaqa.testrail.io/index.php?/runs/view/1148&group_by=cases:section_id&group_order=asc&display=tree&group_id=7373

Mai-Saad avatar Nov 18 '25 14:11 Mai-Saad