Quick homepage optimization

Posted Dec 7, 2009 // 1 comments
Frank:

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:

<?php
function example_menu() {
 
$items = array();
 
$items['homepage/empty'] = array(
   
'page callback' => '_example_homepage_empty',
   
'access arguments' => array('access content'),
   
'type' => MENU_CALLBACK,
  );
  return
$items;
}

function
_example_homepage_empty() {
  return
'';
}
?>

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:

<?php
function example_update_6001() {
 
variable_set('site_frontpage', 'homepage/empty');
}
?>

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?

About Frank

Frank Febbraro is the CTO at Phase2. He is primarily interested in software, technology and integrating new techniques and practices with proven methods and approaches. A combination of inherent understanding and real world experience enables ...

more >

Read Frank's Blog

Comments

by Jhon Wang (not verified) on Wed, 12/09/2009 - 06:14

Great Tips

This is one of the place where unecessary stuff get processed even when it is not needed. I wonder if this has been fixed in Drupal 7 ??

Thanks for the tip :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.