Rails flash notice sticks in Safari? #13331

Closed
joshukraine opened this Issue Dec 15, 2013 · 20 comments

Projects

None yet

8 participants

@joshukraine

I have encountered a scenario where flash notices will not clear in Safari. Using the exact same code, the flash notices clear as expected in both FireFox and Chrome. I have posted this issue to several forums, and as yet no one has been able to find a solution.

The issue is documented in detail in this forum post:
https://railsforum.com/topic/989-rails-flash-notice-wont-go-away-in-safari/

The post includes relevant code, log output, and even links to two screen recordings which I made to demonstrate the issue. If you'd like a preview before wading through the code, check out the screen recordings on YouTube here:

Part 1 - http://www.youtube.com/watch?v=S-kwbx309LQ
Part 2 - http://www.youtube.com/watch?v=qLLqKuf0q1Q

Thanks in advance for any advice/assistance!

@luke-gru

Hi,

This is a weird issue, and I think it may have to do with one of the gems you're using. Can you post the contents of your Gemfile? In particular, it may have to do with Turbolinks, but that's just a guess.

For what it's worth, the person you were talking to in that railsforum exchange was giving you wrong information. Flashes are cleared automatically after a single page view, and using flash.now should only be done for rendering a page, never redirecting.

@joshukraine

Hey,

Yes, I thought about the gem possibility. I actually address that in the second video. Have you seen that? I essentially remove all but the most essential gems, and also all CSS and JS. Same issue.

And yes, the fellow helping me on the forum realized his mistake further into the discussion and corrected it. We worked through quite a bit of code and various debugging attempts, but to no avail.

Lastly, I am not using Turbolinks.

I appreciate your input!

@joshukraine

Oh yes, and here's the gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.16'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'jquery-rails', '2.0.2'
gem 'simple_form', '~> 2.1.0'
gem 'country_select', '~> 1.1.3'
gem 'pg' #, '0.12.2'
gem 'font-awesome-sass'
gem 'pg_search'
gem 'squeel'
gem 'unicorn'
gem 'capistrano'

gem 'simplecov', :require => false, :group => :test

group :development, :test do
  gem 'rspec-rails', '~> 2.11.0'
  gem 'guard-rspec', '1.2.1'
  gem 'spork', '~> 1.0rc'
  gem 'spork-rails'
  gem 'childprocess', '0.3.6'
  gem 'annotate', '2.5.0'
  gem 'meta_request'
  gem 'foreman'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'rails_best_practices'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.5'
  gem 'coffee-rails', '~> 3.2.2'
  gem 'uglifier',     '>= 1.2.3'
  gem 'foundation-rails', '~> 5.0.2.0'
end

group :test do
  gem 'capybara', '~> 1.1.2'
  gem 'factory_girl_rails', '4.1.0'
  gem 'cucumber-rails', '1.2.1', :require => false
  gem 'database_cleaner', '0.7.0'
  gem 'launchy', '2.1.0'
  gem 'rb-fsevent', '0.9.1', :require => false
  gem 'growl', '1.0.3'
end

group :production do
  gem 'rails_12factor'
end

ruby '2.0.0'
@luke-gru

Yes, sorry, I only realized after that there was a 2nd page there. After reading it, I think I might know what's going on... but I might not 😄

In your logs, see how request.referer is blank when you click on the link to go back to the home page in Safari? I don't know why this is, because it should be set when you click on a link, but it appears to be blank.

One thing you could do is something like

session[:return_to] ||= request.referer
back = session.delete(:return_to)
redirect_to(back.presence || default_back)

You'd have to define default_back to return a valid url or path for the case where referer is blank. Let me know if this works!

@joshukraine

OK, if I'm understanding you correctly then default_back needs to be set to something like 'root_path', yes? I tried the code below, but the results were the same.

def search
  session[:return_to] ||= request.referer
  back = session.delete(:return_to)
  default_back = root_path
  if !params[:search].numeric?
    flash[:error] = %Q[<i class="fa fa-times fa-fw"></i> Please enter a numeric student ID.].html_safe

    logger.debug("@@@@ flash: #{flash}")
    logger.debug("@@@@ session: #{session}")
    logger.debug("@@@@ request.referer: #{request.referer}")
    # redirect_to session.delete(:return_to)

    redirect_to(back.presence || default_back)
.
.
.
@rafaelfranca
Ruby on Rails member

I'm pretty sure this is a bug in Safari that doesn't send Set-Cookies in redirects. For this reason you request.referer is empty and the flash is not being cleaned.

I found this reference in the internet http://stackoverflow.com/questions/1144894/safari-doesnt-set-cookie-but-ie-ff-does

Could you observe all the HTTP responses/requests in the different browsers?

Also it would be great if you create an example application where we could reproduce the issue.

@luke-gru

@rafaelfranca could be right, but from my understanding when watching the YT videos, the cookie is being set from the redirect, but it is not being cleared properly. Or, it is being cleared but Safari is showing cached content, maybe?

I searched a bit for related problems, and it seems like there are changes in new Safari versions with how they deal with caching 302 redirects. See https://discussions.apple.com/thread/5531657

You could test if this is the problem by generating unique urls every time you redirect back. For instance:

redirect_to root_path(:digest => rand(100000000).to_s)

I think Rafael is probably right, but either way I can't test it because I don't have a mac 😄

@joshukraine

Test app finished and pushed to GitHub. I have successfully reproduced this bug with the test app. Steps to reproduce are in README.md in the repo.

Code here: https://github.com/joshukraine/safari-flash-bug

Live on Heroku here: http://protected-ocean-3174.herokuapp.com/

@acapilleri

@joshukraine I can't reproduce this using your heroku app with safari 7.0 (9537.71) , I think that it's a safari bug, which is your version?

@joshukraine

@acapilleri I am using Safari 7.0 (9537.71). Also Safari on iOS 7. Did you try multiple times? It often won't show up until you've done 3 or 4 searches.

@joshukraine

UPDATE: I just tested this on a 2006 iMac running Mac OS X 10.6.8 Snow Leopard and Safari 5.1.9 (6534.59.8). I was not able to reproduce the issue with these specs. Seems like another indicator that this is a Safari 7 issue.

@acapilleri

👍

@dmathieu

Since this seems to be a safari issue and not a rails one, I'm going to close this.
Please comment here if you're able to provide an indicator of this being a rails issue.

@dmathieu dmathieu closed this Dec 16, 2013
@acapilleri

@joshukraine I think that you should close this issue, if it is not a rails issue.
Thanks

@acapilleri

👯

@joshukraine

Thanks everyone for your help. Here's hoping Apple will fix this in a future Safari release.

@chancancode
Ruby on Rails member

@joshukraine you can try https://bugreport.apple.com/‎ (if you can share it via http://www.openradar.me/ afterwards, even better 👍)

@joshukraine

@chancancode Thanks for the suggestion. Just so I'm clear, how would you explain this bug from a Safari point of view? I know how to describe what this looks like in Rails, but for the Apple folks, I should tell that Safari is not ______? Thanks!

@elsurudo

As this is kind of a big deal, can anyone suggest a workaround?

@tboulogne

hello,

I was stuck with this flash problem on Safari and i discover this : unobtrusive_flash.

Seems to work better. I keep it in test.

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