Introduction About Site Map

XML
RSS 2 Feed RSS 2 Feed
Navigation

Main Page | Blog Index

Sunday, March 16th, 2014, 9:49 am

Aggregating JavaScript Files in Drupal

Background

In Drupal, provided that the required modules are installed, performance can be improved in a variety of ways. Aggregating and compressing CSS files can decrease page rendering time (getting the required files more quickly prior to rendering) and the same goes for JavaScript, which executes programs while the page is loading or after it is loaded.

Configuration

To minimise the number of GET operations and the size of transmitted files we can go to Configuration -> Development -> Performance. Therein there are many options, cache related ones in particular. Under Bandwidth Optimisation there are two relevant options which are not yet used.

  • One option is “Aggregate and compress CSS files.
  • The other is “Aggregate JavaScript files.

We enable the second option only, then press the “Save Configuration” button.

HTTP Headers

Before the above change is applied, however, we should probe to see what JavaScript are associated with some sample pages and how they are delivered to the visitor. The Firefox Live HTTP Headers plugins can help here. It is available from Mozilla’s official extensions repository; version 0.17 is the latest (this extension is nearly a decade old and it was widely popular half a decade ago). Once installed, Firefox needs to be restarted, whereupon the extension can be invoked, from the bottom of the “Tools” menu, under “Live HTTP headers”. Load the main page of the site (or any other page for that matter) and analyse the output. “Capture” is enabled by default.

Press “Save All” to save the output to a file. This will be used to compare the number of files before and after the change.

Close Live HTTP headers (the window).

Apply the optimisation changes (as above, ensure the green box says “Your configuration options have been saved.”) and repeat the process again, this time choosing “Save All” and choosing another filename.

Before/After Analysis

Open a text editor and start comparing the files. Suffice to say, if there are issues loading pages after applying the above change, then stop and investigate. A more thorough investigation (beyond ensuring the settings have a positive effect) will come later.

If aggregation works correctly, expect to see in the second (“after”) file that the JavaScript GET request is for /sites/default/files/js/js_[SOME_LONG_CODE].js, not something with “modules”. This means that a new cache-esque file was generated which combines several others. Drupal saves it on disk for performance improvements.

Open the “Live HTTP headers” again, ensuring the log is empty. Reload the same page and scroll up to the same point, ensuring the same JavaScript is pulled from cache again. If the file name is not identical (in its name/size), then it is possible that it got regenerated, which may slow down the server side but not the client side (except when waiting for it to be regenerated); network speed — even if files are concurrently downloaded — should not be an issue here, so it may be good enough. The important thing is that we can verify JavaScript got aggregated.

Generally speaking, another sanity check can be comparison of file sizes of the file ‘dumps’ Live HTTP headers. For the main page of one test site, the “before” file is 131KB in size, whereas the “after” file is 106KB in size. This indicates that a lot less was included in GET requests; getting pertinent details (like which requests were spared) is a more time-consuming exercise.

Site Integrity Tests

At this stage, systematic site tests are needed, especially in pages that are JavaScript-heavy and may be affected by JavaScript functions being thrown into the same file. Among the pages tested (not a full list):

  • Front page
  • Contact page

This would of course depend on the site at hand.

JavaScript Console

In Mozilla’s Firefox Web site, navigate to Web Developer’s Toolbox, “javascript” under common tags has JavaScript Debugger. Install it (Add to Firefox, Install Now, Restart Now).

Once the browser has the debugger installed and enabled run through a few pages while the console is visible to identify any issues which remain. File -> Open Web Location is another option. Once all is clear and there are no major errors (if any errors at all) it seems ready for permanent switchover to this operation mode.

Technical Notes About Comments

Comments may include corrections, additions, citations, expressions of consent or even disagreements. They should preferably remain on topic.

Moderation: All genuine comments will be added. If your comment does not appear immediately (a rarity), it awaits moderation as it contained a sensitive word or a URI.

Trackbacks: The URI to TrackBack this entry is:

https://schestowitz.com/Weblog/archives/2014/03/16/aggregating-javascript/trackback/

Syndication: RSS feed for comments on this post RSS 2

    See also: What are feeds?, Local Feeds

Comments format: Line and paragraph breaks are automatic, E-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Back to top

Retrieval statistics: 21 queries taking a total of 0.117 seconds • Please report low bandwidth using the feedback form
Original styles created by Ian Main (all acknowledgements) • PHP scripts and styles later modified by Roy Schestowitz • Help yourself to a GPL'd copy
|— Proudly powered by W o r d P r e s s — based on a heavily-hacked version 1.2.1 (Mingus) installation —|