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. 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?


Comments
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 :)
I also used this drupal site
I also used this drupal site (otimização de sites) and got great results with SEO. Currently, due to some innovations that have needed had to make a new creation in XHTML + PHP .. But soon I intend to make a new creation on top of Drupal! Congratulations for the article!
Post new comment