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

Redirect to referrer instead of root on locale set #743

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cmrd-senya
Copy link

With this change when you change the locale you are redirected to the same page you've been before instead of root of the Rails application.

@cmrd-senya
Copy link
Author

We just found out that http referrer is being spoofed on some configuration and can't be used reliably to redirect back. Is it a good idea to use a parameter for the LocaleController#set passing return URL?

@bbonislawski
Copy link
Contributor

Hello @cmrd-senya ,
thanks for your contribution. It's best to use redirect_back method from rails instead of implementing it yourself. Since we support both rails 4.2 and rails 5.x we have to do a little 'hack' like this:

    if Rails.gem_version >= Gem::Version.new('5.x')
      redirect_back(fallback_location: root_path, **params)
    else
      redirect_to :back
    end

Can you try this solution?

@cmrd-senya
Copy link
Author

cmrd-senya commented Nov 4, 2017

redirect_back uses HTTP_REFERRER too, and the problem with this header is that some web browsers like GNU IceCat spoof this header by default and therefore the feature won't work for users of such browsers. What do you think, if instead (or in addition to) we add a hidden input field with current page location? We can do something like this:

diff --git a/app/views/spree/locale/index.html.erb b/app/views/spree/locale/index.html.erb
index 0dd5e3e..a85b531 100644
--- a/app/views/spree/locale/index.html.erb
+++ b/app/views/spree/locale/index.html.erb
@@ -8,6 +8,12 @@
         <%= select_tag(:switch_to_locale,
           options_for_select(available_locales_options, I18n.locale),
           class: 'form-control') %>
+
+        <%= hidden_field_tag :original_url %>
+        <script>
+          var input = document.getElementById("original_url");
+          input.value = location.href;
+        </script>
         <noscript><%= submit_tag %></noscript>
       </div>
     <% end %>

And then use this input value as a URL to redirect back.

If you're okay with such approach, tell me and I can implement it.

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

Successfully merging this pull request may close these issues.

2 participants