According to the Turbo documentation here: https://turbo.hotwired.dev/handbook/building#working-with-script-elements adding JS files into a page’s <head>
that are not present on the current page should cause a Turbo to merge the <head>
and load the code. Quoting the documentation:
When you navigate to a new page, Turbo Drive looks for any
elements in the new page’s which aren’t present on the current
page. Then it appends them to the current where they’re loaded
and evaluated by the browser. You can use this to load additional
JavaScript files on-demand.
We add these files in the views like:
<%= content_for :head do %>
<%= javascript_include_tag 'example', 'data-turbo-track': 'reload' %>
<% end %>
However when doing this Turbo always does a full page refresh. This seems to conflict with what the documentation says around merging the heads and loading the files as needed. We have this approach in a lot of our views which makes Turbo almost pointless as it means the page is being fully reloaded between views. The fingerprints of the files are the same so in theory it shouldn’t be doing a full re-load (it doesn’t need to load new JS)