Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Rails doesn't include the Javascript files at the bottom of the HTML page by default?

I read here that it is better to include Javascript files at the bottom of the HTML page.

Why Ruby on Rails doesn't do so by default ?

like image 408
Misha Moroshko Avatar asked Sep 28 '11 14:09

Misha Moroshko


People also ask

Can you use JavaScript with rails?

If you plan to use Rails with importmap-rails to manage your JavaScript dependencies, there is no need to install Node. js or Yarn. When using import maps, no separate build process is required, just start your server with bin/rails server and you are good to go.

Should JavaScript be in head or body?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.

Where is application JS in rails 6?

Since Rails 6, JavaScript is no longer a part of the asset pipeline and instead integrates using Webpacker by default. The code lives in the app/javascript folder and is loaded into the body of the application layout via the javascript_pack_tag .


1 Answers

I would guess that Rails includes your application.js in the <head> by default because it assumes you'll be minimizing things via the asset pipeline. There's no need to worry about where your script is included if it's a single external file being loaded in parallel by a single HTTP request.

like image 122
meagar Avatar answered Oct 26 '22 22:10

meagar