Skip to content

Instantly share code, notes, and snippets.

@isaumya
Last active April 19, 2024 09:11
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save isaumya/af10e4855ac83156cc210b7148135fa2 to your computer and use it in GitHub Desktop.
Save isaumya/af10e4855ac83156cc210b7148135fa2 to your computer and use it in GitHub Desktop.
Super Page Cache for Cloudflare — Guide for using Remove Cache Buster Query Parameter feature (when using Cache Everything page rule)

Implementation Guide for using "Remove Cache Buster Query Parameter" feature

The Super Page Cache for Cloudflare plugin has recently added the feature for using the Cache Everything pagerule withing the ?swcfpc=1 cache buster query paramater. This opens up so many new doors where users previously had to use the Cloudflare Workers to remove the cache buster.

With this new option now users are able to take advantage of Cloudflare Cache Everything page rule and take it to the next level by using the new Rulesets released by Cloudflare. Basically this is achived by taking advantage of the all new Cache Rules feature implemented by Cloudflare.


Setp 1 — Setting up the Cache Rules inside your Cloudflare Dashboard

The first thing that you need to do is, log-in to your Cloudflare Dahsbord and go to the domain/zone doe which you are setting up the Super Page Cache for Cloudflare plugin. Then from the left hand side menus, go to Caching > Cache Rules.

Cache Rules Section

Then inside the Cache Rules page, click on the Create Cache Rule button. Then finally on the Create new Cache Rule page, click on the Edit Expression link.

Edit Expression Link

Now it's time to create all the Cache Rules we need... So, let's get started...

Most Important Note
You CANNOT use the cache rule mentioned below along with the Cache Everything page rule. Bedore adding this Cache Rule in your Cloudflare dashbaord, please make sure you have DISABLED the Cache Everything Page Rule automatically added by the plugin inside your Cloudflare dashboard > Rules > Page Rules section.
This one Cache Rule cacn handle everything and you do not need any other page rules.

Super Important Note Badge
Make sure you replace example.com with your actual website hostname e.g. something.com or www.something.com etc. in the code given below before copy/pasting it.

SPCFC Cache Rule ➜ Cache Eligible Requests

Rule Name: Add the following in the Rule Name section SPCFC Cache Rule ➜ Cache Eligible Requests

Rule Expression: Add the following expression inside the expression builder section.

(http.host eq "example.com" and not starts_with(http.request.uri.path, "/wp-admin") and not starts_with(http.request.uri.path, "/wp-login") and not starts_with(http.request.uri.path, "/wp-json/") and not starts_with(http.request.uri.path, "/wc-api/") and not starts_with(http.request.uri.path, "/edd-api/") and not starts_with(http.request.uri.path, "/mepr/") and not http.request.uri.path contains "/register/" and not http.request.uri.path contains "/dashboard/" and not http.request.uri.path contains "/members-area/" and not http.request.uri.path contains "/wishlist-member/" and not http.request.uri.path contains "phs_downloads-mbr" and not http.request.uri.path contains "/checkout/" and not http.request.uri.path contains ".xsl" and not http.request.uri.path contains ".xml" and not http.request.uri.path contains ".php" and not starts_with(http.request.uri.query, "s=") and not starts_with(http.request.uri.query, "p=") and not http.request.uri.query contains "nocache" and not http.request.uri.query contains "nowprocket" and not http.cookie contains "wordpress_logged_in_" and http.cookie ne "comment_" and not http.cookie contains "woocommerce_" and not http.cookie contains "wordpressuser_" and not http.cookie contains "wordpresspass_" and not http.cookie contains "wordpress_sec_" and not http.cookie contains "yith_wcwl_products" and not http.cookie contains "edd_items_in_cart" and not http.cookie contains "it_exchange_session_" and not http.cookie contains "comment_author" and not http.cookie contains "dshack_level" and not http.cookie contains "auth_" and not http.cookie contains "noaffiliate_" and not http.cookie contains "mp_session" and not http.cookie contains "xf_" and not http.cookie contains "mp_globalcart_" and not http.cookie contains "wp-resetpass-" and not http.cookie contains "upsell_customer" and not http.cookie contains "wlmapi" and not http.cookie contains "wishlist_reg")

Cache Status: Set it to Eligible for cache.

Cache Key:

In this section you can enable the following options:

  • Cache deception armor (Always Enable This Option)
  • Ignore query string (Enabling This Option Might Break Your Site — So, Test Thoroughly Before Enabling) ⚠️
  • Enable query string sort (Do Not Enable This Option if you are Using WooCommerce, Easy Digital Download or any Membership platform) ⚠️

Finally the cache rule should look something like this ☟ Cache Eligible Requests & Ignore Query Params from cacheKey Rule Screenshot

