Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbo Drive does full reload when adding on demand JS to the new page with turbo track reload #1326

Open
iamdriz opened this issue Oct 14, 2024 · 0 comments

Comments

@iamdriz
Copy link

iamdriz commented Oct 14, 2024

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 Turbo Drive to merge the <head> and load the JS code on demand. 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.

Our head looks like:

<head>
  <%= javascript_include_tag 'application', 'data-turbo-track': 'reload' %>
  <%= yield :head %>
</head>

And we add these additional JS files in a view like:

<%= content_for :head do %>
  <%= javascript_include_tag 'page_specific_code', 'data-turbo-track': 'reload' %>
<% end %>

However Turbo always does a full page reload when it encounters these types of views.

To confirm this is happening, we have the following in our application.js:

$(document).on("turbo:reload", function () {
  alert("TURBO FULL RELOAD!");
});

This seems to conflict with what the documentation claims around merging the heads and adding the files as on demand and only do a full reload if the fingerprints have changed. We have this approach in a lot of our views which makes using Turbo feel almost pointless as it means the page is being fully reloaded between views (and we don't want to have to rebuild our application setup to make Turbo work with it).

We tried removing the 'data-turbo-track': 'reload' from the page_specific_code and it stopped doing the full reload... however the fingerprints of the files haven't changed so the turbo-track-reload shouldn't be causing it to happen. Perhaps it's not clear in the documentation but it almost sounds like you should only use the reload on the global bundled files that are used across the app and anything that's page specific then you shouldn't otherwise it just does a full refresh. Is that correct? Or is something else happening here that's considered a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant