Fixing SiteGround Dynamic Cache Issues
That's what you get for not using WordPress…
Published on
-/- lines long
If your website is hosted on SiteGround and you have strange issues, such as clicking on a button and nothing happening, it could be a cache issue. By default, SiteGround caches all requests, as they've said in their help center article(opens in new tab):
Our Dynamic caching is a full-page caching mechanism powered by NGINX that's enabled and running by default on all SiteGround servers.
The problem is that everything is cached, including the raw HTML of your pages and AJAX requests made by either your front-end, admin panel, or whatever. There also appears to be no way to simply turn off this functionality.
In my case, I'm using Kirby CMS and this aggressive caching means that if I change the status of a blog post from Draft to Public, it actually happens at a filesystem level, but since the HTML of the blog is cached, the article doesn't appear. Because AJAX requests are also cached, the Kirby panel front-end isn't updated as well, leaving no indication that something actually happened.
SiteGround has built-in exceptions for the cache. For example, if a visitor has the wordpress_logged_in_
or Drupal.visitor.name
cookie, they'll bypass the cache and see all changes they make immediately. However, if you're using something other than WordPress or Drupal, like me, you're screwed.
Luckily, SiteGround respects Cache-Control
headers(opens in new tab) that your server sends. You can turn off the cache entirely by adding the following in your main index.php
file:
header('Cache-Control: no-cache');
Alternatively, in case of using Kirby, you could disable it for just the panel by matching the request path with regex(opens in new tab):
if (preg_match('~^/(panel|api)(/|\?|$)~', $_SERVER['REQUEST_URI'])) {
header('Cache-Control: no-cache');
}
You can check if this works by inspecting the response headers(opens in new tab) and looking for the x-proxy-cache
header. It tells whether the response was served by the cache or not. If you see the following, the response was not cached:
x-proxy-cache: MISS
Handling Cloudflare Cache
Things can be even more confusing if you use Cloudflare, due to its own caching functionality(opens in new tab). It has to be enabled explicitly and only caches static files by default, so it has less unpleasant surprises. In my opinion, it doesn't make much sense to have both caches enabled, so you should probably disable one or the other. You can determine the Cloudflare cache status by looking at the cf-cache-status
header.
The response is cached by Cloudflare if you have these headers:
cf-cache-status: HIT
x-proxy-cache: MISS
…and you get these headers if it's cached by SiteGround instead:
cf-cache-status: DYNAMIC
x-proxy-cache: HIT