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

Using Turbo Drive with data-turbo-track="reload" causes the destination page to be rendered twice #114

Open
wout opened this issue Jan 16, 2021 · 6 comments

Comments

@wout
Copy link

wout commented Jan 16, 2021

My Rails app has two layouts:

  • auth (sign up, sign in, password reset, ...)
  • admin (the app itself)

Both layouts also have their own asset packs, each marked with data-turbo-track="reload". The switch of layout happens after signing in or signing out. Because of the reload, the page is rendered twice. Once as TURBO_STREAM and a second time as HTML:

...
  ↳ app/controllers/auth/sign_outs_controller.rb:7:in `create'
Redirected to http://localhost:3000/sign_in
Completed 303 See Other in 46ms (ActiveRecord: 10.6ms | Allocations: 22060)

Started GET "/sign_in" for 172.21.0.1 at 2021-01-16 14:23:41 +0000
Processing by Auth::SignInsController#new as TURBO_STREAM
  Rendering layout layouts/auth.html.erb
  Rendering auth/sign_ins/new.html.erb within layouts/auth
  Rendered auth/sign_ins/new.html.erb within layouts/auth (Duration: 1.4ms | Allocations: 664)
  Rendered application/_favicon.html.erb (Duration: 4.1ms | Allocations: 3736)
  Rendered layout layouts/auth.html.erb (Duration: 11.6ms | Allocations: 6827)
Completed 200 OK in 14ms (Views: 12.3ms | ActiveRecord: 0.0ms | Allocations: 8005)

Started GET "/sign_in" for 172.21.0.1 at 2021-01-16 14:23:41 +0000
Processing by Auth::SignInsController#new as HTML
  Rendering layout layouts/auth.html.erb
  Rendering auth/sign_ins/new.html.erb within layouts/auth
  Rendered auth/sign_ins/new.html.erb within layouts/auth (Duration: 1.6ms | Allocations: 585)
  Rendered application/_favicon.html.erb (Duration: 4.3ms | Allocations: 3666)
  Rendered layout layouts/auth.html.erb (Duration: 11.1ms | Allocations: 5656)
Completed 200 OK in 13ms (Views: 11.6ms | ActiveRecord: 0.0ms | Allocations: 6106)

Generally, that's not too big of an issue. Except for the fact that flash messages are consumed by the first request, and will therefore not be rendered in the second request.

I have now worked around the issue by creating a single asset pack for both layouts. But I wonder if there is a way to avoid the second page render.

@Kulgar
Copy link

Kulgar commented Oct 18, 2021

Same problem here, and this is a bit more important as I have a button that "generates a data" and the data gets generated twice (because the request is made twice).

If I remove the turbo-track from the second script, I don't have the problem anymore but the JS doesn't load.

And... I would prefer to keep packs separated.

Any advice? ^^'

@as181920
Copy link

as181920 commented Oct 29, 2021

I temporarily use meta tag to load page js(trigger page reload)

<% content_for :head_additions do %>
  <meta name="turbo-visit-control" content="reload">
  <%= javascript_pack_tag "page_specific_js" %>
<% end %>  

@Kulgar
Copy link

Kulgar commented Oct 29, 2021

That is actually a nice catch!
On my part I found that using a "button_to" instead of a "link_to" solved my problem (the POST request doesn't get executed twice). This makes more sense actually for my case (to use button_to) as I create a data when a user clicks the button.

@janko
Copy link

janko commented Nov 29, 2022

We're experiencing the same issue. In our app we're gradually migrating to a new layout, which uses different assets. If we submit the form on a page with the old layout, and that form redirects to a page with a new layout, the flash message set for the redirect will not appear on the final page.

I guess we could work around this by temporarily disabling Turbo for those form submits that go between layouts, until we finish the migration.

@gap777
Copy link

gap777 commented Mar 22, 2023

I think I'm experiencing this, in a slightly different scenario...

  • GET from within a turbo frame
  • Server detects error condition, and instead of responding with turbo-frame, redirects WITH flash alert. Redirect is to page having meta name="turbo-visit-control" content="reload"
  • Page is fetched, containing alert, and discarded.
  • Page is fetched again. No flash.

Any work arounds to prevent this double-pump?

@bluestorm21
Copy link

1 year later and this issue still not resolved :/

@seanpdoyle just curious... any reason why page reload is required? why not just create a script element and replace the old one? (isn't the hash string appended to the file's-name sufficient to for the browser to renew the cache?)

I will try to find a solution if I can.

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

6 participants