Drupal page speed optimization

One of the most important thing in page speed optimization is the number of requests: the smaller the better.

For the images this is accomplished mainly by using css sprites when creating the theme.

But there are other file in Drupal 7 that can be optimized.

First thing is activating the aggregation of css and js files on Configuration > Performance page

Even after this settings we'll have more than one file for css and also for javascript

Functions to add in template.php:

Reducing css files

// Reduce the number of css files
function my_theme_css_alter(&$css) {
    global $user;
    // remove some css files if the user is not logged
    if (!$user->uid) {
        unset($css['sites/all/modules/ckeditor/ckeditor.css']);
        //unset($css['sites/all/themes/traduction_web/system.menus.css']);
        unset($css['modules/user/user.css']);
        unset($css['sites/all/modules/views/css/views.css']);
        unset($css['modules/field/theme/field.css']);
        unset($css['sites/all/modules/ctools/css/ctools.css']);

        // put theme custom css at the end
        $css['sites/all/themes/traduction_web/css/custom.css']['weight'] = 1000;

        // put all the files in one group to have only 1 css file
        foreach($css as &$v) {
            // keep order
            $v['weight'] = $v['group'] + $v['weight'];
            $v['group'] = 1;
        }
    }
}

Reducing the number of javascript files

// Reduce the number of js files
function my_theme_js_alter(&$js) {
    global $user;
    // group js files together
    if (!$user->uid) {
        // group theme custom js file if exists
        if(isset($js['sites/all/themes/traduction_web/js/script.js'])) {
            $js['sites/all/themes/traduction_web/js/script.js']['group'] = -100;
            $js['sites/all/themes/traduction_web/js/script.js']['weight'] = 1000;
        }
    }
}