How to Prevent Browser Page Caching in Rails

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



Leave a reply



Submit