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 ?
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With