wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
699 stars 218 forks source link

Extensive cache clearing when updating non-published products/CPTs #3770

Open vmanthos opened 3 years ago

vmanthos commented 3 years ago

Before submitting an issue please check that you’ve completed the following steps:

Describe the bug

When saving a non-published product, e.g. one that's "pending review", all cached files in the /cache/wp-rocket/example.com/product/ folder are cleared.

This is because the $permalink that get_rocket_sample_permalink() returns and "calculated" here: https://github.com/wp-media/wp-rocket/blob/1682b306bd63d68b691ec26861e463c5de0680e9/inc/functions/posts.php#L48

is like this: https://example.com/product

When that's passed to rocket_clean_files() here: https://github.com/wp-media/wp-rocket/blob/1682b306bd63d68b691ec26861e463c5de0680e9/inc/common/purge.php#L239

is causing the clearing of all those files.

Although I haven't tested this with other CPTs using such a permalink, I'm guessing the same will happen there too.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new product, and set its status to pending review.
  2. Update that.
  3. The cached files in the /example.com/product/ folder will be deleted.

Expected behavior

We should not extensively clear the cache.

Additional context

Related ticket: https://secure.helpscout.net/conversation/1457453338/248767

Although not directly related, get_rocket_sample_permalink() is involved in #3757.

A workaround is to use the WP Rocket | No Purge for Custom Post URLs to remove https://example.com/product from the array of URLs that are to be cleared.

Backlog Grooming (for WP Media dev team use only)

valmirselmani commented 2 years ago

When will this issue be fixed? In my case, when a CPT post is changed from "published" to "draft" or from "draft" to "pending review", the whole cache is purged not only the post from that specific CPT.

The problem is that my site has over 1000 pages and the whole cache is purged every hour because we have an API connected that updates some CPT posts every hour. I paid $249 in order to fix the performance issues, not make them worse.

Any idea for a workaround? This issue should be priority high not low.

NataliaDrause commented 2 years ago

Related: https://secure.helpscout.net/conversation/2029385423/373136/

In this case, the partial cache clearing purged the cache for the whole domain. This is not Product or CPT, just a regular post.

For the $purge_urls here we collected URLs like the following:

Array
(
    [0] => https://example.com/2022/10/11/post-one/
    [1] => https://example.com/?page_id=197
    [3] => https://example.com/2022/10/11/post-two/
...
    [6] => https://example.com/author/my-author/
)

URL https://example.com/?page_id=197 was a draft and caused the whole directory being purged.

Removing the draft (trash) resolved the issue.