How to prevent browser page caching in Rails
I finally figured this out - http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/ in application_controller.rb
.
After Ruby on Rails 5:
class ApplicationController < ActionController::Base
before_action :set_cache_headers
private
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end
Ruby on Rails 4 and older versions:
class ApplicationController < ActionController::Base
before_filter :set_cache_headers
private
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end
Rails ( set_no_cache method) Cannot disable browser caching in Safari and Opera
I faced the same problem and found a good solution and I blogged it to
http://www.fordevs.com/2011/10/how-to-prevent-browser-from-caching-a-page-in-rails.html
To add ‘no-cache’, add the following lines @ the application_controller.rb file
before_filter :set_no_cache
and the function
def set_no_cache
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
Rails 2.3 - How to prevent a browser from caching a partial?
That's not a browser cache, but Rails. When it runs in production environment - it caches all ERB files, so the simpliest thing is to restart your Rails application. But I think you can make your task without changing ERB template file. Can you provide more info about your needs?
Rails 6. A page with outdated data is shown after the browser opening
I've found an answer here.
class ApplicationController < ActionController::Base
before_action :set_cache_headers
private
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end
The solution is 12 years old but it's still workable.
Stop images from caching in Rails and browser?
It's probably not the best way, but I've solved this problem in the past by simply appending a timestamp to the image URL using JavaScript:
http://www.mysite.com/img/some_image.jpg?timestamp=12342312412
Next time it loads, the timestamp is set to the current time and the URL is different, so the browser does a GET for the image instead of using the cached version.
Rails Caching a Page Improperly - How to Stop?
WebKit in particular has an aggressive page caching strategy for handling exactly the case you're describing (clicking a link and then immediately clicking the back button). The idea is to make the back action happen almost instantaneously by caching not just the resources but also the DOM and other state of the page. You can read about it in these two articles:
- WebKit Page Cache I — The Basics
- WebKit Page Cache II — The
unload
Event
You may be able to use a combination of the load
/unload
and pageshow
/pagehide
events to accomplish what you need.
I'm not sure if IE implements something similar to WebKit, but maybe this will fix it too.
Related Topics
How to Make --No-Ri --No-Rdoc the Default For Gem Install
Ruby: What Does 'Require: False' in Gemfile Mean
Ruby Function to Remove All White Spaces
What Does a Double * (Splat) Operator Do
Nokogiri Installation Fails -Libxml2 Is Missing
Rails: Activerecord Query Based on Association Value
Open an Io Stream from a Local File or Url
How to Find Out the Current Route in Rails
What Does the (Unary) * Operator Do in This Ruby Code
How to Get a Random Number in Ruby
Rails :Dependent =≫ :Destroy VS :Dependent =≫ :Delete_All
How to Make a Http Request Using Ruby on Rails
Can't Install Rmagick 2.13.1. Can't Find Magickwand.H.
Rails: What's a Good Way to Validate Links (Urls)
Ruby 1.9: How to Properly Upcase & Downcase Multibyte Strings