cloudflare / Cloudflare-WordPress

A Cloudflare plugin for WordPress
https://www.cloudflare.com/wordpress/
BSD 3-Clause "New" or "Revised" License
208 stars 84 forks source link

Menu Save - Request Times Out After Repeated Cache Purges #498

Closed anthonyeden closed 7 months ago

anthonyeden commented 1 year ago

Confirmation

WordPress version

6.1

Cloudflare-WordPress version

4.11.0

PHP version

8.1

Expected result

When saving a Menu, I expect it to save quickly and possibly trigger a site-wide purge (see https://github.com/cloudflare/Cloudflare-WordPress/issues/478). However, instead, it is repeatedly calling purgeCacheByRelevantURLs for the pages contained in the menu, which results in the Menu page crashing/timing out if the menu is large.

I would expect Cloudflare to keep a log of URLs and/or Page IDs it has purged in any one request. Each time purgeCacheByRelevantURLs is called, it should check this list and abort early if it's already handled that page in that request. This could be as simple as an array in a global variable or object property.

With updating Menus, it triggers the hooks many times over with the same Page IDs, so many requests are sent to Cloudflare. This causes the page load to time out.

Actual result

Upon saving a large Menu, WordPress's request will timeout. This is because purgeCacheByRelevantURLs is being called dozens or hundreds of times by the WordPress hooks. Each time, it is making the same Cache Clear requests to Cloudflare over and over again.

Steps to reproduce

  1. Enable Cloudflare plugin with APO
  2. Setup a large menu (dozens of menu items)
  3. Save the Menu
  4. Wait for the timeout

If you run a debugger, you can see how many times purgeCacheByRelevantURLs is called in one request for saving a menu.

Additional factoids

No response

References

No response

anthonyeden commented 1 year ago

As a very quick and dirty workaround for the moment without modifying the plugin itself, I present this awful code:

add_filter('is_post_type_viewable', function($is_viewable, $post_type) {
// Prevent menu items from triggering a cache purge
// Workaround due to https://github.com/cloudflare/Cloudflare-WordPress/issues/498

// Only impact calls from the Appearance > Menu page.
if(!isset($_SERVER['SCRIPT_NAME']) || pathinfo($_SERVER["SCRIPT_FILENAME"], PATHINFO_BASENAME) != 'nav-menus.php') {
    return $is_viewable;
}

// If this is being called from Cloudflare, return $is_viewable=false, always on this page
$backtrace = debug_backtrace();
foreach($backtrace as $entry) {
    if(isset($entry['file']) && strpos($entry['file'], 'cloudflare') !== false) {
        return false;
    }
}

return $is_viewable;
}, 10, 2);
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

anthonyeden commented 1 year ago

I know this was closed as stale, but I'm just not certain if it has been dealt with? Can anyone comment?

github-actions[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.