Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS script tag in <head> or at bottom of page?

Should the RequireJS script tag be located in the <head> or bottom of an HTML page?

<script data-main="resources/js/main" src="require.js"></script>

I have seen reputable sources place the RequireJS script tag in either position, but no reputable source seems to explicitly say where the ideal location is.

Arguments for placing the tag in the head would be that RequireJS could already begin loading and executing dependent scripts asynchronously while the page continues loading (though this gain may be minuscule).

Arguments for placing the tag at the bottom of the page would be that checks for the DOM being ready wouldn't be needed within modules (though not including this check may cause problems if the module is used in a third-party system that loads the script in the <head> tag).

like image 583
Donald Taylor Avatar asked Oct 02 '22 20:10

Donald Taylor


1 Answers

General best practice is to always place your script at the bottom.

You may have seen people including it in the HEAD as it is not doing a big difference.

I'd recommend in the bottom, because as you'll want to build your scripts before production, you'll end up loading all your script in the head otherwise.

Note that if you're running a "single page application", then it really doesn't matter as chances are your body will be empty - or almost empty.

like image 150
Simon Boudrias Avatar answered Oct 05 '22 05:10

Simon Boudrias