I'm using Magento 2 and I have a js file that was called like this:
default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceContainer name="after.body.start">
 <block class="Vendor\Module\Block\Ajax\Login\Js" template="Vendor_Module::login-ajax.phtml" name="login.ajax" as="login.ajax" before="head.components" ifconfig="loginpopup/general/enabled" />
 </referenceContainer>
 </body>
</page>
login-ajax.phtml
<script type="text/x-magento-init">
{
 "*": {
 "vm/loginajax" : <?php echo $block->getJsOptions(); ?>
 }
} 
requirejs-config.js
var config = {
 map: {
 '*': {
 'vm/loginajax': 'Vendor_Module/js/login-ajax'
 }
 }
};
Vendor_Module/js/login-ajax
define([
 'jquery',
 'mage/translate',
 'jquery/ui',
 'mage/validation/validation',
 'vm/mycustom'
], function (,ドル $t) 
the js file size is 3KB , i noticed the js file is rendered not at the very beginning, how can i set the priority so this js file will load first?
- 
 check this link - stackoverflow.com/questions/48167553/…Mohit Patel– Mohit Patel2020年08月14日 04:25:02 +00:00Commented Aug 14, 2020 at 4:25
- 
 1magento.stackexchange.com/questions/102347/…Pramod– Pramod2020年08月14日 07:23:45 +00:00Commented Aug 14, 2020 at 7:23
4 Answers 4
You cannot manually manage the sort order of JS files within core Magento 2 to the level you are asking for, and even if you could you do not want your file to load first as you have dependencies:
'jquery',
'mage/translate',
'jquery/ui',
'mage/validation/validation',
'vm/mycustom'
You have to wait until your dependencies have loaded else you will just run into a lot of errors. If for some reason you really do want your file to load first then do not use Require JS at all.
- 
 1Totally right 👍🏼Raul Sanchez– Raul Sanchez2020年08月14日 08:26:11 +00:00Commented Aug 14, 2020 at 8:26
- 
 Ben is right here. Question is what do you want to achieve? I mean not what you want to do on code level but what you want to do in you store from user perspective? Perhaps there is some other way. However looking at the name of js script and the list of dependencies I guess it will be difficult to make it in different way.JanuszJanczy– JanuszJanczy2020年08月15日 03:15:29 +00:00Commented Aug 15, 2020 at 3:15
If your goal is to load this js file first then instead of added after body, you can added then into head section like this.
default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="head.additional">
 <block class="Vendor\Module\Block\Ajax\Login\Js" template="Vendor_Module::login-ajax.phtml" name="login.ajax" as="login.ajax" before="-" ifconfig="loginpopup/general/enabled" />
 </referenceBlock>
 </body>
</page>
- 
 is not this still inside the <body> tag?jojo– jojo2020年08月14日 06:15:38 +00:00Commented Aug 14, 2020 at 6:15
- 
 Do you want this in body tag or head tag?Dhiren Vasoya– Dhiren Vasoya2020年08月14日 06:18:50 +00:00Commented Aug 14, 2020 at 6:18
- 
 i want this js file should be loaded up first, should not be matter in head or bodyjojo– jojo2020年08月14日 07:04:08 +00:00Commented Aug 14, 2020 at 7:04
- 
 Then you can use above code.Dhiren Vasoya– Dhiren Vasoya2020年08月14日 09:20:28 +00:00Commented Aug 14, 2020 at 9:20
Add your login-ajax JS file in the default.xml file of your custom module Vendor\Module\view\frontend\layout\default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <head>
 <link src="Vendor_Module::js/login-ajax.js"/>
 </head>
</page>
Keep the file login-ajax.js at Vendor\Module\view\frontend\web\js\login-ajax.js
And finally,
Run setup:upgrade and setup:static-content:deploy commands.
Hope this helps!
Need to override root.phtml instead of using require.js.
Copy default root.phtml from vendor/magento/module-theme/view/base/templates/ to your theme location app/design/frontend/<your_vendor_name>/<your_theme_name>/Magento_Theme/templates/.
Edit the file to include your script right before or after where require.js is included.
<!doctype html>
<html <?= /* @noEscape */ $htmlAttributes ?>>
 <head <?= /* @noEscape */ $headAttributes ?>>
 <script type="text/javascript" src="path/to/your/custom.js"></script>
 <?= /* @noEscape */ $requireJs ?>
 <?= /* @noEscape */ $headContent ?>
 <?= /* @noEscape */ $headAdditional ?>
 </head>
 <body data-container="body"
 data-mage-init='{"loaderAjax": {}, "loader": { "icon": "<?= /* @noEscape */ $loaderIcon ?>"}}'
 <?= /* @noEscape */ $bodyAttributes ?>>
 <?= /* @noEscape */ $layoutContent ?>
 </body>
</html>
- 
 Wouldn't this error asrequireis not yet defined?Ben Crook– Ben Crook2020年08月14日 09:23:22 +00:00Commented Aug 14, 2020 at 9:23
- 
 Above solution will load JS file at the top where code should be written without any dependency(core JS). Using RequireJS as you've mentioned we cannot manually manage the sort order of JS because RequireJS loads its module source files asynchronously, and doesn't promise anything about the order they're loaded in. Just we can manage "load order" dependencies using RequireJS shim configuration directive.Pratik Oza– Pratik Oza2020年08月15日 05:15:12 +00:00Commented Aug 15, 2020 at 5:15
- 
 Ah I see, yeah that would work but not in this use case as there is a dependency on jQuery, jQuery UI, Magento's core validation, and translations :(Ben Crook– Ben Crook2020年08月17日 10:53:31 +00:00Commented Aug 17, 2020 at 10:53
Explore related questions
See similar questions with these tags.