Nice idea. Here's a version (attached) that takes a slightly different approach (reads the local files rather than fetching them over HTTP).

Comments

derjochenmeyer’s picture

Thanks a lot for the response. This is my first try to maintin a module containing functionality that has been useful to myself on many sites and that i want to contribute back, since its often requested. :)

The approach you suggest is more elegant. But there are some drawbacks that i see.

Patterns: In my approach i use the same pattern to search and remove the script files, thus making sure only those get removed that were actually aggregated. E.g. your approach would only aggregate files included with double quotes but remove also files linked with single quotes. Or did i miss something?

install file: It surely makes sense to put the dir creation function into an install file and not run it on every page load. But is it necessary? I dont know enough about performance of this function.

  $js_dir = file_create_path('js'); // FIXME: move to .install
  file_check_directory($js_dir, 1); // FIXME: move to .install

Regarding this if (!file_exists($js_file)){ // FIXME: check if source files have changed, or empty folder if cache is cleared :
What i wanted to do is create an admin page with a "clear aggregated javascript files". Including Admin menu integration.

derjochenmeyer’s picture

Assigned: Unassigned » derjochenmeyer

btw, thanks a lot for this hint:

function js_aggregator_read_file($url){
  return file_get_contents(preg_replace('/^\//', '', $url));
}

it solves this issue i guess: http://drupal.org/node/211061

hickory’s picture

Re: "would only aggregate files included with double quotes but remove also files linked with single quotes"
It would probably be easy to alter the regular expression to match single quotes as well. Is this necessary though? Presumably all scripts added with drupal_add_js use double quotes - can anything else get into $scripts?

There should be an admin box on the 'performance' page to switch JS aggregation on and off: if it's on, then there would ideally be a way to remove the cached .js files in the same way that aggregated CSS files are refreshed at the moment.

Might as well put those two lines in an install file if you can, rather than running them on every page load.

derjochenmeyer’s picture

can anything else get into $scripts?

You're right, probably not :-) But it would be "failsave then"... i just know that one of the gmap modules outputs the include with singlequotes, but thats in $head.

Thats anoter thing i want to make sure. That the file is not an external file.

To put the on/off option on the 'performance' page is a good idea, thanks!

Do you see any way to get rid of the page.tpl.php modification? I guess i cant modify phptemplate variabels in a module because phptemplate is run after all modules, right?

hickory’s picture

I can't think of a way to avoid editing page.tpl.php, but I don't think that's a big problem.

The $scripts variable is generated from drupal_get_js in the phptemplate_page function, but you don't want to be overriding all that, I don't think, even if it was possible.

derjochenmeyer’s picture

Status: Active » Needs review
StatusFileSize
new4.24 KB

Thanks a lot for your suggestions!

I created a new fieldset on the Performance page to disable/enable the js aggregation. I also added a menu callback and a link to clear the js cache. (access only for user 1 at the moment).

I did NOT move the files/js creation to an install file, similar to the core css aggregation its still in the main function (http://drupal.org/node/149402).

Ill update CVS later today.

derjochenmeyer’s picture

StatusFileSize
new4.31 KB

Some more changes and error fixes.

derjochenmeyer’s picture

Status: Needs review » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.