Skip to content

Instantly share code, notes, and snippets.

@isaumya
Last active April 8, 2024 08:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save isaumya/d5990b036e0ed2ac55631995f862f4b8 to your computer and use it in GitHub Desktop.
Save isaumya/d5990b036e0ed2ac55631995f862f4b8 to your computer and use it in GitHub Desktop.
WP Rocket + Super Page Cache for Cloudflare : Disabling WP Rocket Page caching for it to work with Super Page Cache for Cloudflare

WP Rocket (No Page Cache) - Must Use Plugin - Installation Guide

When using Super Page Cache for Cloudflare along with WP Rocket plugin it is highly recommended that you do not use more than one page caching system on your site as that will lead to compatibility issues and problems.

Whether you have a server level page caching system or some other page caching plugin installed in yoru site, you need to ensure that all page caching mechanisms are disabled on those plugin before using the Super Page Cache for Cloudflare plugin on your website. Also, ensure that there is no server rule in your .htaccess file or nginx.conf file which is modifying the cache-control header value of the page as that value needs to be managed by the Super Page Cache for Cloudflare plugin.

Here is what you should do if you are uising WP Rocket along with Super Page Cache for Cloudflare to get the best results.

Step 1 - Make sure you have a clean slate (Disable all caching plugins)

First make sure that both WP Rocket and Super Page Cache for Cloudflare are disabled on your wensite and inside your wp-config.php file there is no WP_CACHE constant. If you see a statement like define( 'WP_CACHE', true ); or something similar inside your wp-config.php file, please delete that line and save your wp-config.php

Step 2 - Install the mu-plugin given below

After this README documentation you will file another file named wp-rocket-no-cache.php file attached to it. Make a copy of that file in your system either by downloading the file or creating a file with the same name and copy poasting the content given below.

After that open your FTP client and go to /wp-content/mu-plugins/ folder. Inside /wp-content/ if you do not see the mu-plugins folder, just create a new folder and name it mu-plugins. Then inside this folder put the mu-plugin code given below.

Finally, when you go inside yoru WP Admin > Plugins section, now you should see that a new Must Use Plugin section has been added and under which this WP Rocket - No Cache plugin is mentioned. This will ensure that you have done this step right.

Step 3 - Enable WP Rocket & Disable page caching

Now enable the WP Rocket Plugin and make sure page caching or mobile device caching is disabled inside this plugin. After enabling WP Rocket, open the FTP cleint again and open wp-config.php file. This time there should not be any mention of the WP_CACHE constant.

Similarly, when you would go inside the /wp-content/ folder, there should not be any advanced-cache.php file as well.

You can use WP Rocket to optimizing your static files like CSS/JS etc. or you may use other plugins dedicated to static file optimization like Autoptimize. For image optimization, there is Cloudflare Polish (comes with Cloudflare Pro plan and above), Imagekit, Optimole, Shortpixel, EWWW Image Optimizer etc.

But this gist is for those who must need to use WP Rocket.

Step 4 - Enable & Setup the Super Page Cache for Cloudflare

Now it is the time to enable the Super Page Cache for Cloudflare plugin, enter the Cloudflare API details and enable page cache. After this, everything should work fine without any major issues.

