WP2Static is a great plugin that lets us convert our WordPress site into a static one. Sometimes though, we would like to shorten the generation time. In this article, I will show what are the best ways to do so.
How WP2Static works
Before we dive into speeding up the whole process, let’s analyze how WP2Static works. The whole process can be divided into 3 parts:
- URL detection – when the plugin tries to find all the website’s URLs
- Crawling – when we download and save the HTML of each page
- Post-processing – when we update the content of each page (URL updates for example)
The cool part is, that we don’t have to launch every process each time, which is useful information because if only did come changes in our content we don’t have to detect the URLs again.
Crawl only things you need to
The best way to improve the speed is to remove the pages that we don’t need to crawl. There are a few filters that might be useful in this case:
wp2static_detect_parent_theme wp2static_detect_child_theme wp2static_detect_plugin_assets wp2static_detect_wpinc_assets wp2static_detect_vendor_cache wp2static_detect_posts_pagination wp2static_detect_archives wp2static_detect_categories wp2static_detect_category_pagination wp2static_modify_initial_crawl_list
Don’t worry, you don’t need to use all of them 🙂
Anyway, using those can make your crawl list a lot smaller.
For example – WP2Static crawls through 292 pages on WP Owls. If I would remove all the filters I use, it would crawl through 3483 pages. Why there is such a big difference? Mostly because of tags, categories (I don’t use archives for those taxonomies), and images.
Let’s look a the time difference:
- with filters I needed enabled, it took ~111 seconds
- without, it took over 20 minutes
Use server operations when possible
As I mentioned before – I don’t crawl through images. This makes my crawl list a lot smaller. Instead, the quickest way is to just copy or rsync the
wp-content/uploads folder. Overall, whenever possible – use server operations if you can.
Use the latest PHP version
Using an older PHP version may negatively affect the crawling speed. I did a test on my server and these are the results:
As you can see – using PHP 7.3 resulted in a bit longer crawl times, while 7.4 and 8.0 went faster.
Run crawler concurrently
Since this commit, you can run crawls concurrently. This feature can give us about 25% time improvement if we’ll set a proper number of concurrent crawls. Setting this number too high might lead to a slower crawl rate or errors.
Take a look at how the static generation would differ on WP Owls hosting:
Buy a better server
If the previous steps didn’t help or your website has grown a lot, you can always buy a better server. Having a faster server will lead to two things:
- crawling each page will take less time
- you can run more crawls concurrently
Time to sum up
Those are the methods I use to speed up generating static websites. We went from 20 minutes to 111 seconds by crawling fewer files and ending with 90 seconds running crawls concurrently.
As you can see – it wasn’t too difficult. It requires some trial and error, but the effect can be really amazing.