We have a custom module that has a JS library that we want to load in the <head>
tag as the very first item even before all the normal Drupal JS files. All other libraries are loaded through mytheme.libraries.yml
.
How can I load the library (mymodule/myscript
) so that it loads as the first <script>
tag in the <head>
?
What we tried:
mymodule.module
function mymodule_page_attachments(array &$page) {
$attachments['#attached']['library'][] = 'mymodule/myscript';
}
mymodule.libraries.yml
myscript:
version: 1.x
header: true
js:
/path/to/file: { external: true, weight: -200 }
Current problem:
<head>
<script>script1</script>
<script>script2</script>
<script>myscript is loaded here</script>
</head>
Goal:
<head>
<script>myscript is loaded here</script>
<script>script1</script>
<script>script2</script>
</head>
hook_page_attachments()
in which you can attach your JS library in$page['#attached']['html_head'][]
. For more info check this link: Click here