Skip to content

Commit

Permalink
fix: don't load scripts if requirejs is present or if they are alread…
Browse files Browse the repository at this point in the history
…y loaded
  • Loading branch information
EdieLemoine committed Dec 5, 2019
1 parent 8045770 commit 54e6a31
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .commitlintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": [
"@commitlint/config-conventional"
]
],
"rules": {
"header-max-length": [0]
}
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@fortawesome/free-solid-svg-icons": "^5.11.2",
"@fortawesome/vue-fontawesome": "^0.1.8",
"@myparcel/js-sdk": "^2.0.0",
"core-js": "^3.4.5",
"core-js": "^3.4.7",
"custom-event-polyfill": "^1.0.7",
"debounce": "^1.2.0",
"lodash.isequal": "^4.5.0",
Expand Down
35 changes: 26 additions & 9 deletions src/components/Pickup/Map/Leaflet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,35 @@ export default {
* On mounting the map component, load all the needed scripts externally. This is done to not bloat the bundle size
* and only load the map when the user selects it.
*/
mounted() {
Promise.all([
createScript('https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css'),
createScript('https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js'),
]).then(() => {
async mounted() {
// Skip all script loading if RequireJS is detected. It does NOT like us loading scripts manually.
const loadScripts = typeof requirejs === 'undefined';
if (loadScripts) {
const scripts = [];
const leafletCss = 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css';
const leafletJs = 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js';
const vue2LeafletJs = 'https://cdnjs.cloudflare.com/ajax/libs/Vue2Leaflet/1.0.2/vue2-leaflet.min.js';
if (!document.querySelector(`link[href="${leafletCss}"]`)) {
scripts.push(leafletCss);
}
if (typeof L === 'undefined') {
scripts.push(leafletJs);
}
await Promise.all(scripts.map((script) => createScript(script)));
/**
* This scripts depends on leaflet.js so has to wait until it's loaded.
*/
createScript('https://cdnjs.cloudflare.com/ajax/libs/Vue2Leaflet/1.0.2/vue2-leaflet.min.js').then(() => {
this.onLoadedScripts();
});
});
if (typeof Vue2Leaflet === 'undefined') {
await createScript(vue2LeafletJs);
}
}
this.onLoadedScripts();
},
created() {
Expand Down

0 comments on commit 54e6a31

Please sign in to comment.