Skip to content

Instantly share code, notes, and snippets.

@hirowatari
Created July 31, 2023 17:06
Show Gist options
  • Save hirowatari/8a8f7dc2412c3a753c73a1515ea41edb to your computer and use it in GitHub Desktop.
Save hirowatari/8a8f7dc2412c3a753c73a1515ea41edb to your computer and use it in GitHub Desktop.
function loadMapboxGl() {
/**
* Load MapBox Assets
* we either need to load mapbox with requirejs if our customer is using it
* or we need to load it ourselves.
*
* If we wait write out a script tag that loads it well...HERE BE DRAGONS.
* requirejs could load before mapbox. In this case mapbox will not work and requirejs will not load it. This is the worst case.
* At times this will load mapbox even when requirejs will exist later,
* but this should work since it should be loaded before requirejs.
*
*/
/**
* To update mapbox-gl, update both the requirejs url, the css and the file app/views/embed/mapbox-gl.js.
*/
if ('mapboxgl' in window) {
return;
}
if ('requirejs' in window) { // if requirejs is loaded before prolo.js.
requirejs(['https://api.mapbox.com/mapbox-gl-js/v1.10.1/mapbox-gl.js'], function(mapboxgl) {
window.mapboxgl = mapboxgl;
if ('ProloInitialization' in window) {
ProloInitialization();
}
});
} else {
<%=raw File.read(Rails.root.join('app/views/embed/mapbox-gl.js')) %>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment