Hey team, I’m trying to set up a turbo project but I’m hitting a snag with history navigation. I’ve distilled it down to this github project: GitHub - Haziba/turbo-not-navigating-backwards
The problem I’m having is that I have two views:
root /
<%= turbo_frame_tag ‘main-content’ do %>
<%= link_to “Comments”, comments_path, { data: { turbo_action: “advance” } } %>
<% end %>
comments/
<%= turbo_frame_tag ‘main-content’ do %>
…
<% end %>
And I output a random number at the top of the application layout to check for page reloads. Doing this I find that clicking the Comments link reloads the page content to the comments/ page and updates the URL without refreshing the application layout random number, which is what I want.
The problem is when I hit “Back” in my history then the URL updates to the root URL but the page content isn’t updated to the root view’s contents. Am I missing something? How do I get the page to bring in the root content when using the navigation, please?
Seems to be a bug caused by the absence of the data-turbo-tracked elements.
To work around it:
create a workaround layout for turbo_frames
<!-- app/views/layouts/turbo_frame.html.erb -->
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<!-- add any other data-turbo-track elements you have -->
</head>
<body>
<%= yield %>
</body>
</html>
override the render method of your controllers to use the new layout when it is a turbo frame request
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def render(*, **opts, &)
opts[:layout] = "layouts/turbo_frame" if turbo_frame_request?
super(*, **opts, &)
end
end
Note that this is a workaround and may cause other problems.