January 18, 2020
Server Caching Madness
January 18, 2020
I recently set up a new account on Bluehost to set up another website using my simple framework on WordPress. Simple is developing into something far more powerful than I originally intended.
This site is grafted onto an account that is over 10 years old now. After working with the new account a while, I began to see differences between the old and the new. Specifically, the new account was subject to server caching of php pages. This can be quite maddening for a developer, as your changes just don't show up, at least not until the cache gets stale and refreshes.
First, I found that Bluehost had sneakily installed a caching plugin in WordPress. Look for anything in the mu-plugins folder and delete it, unless you put it there yourself. But that didn't fix the problem.
I spent a long time 'chatting' with Bluehost's tech support about this. They were fairly knowledgeable ( so am I ) and very skilled at dancing around the subject without actually lying. They claimed to do something, but nothing changed, and they basically would not admit that there was any server caching, and wasted a lot of my time on irrelevancies. But they did accidentally drop one useful bit of information.
There is a nocache directive that can be added to any request that will turn off the server caching and give you a fresh copy of what you are requesting. No doubt intended exactly for developing. Just add ?nocache to the end of any url; &nocache if there are already parameters. For example:
https://luthierylabs.com/?nocache
Now this site doesn't actually have the caching problem, it is grandfathered-out of caching. If they turned on caching on existing accounts, all kinds of things would break, and they would have a lot of irate customers. Also, caching is disabled when you are logged-in to the WordPress dashboard, otherwise it wouldn't work at all. I figured this all out while chatting with tech support, so it wasn't a complete waste of time. I also figured out three ways to defeat server caching:
The first I have already described - just add ?nocache to your url. You can also do this in html links. I was linking to a particular php file who's output was assembled from other files. Although the php file didn't change, the source files did, but since the server cached the output based on the date of the php file, your changes were lost until the next cache flush. Doing this in any html link will cure the problem, but just for that one link.
To make this happen for all file requests, add the following line to the end of your .htaccess file:
RewriteRule (.+) $1?nocache [QSA]
.htaccess is a plain text file typically found in the root of your account's file system, you can edit it through ftp with any text editor. It controls how requests are handled prior to handing them off to the server. The syntax is incredibly arcane, but all you need to do is paste this line on at the end of the file. The new rule simply appends the nocache directive to every request. Goodbye caching!
Finally, a sneaky way to solve the problem for a single file is to simply change its date on every access. This is easily done on a Unix/Linux system by incorporating the following line in your WordPress theme's functions.php:
touch( getcwd() . "/path/file" );
touch is a *nix utility that will change the file date. getcwd() returns your current working directory, the root of your account. Change path to the path to your file, and file to the filename.
So there you have three ways to defeat server caching. I am using the second one, as it defeats ALL server caching for all files and requests all the time. Much like my fix for Gutenberg's tool tips, I could automate this if I find someone has undone my change, but I doubt that will be necessary.
I should mention, all of this assumes apache running on Linux.
I still recommend Bluehost as the best host out there. However, the truth is that most web hosts, including Bluehost, were bought up by Endurance International Group (EIG). This includes HostGator, but not GoDaddy. All the companies that were bought-up were converted to Bluehost's model, so they are all exactly the same except for the name, and any of them would be just as good and just as bad. The only major alternative left - GoDaddy hosting - is awful, especially for WordPress or any other kind of active site. Well, that was the case years ago, maybe they got better, I don't know, and I'm not spending any of my money to find out.