Created
August 9, 2023 04:31
-
-
Save thegulshankumar/4964818ab641732ef86e890329307163 to your computer and use it in GitHub Desktop.
Bypass CDN Caching using functions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Exclude this opening tag. | |
/** | |
* The template_redirect action hook allows pre-template actions. | |
* We set s-maxage to 0 to ensure CDNs fetch fresh content, | |
* assuming the CDN respects existing headers. | |
*/ | |
add_action('template_redirect', function () { | |
header('cache-control: s-maxage=0'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use Case
Imagine managing two WordPress sites:
example.com
andexample.com/wp/
, both are hosted on Cloudways with the Cloudflare Enterprise Addon.However, an issue arises with Breeze caching. Breeze fails to purge Cloudflare's cache for the WordPress installation in the subdirectory
example.com/wp/
.[1] Additionally, there's no direct way to deactivate the Cloudflare HTML cache specifically for the subdirectory.On a positive note, Cloudflare does respect existing headers and allows an opportunity to modify the caching behaviour using the s-maxage directive. By implementing this workaround, we can bypass the Cloudflare Cache for the HTML while continuing to benefit from a Page Caching plugin such as Breeze.
Reference