TechCrunch recently underwent a pretty massive redesign, running on WordPress VIP. One of the biggest priorities for the development team was increasing performance. And not just a little, a lot. One of the tools that was integral to their giant leap in performance was the ability to run non-essential tasks to load asynchronously, meaning time consuming processes can run in the background while a page loads quickly for users. Now, the TechCrunch team has open sourced this asynchronous library so you can start using it on your own site.
What’s A Non-Essential Task
So what kind of things should run asynchronously. Basically, you can queue things that are not vital to a user’s experience. Related content at the bottom of the page is a great example of this. You don’t need to load this right away. If the information isn’t already cached from a previous request, you can simply send a task off in the background to go and gather that data so it is available to future requests without blocking content.
There’s probably all sorts of components like this on your average page, and many of them can be loaded in after the page has been completely loaded. If you run the task asynchronously, you can simply pull the content in asynchronously client side once content has been rendered.
How’s It Work?
Though TechCrunch has made documentation available on GitHub, it is still a bit confusing. That’s probably because it is a bit out of my league, but here are the basic principles.
WP Asynchronous Tasks works by allowing you to extend any WordPress action (and there are lots to choose from) to run asynchronously in a non-blocking matter.
The first step is to extend the WP_Async_Task class. Then you pass it the name of the action, then prepare the data. This means that you have to pass ID’s and the like to the task in a meaningful way, hopefully returning an array of helpful data for the task. Next you define the run_action function that is performed when an action is run, similar to how you set up a basic WordPress action.
After this is set up you can pass the custom async action using add_action and the task will be run asynchronously in the background instead of blocking page load. There are of course more details to implementation, but that’s how it’s all set up.
To me, this seems like any incredibly useful tool that is often difficult to accomplish in WordPress without using something like Cron events. If you’re a developer concerned with performance check it out. It’s still early days for the plugin so hopefully we’ll get a few more robust examples soon.
What do you think? Could you see yourself using WP Asynchronous Tasks?
Jay Hoffmann is a WordPress developer hailing from NYC. In the strictest sense of the word, he is a WordPress enthusiast with an eye for front-end development and design. He has been working with WordPress since 2006 and currently works for a popular children’s media company. This year, Jay started Tidy Repo, a curated list of the best and most reliable plugins from around the web. You can also follow Jay on Twitter.