This lets you load application JavaScript asynchronously from <head>
. Compatible with Turbolink v2, v5.0, v5.1, jquery-turbolinks, and no Turbolinks. Compatible with all browsers and IE9+ (only IE10+ will actually benefit from this though).
How to use this:
-
Add the HTML snippet just before the closing
</body>
tag. -
Add the on_page_load.js script to the beginning of your
application.js
, and usewindow.App.onPageLoad(...)
instead ofdocument.addEventListener('DOMContentLoaded', ...)
andjQuery(($) -> ...)
everywhere. -
Include your JavaScript like so:
<head> ... <%= javascript_include_tag 'application', # In debug mode Rails will serve individual script files. # With `async: true` these would be loaded out-of-order. # Disable `async` in assets debug mode: async: !Rails.application.config.assets.debug, defer: true, 'data-turbolinks-track': 'reload' %> </head>