Creating a static website with Modx

Given that title, you may be thinking “hang on, if it’s a CMS then it’s not going to be static pages, right?” – wrong.

Along with one the co-founder and lead Architect of Modx – Jason Coward, for my book review site SFBook, I’ve managed to get the best of both worlds – a static site that can be edited in the Modx CMS.

Why bother? – No matter how great your web server is, no matter how big the processor, memory or how fast the seek time the fact is that reading from a Mysql (or other non-flat database) takes time. The bigger or more complex the database and the query, the longer the time it takes to get the information from the database.

That’s where caching comes in. Caching saves the result of that database query to a physical file so that if that same query is run again within the lifetime of the cache, the file is retrieved instead. This saves a huge amount of time.

Some form of caching is pretty much vital for any production website that runs on a database and has traffic. The busier or more complex the site, the more important it becomes.

What Jason did was take this one step further, so rather than simply cache elements of a page or stored in a random cache folder he created a plugin that caches the whole page intact and with the appropriate page name in a folder on the server. After that it’s simply a case of getting the server to look in that folder first and if the page exists there load that and don’t even touch the database. This saves a huge amount of time and server resources.

Adding a hook into the modx “clear cache” and “clear on save” means that if you edit the page in modx, saving the page over-writes the copy in the folder resulting in an up-to-date version of the page always being stored.

Clearing the cache will delete the contents of the folder so the website will generate a new version of a page the next time it loads.

Now in theory this could mean that the first visitor would face a longer page load however this is where another usefull Modx Snippet comes in. Refresh Cache by Bob Ray will go through the site as if a visitor was doing so which saves a new copy to the “static” folder.

When you combine this method with using an Nginx server the result is one of the fastest and traffic-proof sites you can possibly imagine and a site that makes the best of both worlds – static pages that are edited through a database driven CMS.