The Parallel module sets $scripts src to "//js-subdomain.examples/sites/default/files/js/js_b918e9e12007a05d73f9101cf854afde.js"

On line 94 of javascript_aggregator.module, the regex pattern filters out any path other than base_path() . file_directory_path():

    $path_to_files_directory = base_path() . file_directory_path();
    $pattern = "!(<script type=\"text\/javascript\" src=\"$path_to_files_directory)(.*?)(\"(.*?)><\/script>)!";
    if (preg_match_all($pattern, $scripts, $matches) > 0) {

As a result, any script whose path has been rewritten by Parallel is skipped by JS aggregator.

Vianney Stroebel
Likwid - Spécialistes Drupal - Paris

Comments

mikeytown2’s picture

Title: Incompatibility with Parallel module » Incompatibility with Javascript Aggregator
Project: Javascript Aggregator » Parallel

Sounds like I need to change the weight on my module so it runs near the end.

dalin’s picture

Hmmm, I didn't look into the details. I just changed the weight to 10 and saw no change. JS aggregated files are not being rewritten.

dalin’s picture

Ah, on further research it looks like it needs to fire after jquery update which has a weight of 99. When I set the weight of Parallel to 100 it works as expected.

dalin’s picture

Status: Active » Needs review
StatusFileSize
new1.92 KB

Turns out that jQuery Update is also manipulating the theme registry in tricky ways. And we have to do the same so that our code runs last. The code in this patch is taken almost verbatim from jQuery Update.

mikeytown2’s picture

Status: Needs review » Fixed

changed update 6001 to 6100
added in a hook install function
committed

philbar’s picture

Version: 6.x-1.x-dev » 6.x-1.0-beta2
Status: Fixed » Needs work

I'm still having an issue with 6.x-1.0-beta2.

Parallel enabled - JS is aggregated but not compressed.

Parallel disabled - JS is compressed and I get a file with .jsmin.js attached to the file name.

ak’s picture

I can confirm that, same problem by me.

srobert72’s picture

Subscribing

I moved <?php echo $scripts ?> to bottom of my template (page.tpl.php) just before closure.
JavaScript from header aren't minified. But those in footer are.
Both URL point to my CDN server.

srobert72’s picture

I tried some debug traces with some "echo".

function javascript_aggregator_preprocess_page(&$variables) {
  // Only do this for pages that have JavaScript on them.
  if (!empty($variables['scripts'])) {
echo 'javascript_aggregator_preprocess_page';
    $variables['scripts'] = _javascript_aggregator_minify($variables['scripts']);
  }
}
function phptemplate_closure($main = 0) {
  $footer = module_invoke_all('footer', $main);
  
  $js_footer = drupal_get_js('footer');
  // Only do this for pages that have JavaScript on them.
  if (!empty($js_footer)) {
echo 'phptemplate_closure';
    $js_footer = _javascript_aggregator_minify($js_footer);
  }

  return implode("\n", $footer) . $js_footer;
}
function _javascript_aggregator_minify($scripts) {
echo $scripts;
....
}

And here is interesting result :

phptemplate_closure
<script type="text/javascript" src="/sites/default/files/js/js_6dce5c63a61c264ce1dfa4a44885c864.js"></script>

javascript_aggregator_preprocess_page
javascript_aggregator_preprocess_page<script type="text/javascript" src="//cdn3.example.com/sites/default/files/js/js_7e290aa7e34d227e44099a687fa11098.js">
</script>

Javascript_Aggregator search pattern could not operate correctly in second case because URL has already been replaced by Parallel module.

Are we sure Parallel module always operates after Javascript_Aggregator ?

srobert72’s picture

Maybe this code from Javascript_Aggregator causes this issue :

/**
 * Implementation of hook_theme_registry_alter().
 *
 * Make javascript_aggregator's page preprocess function run *after* everything else's (even jQuery Update).
 */
function javascript_aggregator_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['page'])) {
    // If javascript_aggregator's preprocess function is there already, remove it.
    if ($key = array_search('javascript_aggregator_preprocess_page', $theme_registry['page']['preprocess functions'])) {
      unset($theme_registry['page']['preprocess functions'][$key]);
    }
    // Now tack it on at the end so it runs after everything else.
    $theme_registry['page']['preprocess functions'][] = 'javascript_aggregator_preprocess_page';
  } 
}

Modules weights have no effect because Javascript_Aggregator forces to be always called last one.
Issue to be assigned to Javascript_Aggregator module ?
Hmmm no DEV release since July 2009, I'm afraid it will not be fixed rapidly.

