Patch to use JS Aggregation or JS at the bottom of the page

eliosh - July 24, 2008 - 10:34
Project:GMap Module
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:critical
Assigned:bdragon
Status:reviewed & tested by the community
Description

Based on this article :
http://wimleers.com/article/improving-drupals-page-loading-performance
I put all JSes on bottom of page.

I can see that gmap does not work, because module write script tag directly in body.

So i copy and paste theme_gmap function :

<?php
// ...

  // $map can be manipulated by reference.
 
foreach (module_implements('gmap') as $module) {
   
call_user_func_array($module .'_gmap', array('pre_theme_map', &$map));
  }

 
// Inline settings extend.
 
$o .= '<script type="text/javascript">'."\n";
 
$o .= "/* <![CDATA[ */\n";
 
$o .= 'Drupal.extend({ settings: '. drupal_to_js(array('gmap' => array($element['#map'] => $map))) ." });\n";
 
$o .= "/* ]]> */\n";
 
$o .= "</script>\n";
 
  return
$o;
?>

making this substitution :

<?php
 
// $map can be manipulated by reference.
 
foreach (module_implements('gmap') as $module) {
   
call_user_func_array($module .'_gmap', array('pre_theme_map', &$map));
  }

 
// Inline settings extend.
 
$o_script .= 'Drupal.extend({ settings: '. drupal_to_js(array('gmap' => array($element['#map'] => $map))) ." });\n";
 
 
drupal_add_js($o_script, 'inline');
 
  return
$o;
?>

I think it'll be great to put this modification in module.

#1

jcmarco - September 9, 2008 - 19:22

This patch works really well!

As Eliosh says, it is better for performance to load all javascript at the end of the html, so if you want to increase the load performance, just change the page.tpl.php template of your theme in order to load the scripts at the end (moving

<?php
print $scripts;
?>
just before your
<?php
print $closure;
?>
)

Also IMHO, it is more of a Drupal standard to load the JS with drupal_add_js function.

Actually that is mandatory if you are working with javascript_aggregator or sf_cache modules.

#2

eliosh - September 25, 2008 - 00:18
Version:5.x-1.0-beta3» 5.x-1.0-beta5

Please, can someone confirm me that this is a possible patch for future versions ?
I made this patch on a production site and, after upgrading from beta3 to beta5, i spent hours to understand why it doesn't work (without the patch).

Thanks a lot for all your work.

#3

jcmarco - October 7, 2008 - 14:25
Version:5.x-1.0-beta5» 5.x-1.0-rc1
Status:needs review» reviewed & tested by the community

The patch still works great also with 5.x-1.0-rc1!
Works fine with or without JS aggregation module or changing the JS load order in the page.tpl.php
More performance!

AttachmentSize
gmap_5.x-1.0-rc1.patch 877 bytes

#4

Rob Loach - December 9, 2008 - 07:44

Is there any reason why you're not using:

<?php
drupal_add_js
(array('gmap' => array($element['#map'] => $map)), 'setting');
?>

#5

jcmarco - December 14, 2008 - 11:00
Version:5.x-1.0-rc1» 5.x-1.0
Status:reviewed & tested by the community» needs review

Hi Rob,
I was applying the eliosh recommendation and it helped to understand the change. I agree with you that your code is cleaner.

I have re-rolled the patch removing old lines and leaving only the drupal_add_js. I have tested it and works fine.

Just as a reminder, this change allows that Drupal manage the javascript, allowing that if you are using JS Aggregator to optimize the html and js loading times GMap will work fine.

AttachmentSize
gmap_5.x-1.0.patch 582 bytes

#6

Rob Loach - December 14, 2008 - 20:37
Version:5.x-1.0» 5.x-1.x-dev
Status:needs review» reviewed & tested by the community

Much better... This should possibly go into the DRUPAL-6--1 branch if it doesn't already exist as well.

#7

Rob Loach - December 14, 2008 - 20:38

Very cool, thanks.

#8

SocialNicheGuru - December 21, 2008 - 02:43

this is great. thanks for this. it wasn't working for me until this patch. i hope it gets committed

#9

jcmarco - January 14, 2009 - 12:34
Title:Put javascript on bottom of the page (as suggested by YSLOW)» Patch to use JS Aggregation or JS at the bottom of the page
Version:5.x-1.x-dev» 6.x-1.x-dev
Status:reviewed & tested by the community» needs review

Once again for GMap for Drupal 6.x.

If your theme has the js at the bottom of the page, or you are aggregating JS, you need this to work

AttachmentSize
gmap-6.x-dev.module.patch 587 bytes

#10

ron_s - March 11, 2009 - 14:34

I applied the patch, and it worked fine (in 5.x-1.1-rc1). However if I move $scripts to the bottom of the page right before $closure in page.tpl.php, the GMap on the page never loads and I get a "Javascript is required to view this map" message.

