4

Hey I have added the following libraries:

my-google-maps:
  js:
    https://maps.googleapis.com/maps/api/js?key=my_funkey_key&: {type: external, weight: -400, attributes: {defer: true,async: true}}
my_other_lib:
  header: true
  js:
    js/mylib.js: { }
  dependencies:
    - core/drupal
    - core/drupalSettings

I have tried multiple ways to attach these files in a specific order(first google and then my custom js:

{{ attach_library('my_module/my-google-maps') }}
{{ attach_library('my_module/my_other_lib') }}

If it is not in the specific order the map is undefined.How can I specific the load order?

1
  • Did you try making my_module/my-google-maps a dependency of my_module/my_other_lib ? Sep 22, 2016 at 17:17

1 Answer 1

6

The libraries are loaded by weight, not in the order they are defined.

You already had figured that out. You have set a weight of -400 for the external library. This will be the first library loaded, because in drupal the lowest weight for a library is -22. But most times you would want to be above -20, because this is the weight of jquery, on which most libraries depend.

The problem in your case is, that the library mylib.js is specified to be loaded in head. So the external library will never be able to come ahead. It's loaded at top of all other libraries because of the low weight, but still with the bunch of all the other libraries at the end of the document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.