There is a fine tutorial over on WP-Rocket’s blog on how to implement WP Rocket with a Bedrock/Trellis setup. You can find this tutorial here https://wp-rocket.me/blog/how-to-set-up-wp-rocket-on-trellis-and-bedrock/

However, there are a few missing things in this tutorial. I figured out that caching didn’t work as expected after deploying and sometimes didn’t work at all. Here, I will try to fix these issues.

First, go ahead and follow the above tutorial. Then continue with the below steps:

Recreate advanced-cache.php and clear cache on deploy

WP Rocket saves its settings in a file named advanced-cache.php. It is automatically regenerated each time you change your WP Rocket settings.

You need to regenerate this file on each deploy as WP Rocket finds the version path. Not the current path, so configuration lines in this file will look like this:

This is of course not very good (as 20200507181234 will change for each deploy).  So we need to update this file each time we deploy in order to update to the new release path.

Furthermore, we need to clear the cache upon deploy as all links to generated assets like stylesheets and JavaScripts will be updated and the contents of WP Rocket’s cached files are invalid. Here is how to do this:

In the folder site/web create a file named wp-rocket-cache-helper.php  and insert this content into the file:

This will first regenerate the advanced-cache.php  file and after that clear the cache

Now, we need to make this file execute on deploy.

Go to trellis/deploy-hooks, create a file named finalize-after.yml, and insert the following:

This will use the command line PHP in the server you are deploying to, to call the functions in the wp-rocket-cache-helper.php file.

Now, we need to make sure that finalize-after.yml is executed when we deploy. In order to do so navigate to the folder trellis/roles/deploy/defaults and open the file main.yml

Here, you should find the line that says deploy_finalize_after  and alter it so it looks like this:

You should now commit and push your changes to your repository and deploy your changes as you would do normally.

Take it further

You can take it even further. Say you want to start preloading automatically after deploying, you just add that function to wp-rocket-cache-helper.php. See more in this article: https://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job

Leave a Reply

Your email address will not be published. Required fields are marked *