Skip to content

Commit 8d6ed16

Browse files
committed
fix(map-loader): change includeScript to place maps api script tag to head element instead of body
Placing the script tag in document body causes the Google Maps API to be included multiple times when document is stored using the History API and then later retrieved (e.g. when using Turbolinks.js). Placing the script tag inside head element solves this problem, ensuring the Google Maps API is loaded only once.
1 parent f18753d commit 8d6ed16

13 files changed

+44
-44
lines changed

dist/angular-google-maps-street-view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! angular-google-maps 2.3.3 2016-06-03
1+
/*! angular-google-maps 2.3.3 2016-07-16
22
* AngularJS directives for Google Maps
33
* git: https://github.com/angular-ui/angular-google-maps.git
44
*/
@@ -109,7 +109,7 @@ return UUID;
109109
script.id = scriptId = "ui_gmap_map_load_" + (uuid.generate());
110110
script.type = 'text/javascript';
111111
script.src = getScriptUrl(options) + query;
112-
return document.body.appendChild(script);
112+
return document.head.appendChild(script);
113113
};
114114
isGoogleMapsLoaded = function() {
115115
return angular.isDefined(window.google) && angular.isDefined(window.google.maps);

dist/angular-google-maps-street-view.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps-street-view_dev_mapped.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps-street-view_dev_mapped.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps-street-view_dev_mapped.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps-street-view_dev_mapped.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! angular-google-maps 2.3.3 2016-06-03
1+
/*! angular-google-maps 2.3.3 2016-07-16
22
* AngularJS directives for Google Maps
33
* git: https://github.com/angular-ui/angular-google-maps.git
44
*/
@@ -96,7 +96,7 @@ Nicholas McCready - https://twitter.com/nmccready
9696
script.id = scriptId = "ui_gmap_map_load_" + (uuid.generate());
9797
script.type = 'text/javascript';
9898
script.src = getScriptUrl(options) + query;
99-
return document.body.appendChild(script);
99+
return document.head.appendChild(script);
100100
};
101101
isGoogleMapsLoaded = function() {
102102
return angular.isDefined(window.google) && angular.isDefined(window.google.maps);

dist/angular-google-maps.min.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps_dev_mapped.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps_dev_mapped.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-google-maps_dev_mapped.min.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/coffee/providers/google-map-api-provider.spec.coffee

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe 'uiGmapGoogleMapApiProvider', ->
2222
it 'uses maps.google.cn when in china', ->
2323
options = { china: true, v: '3.17', libraries: '', language: 'en' }
2424
mapScriptLoader.load(options)
25-
lastScriptIndex = document.getElementsByTagName('script').length - 1
26-
expect(document.getElementsByTagName('script')[lastScriptIndex].src).toContain('http://maps.google.cn/maps/api/js')
25+
lastScriptIndex = document.head.getElementsByTagName('script').length - 1
26+
expect(document.head.getElementsByTagName('script')[lastScriptIndex].src).toContain('http://maps.google.cn/maps/api/js')
2727

2828
describe 'on Cordova devices', ->
2929
beforeAll ->
@@ -40,34 +40,34 @@ describe 'uiGmapGoogleMapApiProvider', ->
4040
options = { v: '3.17', libraries: '', language: 'en', device: 'online' }
4141
mapScriptLoader.load(options)
4242

43-
lastScriptIndex = document.getElementsByTagName('script').length - 1
44-
expect(document.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=online')
43+
lastScriptIndex = document.head.getElementsByTagName('script').length - 1
44+
expect(document.head.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=online')
4545

4646
it 'should wait for the online event to include the script when the device is offline', ->
4747
window.navigator.connection.type = window.Connection.NONE
4848

4949
options = { v: '3.17', libraries: '', language: 'en', device: 'offline' }
5050
mapScriptLoader.load(options)
51-
lastScriptIndex = document.getElementsByTagName('script').length - 1
52-
expect(document.getElementsByTagName('script')[lastScriptIndex].src).not.toContain('device=offline')
51+
lastScriptIndex = document.head.getElementsByTagName('script').length - 1
52+
expect(document.head.getElementsByTagName('script')[lastScriptIndex].src).not.toContain('device=offline')
5353

5454
# https://github.com/ariya/phantomjs/issues/11289
5555
onlineEvent = document.createEvent 'CustomEvent'
5656
onlineEvent.initCustomEvent 'online', false, false, null
5757
document.dispatchEvent onlineEvent
5858

59-
lastScriptIndex = document.getElementsByTagName('script').length - 1
60-
expect(document.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=offline')
59+
lastScriptIndex = document.head.getElementsByTagName('script').length - 1
60+
expect(document.head.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=offline')
6161

6262
describe 'performance', ->
6363
it 'should delay loading the API when delayLoad is true, until the controller explicitly calls it', ->
6464
options = { v: '3.17', libraries: '', language: 'en', sensor: 'false', device: 'online', preventLoad: true }
6565
mapScriptLoader.load(options)
6666

67-
lastScriptIndex = document.getElementsByTagName('script').length - 1
68-
expect(document.getElementsByTagName('script')[lastScriptIndex].src).not.toContain('device=online')
67+
lastScriptIndex = document.head.getElementsByTagName('script').length - 1
68+
expect(document.head.getElementsByTagName('script')[lastScriptIndex].src).not.toContain('device=online')
6969

7070
mapScriptManualLoader.load()
7171

72-
lastScriptIndex = document.getElementsByTagName('script').length - 1
73-
expect(document.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=online')
72+
lastScriptIndex = document.head.getElementsByTagName('script').length - 1
73+
expect(document.head.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=online')

src/coffee/providers/map-loader.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ angular.module('uiGmapgoogle-maps.providers')
3333
script.id = scriptId = "ui_gmap_map_load_#{uuid.generate()}"
3434
script.type = 'text/javascript'
3535
script.src = getScriptUrl(options) + query
36-
document.body.appendChild script
36+
document.head.appendChild script
3737

3838
isGoogleMapsLoaded = ->
3939
angular.isDefined(window.google) and angular.isDefined(window.google.maps)

0 commit comments

Comments
 (0)