2

I just got assigned to a project that requires bug fixes. It is not a bug, but the client is asking us to reduce size of our site's compressed javascript.

I found out that the compressed js' size is around 500kb, which is pretty big. I think it is that big because Drupal loads all the javascript from all the enabled modules. The problem is that there are scripts that I don't use on my homepage or other pages.

What would be a good solution or workaround for my issue? One thing I can think of is to turn off compreconditionally disable modules in my template...but...is that okay or even possible?

2
  • Check this link: shvetsgroup.com/blog/optimizing-javascript-and-css-files-drupal . Don't try to remove default libraries like jquery etc. Also the I assume your web server is setting the expires headers then those javascript libraries will be loaded only once. – AgA Dec 17 '11 at 10:56
  • @Aga // thank you for the link and time to put your post! – Moon Dec 19 '11 at 6:41
1

What your looking to do is remove extra JS or CSS files, correct? AdvAgg can do this for you.
hook_advagg_js_pre_alter()
hook_advagg_css_pre_alter()
are the hooks you want to implement. Then inside of the hook you can use the arg() function to figure out what page your on.

$args = arg();
if (   isset($args[0]) && $args[0] == 'node'
    && isset($args[1]) && is_numeric($args[1])
    && !isset($args[2])
      ) {
  $node = node_load($args[1]);
  if (!empty($node) && $node->type == 'page') {
    // Do awesome stuff to the JS/CSS array.
  }
}

See advagg_js_cdn_advagg_js_pre_alter() for an example on how the hook is used. Also be aware of drupal_is_front_page(); it might be useful.

1
  • exactly what I was looking for!!!!!!! – Moon Dec 19 '11 at 6:41
0

You can also check out a context add assets module:

This module will add a reaction within context to add theme, module, or custom folder files to your page(s).

Ever wanted to quickly add a css file to a theme? Include a js file on only one page (or section?)

Make using multiple CSS files a breeze with this module and harness the power of Context in your theme!

From context add assets project page. Of course it requires the context module and that maybe a lot of overhead to do something like this. But it's a lot easier!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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