Very Important Note Badge
Once you add the Ignore query string option inside the cache rule, Cloudflare will simply ignore the unnecessary query strings (except for the one excluded inside the rule) when checking the cached contents of your website.
If you check the above cache rule, you will see that the s, p, nospcfc, nowprocket query params which are commonly used in WordPress websites for search and other functionalities have already been whitelisted so that Cloudflare system does not ignore that query parameter.
So, if you provide an URL e.g. https://example.com/some-page/?s=some+search+phrase in this case Cloudflare system will NOT ignore the s query param so that your search functionality works properly.
If you have more query params on which your website is dependent on and you do not want Cloudflare to ignore them, then you can check the above screenshot and add new query params below the whitelisted query params to whitelist them exactly the same way I have whitelisted those query params.
Alternatively if you enter https://example.com/some-page/?fbcid=123&foo=bar&something=test, Cloudflare will simply ignote the ?fbcid=123&foo=bar&something=test part and check if https://example.com/some-page/ is cached, if so, return the cached content.
This will help increasing the Cache HIT ratio of your website as most unnecesarry and marketing query params will be ignored by Cloudflare.

Once you have added this Cache Rule and disabled the Cache Everything Page Rule in your Cloudflare dashboard, you can proceed to the Step 2 below.

Step 2 — Enabling Remove Cache Buster Query Parameter option in the plugin settings & Purge the whole Cloudflare Cache

Inside the Super Page Cache for Cloudflare plugin settings, go to the Other tab and scroll down. You will see an option named Remove Cache Buster Query Parameter.

plugin settings to enable

Enable that option and save the plugin settings. Then all you need to do is to make sure that you have force purged everything from the Cloudflare Cache.

That's it. Now you have the Cloudflare CDN Level Page Caching with the Cache Everything Page Rule without any cache buster query parameter (e.g. ?swcfpc=1). Also if you have added the fourth Cache Rule then your cache HIT ratio will also increase dramatically as Cloudflare will now ignore the query strings when checking if the URL is already cached. Enjoy... 🥂🍾🥳🎉

P.S.: Make sure you have enabled Smart Tiered Cache inside your Cloudflare Dashboard for the highest cache HIT ratio.

@TheKidRock
Copy link

Hi @isaumya I followed the method above. However, there is no "Remove Cache Buster Query Parameter" option available. Or is this option not available since I have enabled "Worker mode"?
For a blog site, which is better - page caching or worker mode? I had read somewhere (can't remember exactly where) that worker mode is better than page caching that's why I enabled worker mode.

@isaumya
Copy link
Author

isaumya commented Dec 5, 2023

Hi @TheKidRock, the "Remove Cache Buster Query Parameter" option will only be shown when you have disabled the worker mode.

@GwynethLlewelyn
Copy link

Thanks for posting this; I know it's being linked from the backoffice of your fantastic plugin, but I never clicked on it, because, frankly, I didn't really quite knew what exactly to expect!

In fact, I'd say that the removing cache buster option is the least important of all the remaining information — namely, how to abandon the deprecated (but oh so useful!) Page Rules and use Cache Rules instead (as recommended by Cloudflare), and continue to have everything working smoothly!

Also, I have to commend Cloudflare on that — I had no idea that Cache Rules could get so complex and deal with so many different things at the same time. I mean — with one rule, you can do an insane amount of things at the same time, and it still only counts as just one rule. They're being very generous — and showing off their 'new' rule engine, I suppose...

@isaumya
Copy link
Author

isaumya commented Dec 19, 2023

Yes, @GwynethLlewelyn Cache Rule is super powerful, especially for advanced users.

@JJAsh1
Copy link

JJAsh1 commented Dec 20, 2023

Interesting. Thanks a lot for this.

Two questions :

  1. Is it add_items_to-cart instead of edd_items_to-cart ?

  2. Won't the "woocommerce_" cookie check only serve uncached page to all Woocommerce clients with an item in the cart ?

@isaumya
Copy link
Author

isaumya commented Dec 20, 2023

Hi @JJAsh1,
I don't know what your first question is, you can change the cookie name if you feel like it. For the second question, yes when a user has item in cart it will serve pages from the server instead of the cache which is needed as most sites show dynamic data when items are added to cart like the cart item, count of items in cart etc.

@Dawiducik
Copy link

Hello @isaumya, great work!
I have set this configuration up, went to my-account page and the plugins seems to do its job, however, how does it know that it should still cache css/js and "bypass" html when user is logged in? I mean, the browser sends cookies in request, regardless if its css, html or php call, so they all should be bypassed from cache, because they have a "wordpress_logged_in" (the one in cf cache rule) cookie right? So why is it still served and I have Cf-Cache: HIT header? Thank you!

@isaumya
Copy link
Author

isaumya commented Mar 22, 2024

Hi @Dawiducik,
Yes CSS/JS should be served from cache. Please try and check if it's working as expected.

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