8

Drupal 6 has the awesome advagg module, which does an awesome job of intelligently bundling CSS and JS, but currently no D7 release.

What I am looking to achieve is:

  • less CSS and JS files (5 CSS and 5 JS files is far too many)
  • JS files have minification (currently core only does this for CSS)

Is there a drupal 7 solution that will help here? What are other people's experience with optimising this?


Edit: The question was originally posted in 2011. There is now a very stable D7 release of advagg.

2 Answers 2

12

For D7, there's Core Library which has a learning mode that aggregates based on real-world statistics about your site.

If you want to brute-force everything into one giant aggregate, I've written an article with some sample code:

/**
 * Implements hook_js_alter().
 */
function mymodule_js_alter(&$javascript) {
  uasort($javascript, 'drupal_sort_css_js');
  $i = 0;
  foreach ($javascript as $name => $script) {
    $javascript[$name]['weight'] = $i++;
    $javascript[$name]['group'] = JS_DEFAULT;
    $javascript[$name]['every_page'] = FALSE;
  }
}

/**
 * Implements hook_css_alter().
 */
function mymodule_css_alter(&$css) {
  uasort($css, 'drupal_sort_css_js');
  $i = 0;
  foreach ($css as $name => $style) {
    $css[$name]['weight'] = $i++;
    $css[$name]['group'] = CSS_DEFAULT;
    $css[$name]['every_page'] = FALSE;
  }
}

Drupal.org issue #1034208 also proposes relaxing the strict ordering requirements slightly.

5
  • The giant aggregate did not seem to produce the correct behaviour, it would indeed make 1 large CSS/JS file for page, but this file would change depending on the page you are on. This is obviously less than ideal.
    – wiifm
    Feb 12, 2012 at 10:47
  • Exactly right, if each page loads different JS files, then the aggregate will necessarily be different. You've just rediscovered the reason Drupal 7 splits files into functional groups (JS_LIBRARY, JS_DEFAULT, and JS_THEME). With complex sites this is often more efficient than one giant file.
    – Dylan Tack
    Feb 13, 2012 at 18:59
  • 1
    If there are 3 groups, how can there be 5 files loaded in the head? Why not never more than 3? Bundling everything in 1 file is apparently a horrible idea, but how to keep the JS files to a mimimum?
    – Rudie
    May 14, 2013 at 19:05
  • i have tried the code that mentioned here. Actually it creating two compressed js files for me. How can I make it one js file?
    – Cool
    Aug 18, 2014 at 11:57
  • @DylanTack one of my website has problem with loading css fiels [drupal.stackexchange.com/questions/128649/… and website address [living.md/] I was wandering where to put your code, and can i use your code along with advagg module ?
    – Yama
    Sep 11, 2014 at 13:09
3

AdvAgg D7 is being developed. Other options (quotes included are from their project pages):

... allows on-the-fly minification of site JavaScript with uglify.js. The module relies on a web service (uglify.me) by default to do the minification so you do not need to do any pre-processing on your server to enjoy the benefits of minified JavaScript.

... designed to help speed up front end performance in a site. In this first release of the Speedy module it provides minified versions of core JavaScript files that are not already minified. For example, a minified version of drupal.js is provided while jquery.js (already minified) is not.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.