<?php
/**
* Plugin Name: WP Rocket | Disable Page Caching
* Description: Disables WP Rocket’s page cache while preserving other optimization features.
* Plugin URI: https://gist.github.com/isaumya/d5990b036e0ed2ac55631995f862f4b8#file-wp-rocket-no-cache-php
* Author: Saumya Majumder & WP Rocket Support Team
* Author URI: http://wp-rocket.me/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace WP_Rocket\Helpers\cache\no_cache;
// Standard plugin security, keep this line in place.
defined( 'ABSPATH' ) or die();
/**
* Disable page caching in WP Rocket.
*
* @link http://docs.wp-rocket.me/article/61-disable-page-caching
*/
add_filter( 'do_rocket_generate_caching_files', '__return_false' );
// Disable generating the advanced-cache.php file by WP Rocket
add_filter( 'rocket_generate_advanced_cache_file', '__return_false' );
// Disable WP Rocket Mandetory Cookies
add_filter( 'rocket_cache_mandatory_cookies', '__return_empty_array' );
// Prevent WP Rocket from changing the WP_CACHE constant
add_filter( 'rocket_set_wp_cache_constant', '__return_false' );
// Prevent WP Rocket from writing to the htaccess file
add_filter( 'rocket_disable_htaccess', '__return_true' );
// Disable other WP Rocket stuffs that are not needed and handelled by this plugin
add_filter( 'rocket_display_input_varnish_auto_purge', '__return_false' );
/**
* Cleans entire cache folder on activation.
*
* @author Arun Basil Lal
*/
function clean_wp_rocket_cache() {
if ( ! function_exists( 'rocket_clean_domain' ) ) {
return false;
}
// Purge entire WP Rocket cache.
rocket_clean_domain();
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\clean_wp_rocket_cache' );
@Paktas
Copy link

Paktas commented Nov 22, 2022

"make sure page caching or mobile device caching is disabled" - it's not entirely clear WHY this needs to be disabled. pregenerating HTML on server level make things faster. How is this functionality replaced by Super Page Cache for Cloudflare plugin?

@isaumya
Copy link
Author

isaumya commented Nov 23, 2022

@Paktas The Super Page Cache for Cloudflare plugin also has a Fallback Cache option for disk-level HTML cache. Have you checked out/used the plugin?

@fedeandri
Copy link

fedeandri commented Jan 9, 2023

@isaumya first of all, great job with your plugin (I see you're one of the official contributors), I just started experimenting with it and I have to admit that I'm really impressed.

But I have the same question that @Paktas has, why is it better to disable third parties page caching and only use Super Page Cache for Cloudflare page caching?

I'm currently using Super Page Cache for Cloudflare (with page caching off) on a landing page hosted on Siteground (with server side page caching on) and WP Rocket (with page caching on) and the results are outstanding and everything seems to work flawlessly so far.

Can you please explain which issues/problems could occur by using third parties page caching (leaving Super Page Cache for Cloudflare page caching off)? I'd like to better understand the reasoning behind this recommendation.

Thanks

@isaumya
Copy link
Author

isaumya commented Jan 9, 2023

Hi @fedeandri,
You see, Super Cache for Cloudflare is a Page Caching plugin. Now to explain your questions first you need to understand what page caching is. It simply means a system that caches the HTML pages generated by server-side languages like PHP at the disk level so that when another user request the same thing, instead of generating the page HTML again through PHP, instead the cached HTML will be returned to the user.

So, this is why it is crucial to ensure you have only one page caching system in place for any site. So that none of them conflict with one another. You can definitely use a separate plugin for static like optimization if you want to cause that has nothing to do with page caching.

Now you can have page caching in multiple ways. You can have WordPress plugins that cache the page at the disk level, you can also have server-side tools like fast-CGI cache or varnish cache which does the same thing at the server level without the help of WordPress system or PHP.

Now when you have multiple page caching systems it creates a wired loop of confusion. Also not to mention each of these page caching systems does things in a different way. So, when you have a multiple page caching system in place, whichever wins the war of handling the request will return the response with the cached HTML along with whatever Response Header it sets. This Response Header is a crucial thing in terms of both disk-level page caching and CDN-level page caching.

Imagine you in a multiple-page caching system in place. At this time when a request comes and the server starts processing the request, at this time the server might not know what to do, i.e. whether to process the request via PHP or checked the cached HTML and return that. At this time, imagine a war is going on between all the page caching systems you have in place to see who will get to handle/process the request.

So, at this time whoever of them wins will get to cache/process the request. Also not to mention this creates a nightmare of cache purging as god knows who gets to cache what, at what point in time. Moreover, most importantly depending on which caching system is handling the request the Response Header will be set accordingly. And without the proper response header that is set by the plugin, Cloudflare caching will not work properly and you will have issues caching/purging cache.

In the end, Super Page Cache for Cloudflare comes with a multi-tier caching system. First, it caches pages in Cloudflare global CDN and along with that it also has a Fallback Cache which is a traditional disk cache there as a fallback for the time when the cache is expired in Cloudflare CDN, instead of executing PHP to generate the page, the page HTML can be served from the disk level fallback cache.

So, I hope now it is clear to everyone why it is important to have only one page caching system on any website. You can have any page caching system you like, but try to keep only one page caching system. Otherwise, things will conflict, cause issues, and will waste server resources.


I'm currently using Super Page Cache for Cloudflare (with page caching off) on a landing page hosted on Siteground (with server side page caching on) and WP Rocket (with page caching on)

This is a devastating combination. Firstly I don't know how anyone can use the Super Page Cache for Cloudflare plugin with the page caching off as it is a literal page caching plugin which you cannot turn off. It just comes with two flavors of page caching. First is the Cloudflare global CDN page caching which is enabled by default and then secondly the traditional fallback cache which is disabled by default and users need to manually enable it.

You can use any combination you want, but it is not something that I will recommend. If I was in your place, here's what I would have done:

  1. Disabled Siteground Caching
  2. Disabled page caching in WP Rocket and either use it just for static files optimization or just not use WP Rocket at all and instead use something like Perfmatters which is not a caching plugin but rather optimization plugin including static file optimization.

But again at the end of the day you can do whatever that pleases you.

@fedeandri
Copy link

@isaumya thanks for the detailed explanation and sorry for the super late reply.

I'm far from being an expert, but I'm familiar with how caching layers work and what are the risks of having too many caching systems in place at once. The piece of the puzzle I was missing was the importance of the Response Header when you want to make sure that your pages get cached correctly on Cloudflare through Super Page Cache for Cloudflare.

Firstly I don't know how anyone can use the Super Page Cache for Cloudflare plugin with the page caching off as it is a literal page caching plugin which you cannot turn off.

Yeah, sorry for not being very clear, I was referring to the fallback disk cache.

I'll follow your recommendation and test the results, but I have two last questions:

  • Did you explicitly test WP Rocket (with page cache enabled) + Super Page Cache for Cloudflare (with fallback disk cache off) and pages started not being cached correctly on Cloudflare? If yes, can you please make an example of a common scenario where this can happen?

  • Or you're just recommending to disable WP Rocket page cache and only use Super Page Cache for Cloudflare (with fallback disk cache on) in order to prevent possible problems (even if they didn't occur or no one detected them so far)?

@isaumya
Copy link
Author

isaumya commented Jan 27, 2023

Hi @fedeandri,
First of all when using Super Page Cache for Cloudflare plugin, the usage of fallback cache is optional because when pages are cached at Cloudflare CDN level, the response will be served from the Cloudflare CDN edge nearest to you, the request won't even reach the origin server. Now in situations where the cache has expired from CDN or hasn't been cached yet, only in those cases, the requests will come to the origin server. At that point, if the fallback cache is disabled, then the PHP will be used to generate the page HTML content which will then further be cached at Cloudflare CDN. Alternatively, if the fallback cache is enabled, the system will look if the page being requested if that page is already cached in the fallback cache, if so then serve that cached HTML from the disk-level fallback cache or use PHP to generate the page content.

Now if you have enabled page caching inside WP Rocket and disabled the fallback cache in Super Page Cache for Cloudflare plugin, WP Rocket's code will overwrite the page cache and most importantly page response headers. So, Super Page Cache for Cloudflare plugin will not have any control over the page response header which will cause issues of the page being properly cached at Cloudflare level as WP Rocket will be overwriting the page response headers. So, you need to make sure that WP Rocket does not handle the page caching operation in any way, it may do static file optimization if you would like to use it, but not page caching. That should always be handled by Super Page Cache for Cloudflare only.

If you would like to have a disk-level fallback cache on your website, you can definitely use the fallback cache feature inside Super Page Cache for Cloudflare plugin, But if your site has good traffic and you think pages are staying cached for a long period of time in the CDN, you can disable that as well. It's totally up to you.

@fedeandri
Copy link

Thanks @isaumya I'm familiar with everything you described, but that doesn't answer any of my previous questions.

I'll reword them here again, trying to be more clear:
did you ever test WP Rocket (with page cache enabled) + Super Page Cache for Cloudflare (with fallback disk cache off) to see if the combo works?

No need for long explanations, a simple
"yes I've tested it and the combo works/doesn't work"
or
"no, I've never tested it, so I don't know for sure if it works or not"
would be greatly appreciated.

Hope to not sound too rude (I apologize in advance if that's the case), I'm only trying to understand if there's a real and documented technical reason why we should disable WP Rocket page cache (and only use Super Page Cache for Cloudflare fallback disk cache) or it's just a recommendation to avoid possible (but never detected so far) problems.

Thanks

@isaumya
Copy link
Author

isaumya commented Jan 27, 2023

Hi @fedeandri,

did you ever test WP Rocket (with page cache enabled) + Super Page Cache for Cloudflare (with fallback disk cache off) to see if the combo works?

No it will not work as I have explained above. Please check my reply above for the detailed explanation.

I'm only trying to understand if there's a real and documented technical reason why we should disable WP Rocket page cache

Check my response above related to the response header to understand why it will not work.

But in the end, if my words are not satisfying to you, feel free to run your own experiments.

@fedeandri
Copy link

@isaumya got it, so you tested the combo and you saw that WP Rocket sends the wrong headers, thanks for clarifying that.

I'll try to run my own experiments, just to understand more about the whole process, but I'll follow your recommendation.

Thanks again

@zavants
Copy link

zavants commented Mar 21, 2023

The plugin works well but I found huge disadvantages in the page cache of Super Cache vs WP Rocket for example:

On a site with more than 4000 pages Super Cache hangs the page until the cache finishes, Super Cache has problems to manage the site resources thing on the other hand WP Rocket may have some delays but it never leaves a site offline with timeout.

It also has incompatibilities with some templates, something that WP Rocket has worked and solved very well over the years.

I consider that the ideal configuration would be Wp Rocket Cache (where I consider that in this case is superior) and Super Cache for the CDN cache (where this is unbeatable).

@Odene
Copy link

Odene commented Feb 12, 2024

@isaumya do you have by chance any tutorial on how to set up the Super Page Cache for Cloudflare? What do you have on and off? I struggle with my listing page Placówkowo which is mostly DYNAMIC.

I had the best results with wp-rocket + cloud flare but I did not use the Super Page Cache for Cloudflare, only the wp-rocket (and using the wp-rocket caching).

I also tried litespeed, that was a desaster..

@isaumya
Copy link
Author

isaumya commented Feb 15, 2024

Hi, if you check the plugin description page there is a video there which someone has made showing how to use the plugin.

@lyassinel
Copy link

Hello,

on wprocket do i need to desactived the option for Cloudflare ?

Best regards

@isaumya
Copy link
Author

isaumya commented Apr 8, 2024

Yes you do @lyassinel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment