Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Double request when changing layout in Rails 5 #204

Closed
flamontagne opened this issue Nov 4, 2016 · 10 comments
Closed

Double request when changing layout in Rails 5 #204

flamontagne opened this issue Nov 4, 2016 · 10 comments

Comments

@flamontagne
Copy link

Whenever I click on a link or do a server redirect where the layout changes (for example going from the 'admin' layout to the 'application' layout), there are two requests that occur on the server.

Both layouts have their own js manifest file with the //= require turbolinks directive.

Any idea?

@packagethief
Copy link
Member

I'm guessing that Turbolinks is detecting a change in tracked assets when you switch layouts. It performs a visit, decides that assets have changed, and issues a full reload: two requests.

Do your layouts have tracked asset tags that differ?

@flamontagne
Copy link
Author

You are right. That's exactly what appears to be happening. Thanks!

In application layout, I have
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'

and in admin layout, I have
= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload'
= stylesheet_link_tag 'admin', media: 'all', 'data-turbolinks-track': 'reload'

Just to test, I tried including the 'application' manifests in the admin layout and the double requests stopped immediately.

It's not a huge problem, as this only happens when switching from one layout to the other. However if i do a server redirect via redirect_to and the layout changed, the double request invalidates any flash message I had set in the controller. In fact this is exactly how I noticed that double requests were happening.

@matt297
Copy link

matt297 commented Feb 14, 2017

Our layout setup is identical to what @flamontagne described, and we were able to prevent turbolinks:load from firing twice when manually navigating between layouts by adding data: { turbolinks: false } to the link.

i.e. On a page using the application.html layout...

<%= link_to 'Admin', admin_path, data: { turbolinks: false } %>

While it still doesn't address the concern with flash messages, it helped us work around the double-firing issue in the meantime.

@fedegos
Copy link

fedegos commented Feb 14, 2018

Any solution to this issue? I'm having the same problem, the double request clears the flash notice.

@pensebien
Copy link

pensebien commented Aug 28, 2018

For now I have decided to setup another controller for my redirect links if I really want the flash message to show. I hope I could see a better solution. I have decided to keep the flash using flash.keep

@fedegos
Copy link

fedegos commented Aug 28, 2018

What about using request.head? in a controller decorator/ActionController base class to preserve the flash if the request is head only?

@pensebien
Copy link

@fedegos I do not understand how you solve the problem?

@fedegos
Copy link

fedegos commented Sep 3, 2018

I haven't tried it yet. I will see if that may work as a workaround.

@domchristie
Copy link
Collaborator

To get the best out of Turbolinks, it is best to avoid tracked asset changes as much as possible. If that's unavoidable, then try to steer-clear of setting flash messages which may appear on different layouts 😬 And if that's unavoidable, you might have to get creative with a solution to solve your use-case. Hope that helps!

@ghost
Copy link

ghost commented May 8, 2019

Did anyone find a workable solution? It's quite a common use-case to have a frontend controller redirect to an admin controller after a successful sign in — whilst setting a flash notice to 'Successfully signed in' for example. I can't find a way around this.

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

No branches or pull requests

6 participants