Is it possible to fixed it in another way in Parallel module ?

srobert72’s picture

I found a solution with modified Javascript_Aggregator code :

In javascript_aggregator.module file (function _javascript_aggregator_minify($scripts))
replace :

    $pattern = "!(<script type=\"text\/javascript\" src=\"$path_to_files_directory)(.*?)(\"(.*?)><\/script>)!";
    if (preg_match_all($pattern, $scripts, $matches) > 0) {
      $aggregated_file_name = $matches[2][0];

by

    $pattern = "!(<script type=\"text\/javascript\" src=\"(.*?)$path_to_files_directory)(.*?)(\"(.*?)><\/script>)!";
    if (preg_match_all($pattern, $scripts, $matches) > 0) {
      $aggregated_file_name = $matches[3][0];
SaxxIng’s picture

I can confirm that the "patch" proposed by srobert72 works very well on my test site!

srobert72’s picture

@SaxxIng
Thank you for testing my patch.

Problem remains this patch is for Javascript_Aggregator and not Parallel module.

We could reassign this issue to Javascript_Aggregator module.
But no DEV release since nearly one year for Javascript_Aggregator module. And I don't know if maintainer would want to fixe something concerning another module.
That's why I leave this issue to Parallel module.

EDIT on April 13, 2010
Javascript_Aggregator module has just released new DEV version.
I was wrong thinking there's no more activity in this module.
I proposed to patch it in order to fix issue with Parallel module.
See #770374: Incompatible with Parallel module

dalin’s picture

I believe that you just need to replicate the approach that I took in the patch that I submitted. The problem in that case was jQuery Update. Parallel's theme functions needs to run after jQuery Update. This was accomplished with a combination of setting the module weight to one heavier than jQuery Update, and use hook_theme_registry_alter() to move Parallel's theme function to be last. To make this approach work with JavaScript Aggregator all you should need to do is to make the weight of Parallel to be one heavier than JavaScript Aggregator (Parallel's implementation of hook_theme_registry_alter() just needs to run after JavaScript Aggregator's). The weight of JavaScript Aggregator is 9999 so setting the weight of Parallel to 10000 should do the trick.

srobert72’s picture

This feature has been added to Javascript_Aggregator module.
It now proposes option to serve JS files from external site.

#533190: PATCH: allow serving js files from an external file server

Solution without any patch is to configure base URL in Parallel module to serve images and CSS, and base URL in Javascript_Aggregator to serve JS files.

EDIT on April 13, 2010
Sorry for wrong news, this Javascript_Aggregator patch is for 5.x branch and not 6.x

dalin’s picture

Status: Needs work » Closed (fixed)

Well that's a bit crufty, but oh well.

srobert72’s picture

Status: Closed (fixed) » Needs work

@mikeytown2
What solution do you prefer ?

mikeytown2’s picture

hmmm... I have some code that makes parallel compatible with boost; looks like I need to modify boost for the new JavaScript Aggregator patch.

srobert72’s picture

EDIT #15
Sorry for wrong news, this Javascript_Aggregator patch is for 5.x branch and not 6.x

srobert72’s picture

EDIT #13
Javascript_Aggregator module has just released new DEV version.
I was wrong thinking there's no more activity in this module.
I proposed to patch it in order to fix issue with Parallel module.
See #770374: Incompatible with Parallel module

derjochenmeyer’s picture

I committed the code suggested in #11 to Javascript Aggregator. Please try new dev version and report here: #770374: Incompatible with Parallel module

Thanks.

srobert72’s picture

Patch commited in Javascript_Aggregator module works properly.
Seems this issue could be closed.

mikeytown2’s picture

Status: Needs work » Closed (fixed)

reopen if something new happens

dalin’s picture

Status: Closed (fixed) » Needs review

The patch mentioned in #20 and #21 was either never committed to JavaScript Aggregator, or it was rolled back. This is a good thing because JS Aggregator is the wrong place to make such a change (Any module should only be doing one thing).

To get Parallel working with JS Aggregator simply requires the weight of Parallel to be heavier. It must be 10000.

I would write a patch to do this, but I think it would be easier for @mikeytown2 to just edit the .install file and change the weight from 100 to 10000.

mikeytown2’s picture

Status: Needs review » Fixed

Here's the query

db_query("UPDATE {system} SET weight = 10000 WHERE name = 'parallel'");

Committed
http://drupal.org/project/cvs/548828

srobert72’s picture

@dalin #24
Patch is still in CVS (line 102):
http://drupalcode.org/viewvc/drupal/contributions/modules/javascript_agg...

I don't understand why it was wrong place because Javascript_Aggregator continues to do just its work without any interaction with Parallel. Any trace of Parallel in Javascript_Aggregator code.

dalin’s picture

Before Mikeytown2's patch in #25, the current stable version of Parallels + the current stable version of JavaScript Aggregator resulted in the JS file being moved to the CDN, but it was not minified.

One of the rules of good programming is that each component should do just one atomic thing and should not be affected by enabling/disabling other components. If you either build a component to do X, Y, Z and wash your dishes you quickly get into a maintenance nightmare. If you have to create workarounds for other components you also get into a maintenance nightmare. The logical way to have the system work is for Parallels to fire last to rewrite finalized URLs from the local system to the remote system. JSA should not need to write preg_replace workarounds for cases when Parallels is or isn't installed.

srobert72’s picture

And don't you think it could be a limitation in JSA to always search for relative URL ?
Doing this it will never be compatible with other modules which rewrite URL such CDN like modules.

Changing weight is a sort of workaround you speak about. You set your weight depending on other's weight.

I agree your solution works and it's a good one. Don't misunderstand me.
But I'm not sure mine was so bad. I really think JSA couldn't operate in all cases depending on which modules operate before it.

srobert72’s picture

For information there is same issue in ColorBox module. I've just checked it has weight 0.
#773006: Add support for the Parallel module

derjochenmeyer’s picture

@dalin #24
The patch mentioned above did get committed to 6.x-1.x-dev of Javascript Aggregator! Since it's a minor change I dont think it needs to be rolled back?

You are also right, about #788202: The current module weight is absurdedly high.

mikeytown2’s picture

so, change this to 110?

dalin’s picture

@mikeytown2 I would say no, the current regex workaround that JSA is trying to use to make itself run after Parallel isn't working.

srobert72’s picture

@dalin
But it works for me and for SaxxIng #12...
You said it hasn't been commited, but it was.
Are you sure you have this piece of code in your Drupal code ?
Check .../modules/javascript_aggregator/javascript_aggregator.module line 102

dalin’s picture

JSA 6.1.x dev has some regex that should work, but the current release does not.

srobert72’s picture

Yes and weight patch for Parallel has been commited to DEV branch too.
We are not comparing official releases.
But we are searching right solution in DEV branches before making it official.

derjochenmeyer’s picture

In fact, this all in 6.x-1.x-dev (only!) at the moment.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Version: 6.x-1.0-beta2 » 6.x-1.x-dev
Status: Closed (fixed) » Active

I'm using latest 1xdev for Parallel and latest dev for JSA. But the js url is not being rewritten.

Parallel has a weight of 10000, and JSA dev is now 109.

Also same issue with the CSS Gzip module, urls not being rewritten.

Anonymous’s picture

First thing I noticed is that my CSS Gzipped & JSA files have abolute paths, like http://www.domain.com/sites/default/files/css/css_3243423.css.

So Parallel's str_replace "src=/" doesn't work. I fixed it in parallel by using $base_url:

      global $base_url;

      (...)

      $variables['styles'] = str_replace(' href="'.$base_url.'/', ' href="' . variable_get('parallel_domain_css', '') . '/', $variables['styles']);

      (...)

      $variables['scripts'] = str_replace(' src="'.$base_url.'/', ' src="' . variable_get('parallel_domain_js', '') . '/', $variables['scripts']);
      $variables['closure'] = str_replace(' src="'.$base_url.'/', ' src="' . variable_get('parallel_domain_js', '') . '/', $variables['closure']);

My image URLs are relative, just CSS & JS is absolute - and only when using CSS Optimize and JSA.

So... does anyone else have abolsute urls?

dalin’s picture

Status: Active » Needs review
StatusFileSize
new2.32 KB

Hrrrrm. Just noticed that all of a sudden my aggregated CSS and JS have absolute URLs. Not sure what is causing this or why. I don't think it's core, but I don't have any modules on this site that I suspect either. Well here's a patch that handles both relative and absolute paths for CSS and JS.

mikeytown2’s picture

StatusFileSize
new2.22 KB

patch file didn't apply using CVS for some strange reason... my guess is the 2 random \ No newline at end of file lines in there.

can you verify that this patch does what your looking for?

mikeytown2’s picture

Status: Needs review » Fixed

eh looks good
committed.

Status: Fixed » Closed (fixed)

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

momper’s picture

sub