Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some JS and CSS on my site that are very specific to some pages. From a Drupal perspective, it is good idea to add them using drupal_add_js() instead of THEME.info? Does such thing affect caching, or anything else Drupal-specific?

share|improve this question
It is not really Drupal related. Consider flagging for moderator attention and asking to move to Webmasters. Unless you have specific concerns about #attached - then please write them – Mołot Jan 28 '14 at 14:08
@Mołot maybe adding js/css files through .info causes some sort of caching into Drupal. so it is related to Drupal – user2851298 Jan 28 '14 at 14:12
up vote 6 down vote accepted

This question is borderline one, it's one of these topics where art meets science of software development.

Either way, drupal_add_js() should be avoided. Always favour #attached property, and if possible, add libraries, not single JS files.

For site-wide vs single page: you probably have some kind of JS and CSS aggregation enabled. Using different sets of files is wasteful - it means that for every page your client needs to download another aggregated file. Leaving the same set everywhere allows clients to use cached aggregated version - and this is (usually) the least time-consuming and least transfer-consuming way to go.

As Clive reminded, there are situations that are exceptional. Sometimes you need different versions of jQuery, or conflicting sets of libraries, and aggregating them can result in JS error, or create really big aggregated file. In situations like this you may not want to include all of them, or simply it might be impossible due to conflicts. Even then minimizing the number of "sets" is a good idea.

If you have a really big script used only on one rarely used subpage, some aggregation modules will allow you to exclude it from aggregation and always serve separately - a wise choice if you have 1kb of JS logic that you need in one place only, for example. This part is an art of profiling, not really knowledge and recipes.

share|improve this answer
It's worth mentioning that there are potential exceptions to the "Using different sets of files is wasteful" rule. For example, I have a site that needs 3 different versions of jQuery and various other libraries on different page types - I wouldn't want all of those libraries included in the aggregated JS for every page, much too heavy for my liking – Clive Jan 28 '14 at 14:45
@Clive updated. – Mołot Jan 28 '14 at 14:50
@Clive Some would argue that one bad page load w/ everything and then faster subsequent pages b/c cached files is a better experience for the end user. – MPD Jan 28 '14 at 14:55
And that's the part that is not about knowledge and facts, but about feelings. Also, @MPD, having 2 jQuery versions at the same time is not really a good idea ;) – Mołot Jan 28 '14 at 14:57
@MPD Definitely agree with that in most circumstances - but when it's a busy site (CDN not an option for various reasons), and the pages with the heavy libraries don't get hit very often, it's not the best trade off IMO. For 90% of cases aggregating the whole lot may well be better like you say. Just backs up what you guys are both saying about needing to examine the specific site to get a real decision – Clive Jan 28 '14 at 15:06

This is a slightly complicated situation on a Drupal site, and it depends on whether you are using Javascript aggregation or not.

If you aren't, then the question isn't really Drupal specific. Each JS file will potentially result in a HTTP request, depending on cache headers and what your browser does. In general, you want to keep the number of requests low, and also try to transfer as few bytes as possible.

If you are using aggregation, then things get tricky. If you use per-page files, then you will have different aggregated files, which means that your users will end up making HTTP requests for them. In other words, this can defeat the effectiveness, somewhat, of file aggregation.

As with all things, proper profile on your site is the only way to tell whether it will make a difference for you.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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