Quick homepage optimization

On our Drupal sites, we always override page-front.tpl.php and go about our merry way customizing the home page. Not once thinking about or using the $content variable that is available. Well, it bit us in the ass pretty hard and it just might be slowing down your sites as a result.

If you just implement page-front.tpl.php but don’t change the Default Front Page it will, um, default, to node. That might not seem like such a big deal, besides, all that is getting output is that largely unhelpful intro text you’ve seen when you create a new site. Well, no so fast.

Frank Febbraro, Chief Technology Officer
#Drupal | Posted

On our Drupal sites, we always override page-front.tpl.php and go about our merry way customizing the home page. Not once thinking about or using the $content variable that is available. Well, it bit us in the ass pretty hard and it just might be slowing down your sites as a result.

If you just implement page-front.tpl.php but don’t change the Default Front Page it will, um, default, to node. That might not seem like such a big deal, besides, all that is getting output is that largely unhelpful intro text you’ve seen when you create a new site. Well, no so fast. If any content in your site is Promoted to front page, then that little innocuous node path calls node_page_default which starts rendering a list of nodes and even calls, gasp!, the node_load function. Now, if the rest of the defaults are also in place, namely Number of posts on main page, it will query 10 nodes and node_load all 10 in the best case scenario. What made matters worse for us is that there was a slight bug in the node-[type].tpl.php file and content that was not even going to be displayed on the home page was loaded, rendered, caused an error, and crashed the homepage. Egads!!!

Since there is value in using Promoted to front page even if you are not using the default node path, it does not make sense to just remove that setting from your nodes to make sure nothing appears in $content. It still performs a query to, at best, return no rows.

The fix for us was pretty easy. We added a menu path specifically for the homepage that returns an empty string to guarantee that no queries, node_loads, or extra theme related things are invoked. It looks like this:

  1. function example_menu() {
  2. $items = array();
  3. $items['homepage/empty'] = array(
  4. 'page callback' => '_example_homepage_empty',
  5. 'access arguments' => array('access content'),
  6. 'type' => MENU_CALLBACK,
  7. );
  8. return $items;
  9. }
  10.  
  11. function _example_homepage_empty() {
  12. return '';
  13. }

Then, just make sure you change your Default Front Page to homepage/empty and you are good to go.

Bonus tip, after you implement this in your module when you roll out your new release if you want to enable the new homepage automatically you can do it with an update hook in your .install file like so:

  1. function example_update_6001() {
  2. variable_set('site_frontpage', 'homepage/empty');
  3. }

BTW

, this same idea is one of the reasons that the Context module is so great. Without using Context, Drupal will render block content for all of your regions and blocks (depending on visibility) even if you have no intention of ever displaying that region on the page. Context will only render the blocks you tell it to for a particular condition.

Anyone else have other tips to optimize the Drupal homepage?

Frank Febbraro

Chief Technology Officer