← Back to Upcase

Fixing the back button for shopping cart apps using response headers


(Justin Gordon) #1

I had the issue of the browser caching the pages, not sending anything to the server, when the user hit the back button. This is certainly problematic with a shopping cart application! I found this old article, and I the technique seems to work perfectly. Any downsides to doing this? Any better way to doing this?

Here’s the article: How to prevent browsers from caching a page in Rails, from April 14th, 2009

This took me forever to figure out, so I hope I’ll be able to save someone a few hours of annoyance someday.

in application_controller.rb…

  before_filter :set_cache_buster
  def set_cache_buster
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  end