Force Page to Reload on Browser Back in Rails

On my current project we do some heavy modification of the DOM via JavaScript. This causes some issues when the users navigate via the back button – quite often the page will not contain the changes the user made, or the information they see will be out of date. (Our users are also unfamiliar with websites and tend to find this confusing)

To fix this we can force the browser to refresh the page when the back button is used. This is the Rails solution, but the headers should be the same independent of the framework used.

class ApplicationController < ActionController::Base
  before_filter :set_cache_headers
  
  private

  def set_cache_headers
    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
end

Be careful how you use this, since it can be very annoying for the user if the page is reloaded unnecessarily. Happy coding.