#11

eliosh - March 17, 2009 - 23:05
Version:6.x-1.x-dev» 6.x-1.0
Status:needs review» reviewed & tested by the community

Confirm Rob patch even form 6.x-1.0

AttachmentSize
gmap-6.x-1.0.patch 589 bytes

#12

jcmarco - May 5, 2009 - 17:35

The patch works fine. And it adds the feature to be aggregated to the bottom of a page, increasing load performance. It is also more drupalish than original code.

Is there some issue with that patch that we could help to solve and avoid to being patching every time that GMap module is updated?

#13

EvanDonovan - May 8, 2009 - 21:55
Version:6.x-1.0» 6.x-1.1-rc1

I can confirm this works well for 6.x-1.1-rc1 also.

#14

heyyo - May 24, 2009 - 14:26

Why this patch is not in included in the module ?

#15

Rob Loach - May 24, 2009 - 15:22
Version:6.x-1.1-rc1» 6.x-1.x-dev

Yup, still not committed.

#16

mErilainen - July 9, 2009 - 07:27

Works well for me also. I had problems when using with Panels, that randomly the maps were just missing, without any message. Now it works fine.

#17

Rob Loach - July 9, 2009 - 09:03
Priority:normal» critical

#18

ron_s - July 20, 2009 - 14:24

The patch works fine. And it adds the feature to be aggregated to the bottom of a page, increasing load performance. It is also more drupalish than original code.

jcmarco, I have identified the issue. The patch is applied to the theme_gmap function. I changed this in gmap.module, but I had forgotten I was already overriding this theme in my template.php file. As soon as I applied the patch in template.php, everything works fine.

#19

ron_s - July 21, 2009 - 18:04

One additional comment: I had attempted to apply this patch suggested by Wim Leers to improve Drupal performance. The patch modifies the drupal_add_js function in common.inc to ensure jquery.js and drupal.js are loaded at the bottom of the page.

When using this patch with GMap, the map is loaded, but no markers are shown. I have looked at the code extensively, and all markers are being generated as expected. However when the map is built on the page, no markers exist (not even a marker "shell" in the HTML).

Any thoughts?

#20

seanr - August 17, 2009 - 17:40

Patch works fine for me. Let's please get this committed.

#21

seanr - August 17, 2009 - 17:41

ron_s, I think that should be a separate issue.

#22

snorkers - August 26, 2009 - 17:06

Tried the patch and moved $scripts to the foot of page.tpl.php. Works fine in a Blueprint based theme; although in Admin/Slate part of the map is missing - looks like it is rendering only upper left tile of the map. Tried in FF3 and Saf4 on a Mac.

#23

bec - September 4, 2009 - 10:38
Assigned to:Anonymous» bdragon

This patch fixes this issue as well: http://drupal.org/node/266595

#24

linuxuser - September 8, 2009 - 12:40

I still have this problem.

sites/all/modules/gmap/gmap.module:

  // $map can be manipulated by reference.
  foreach (module_implements('gmap') as $module) {
    call_user_func_array($module .'_gmap', array('pre_theme_map', &$map));
  }

  if (isset($mapids[$element['#map']])) {
    drupal_set_message(t('Duplicate map detected! GMap does not support multiplexing maps onto one MapID! GMap MapID: %mapid', array('%mapid' => $element['#map'])), 'error');
    // Return the div anyway. All but one map for a given id will be a graymap,
    // because obj.map gets stomped when trying to multiplex maps!
    return $o;
  }
  $mapids[$element['#map']] = TRUE;

  // Inline settings extend.
   $o_script .= 'Drupal.extend({ settings: '. drupal_to_js(array('gmap' => array($element['#map'] => $map))) ." });\n";

  drupal_add_js($o_script, 'inline');

  return $o;
}

There is a code in the middle, which is not mentioned above. When I removed this code, it didn't help.

#25

linuxuser - September 9, 2009 - 15:35

gmap-6.x-1.x-dev.tar.gz works for me!

#26

liberatr - September 24, 2009 - 01:51

The patch from #11 worked for me as well.

#27

gagarine - November 6, 2009 - 07:18

Not yet committed?

#28

EvanDonovan - November 6, 2009 - 16:26

@gagarine: No - we need to find a way to get in touch with the maintainer (bdragon). He hasn't been replying in the issue queue for the past few months.

#29

desecho - November 18, 2009 - 21:32

I have applied the patch #11, but the javascript error is still there. I can see the script tag and "CDATA" text in the source. It looks like it has not been updated, but the module is definitely patched and I cleared the cache using in Admin->Performance menu. Does anyone have a guess what might be a problem?

#30

stfb - November 25, 2009 - 20:07

Same problem as desecho ! Any guess ?

 
 

Drupal is a registered trademark of Dries Buytaert.