Skip to content

Instantly share code, notes, and snippets.

@ebouchut
Created October 17, 2014 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebouchut/77391c3bb85b19729034 to your computer and use it in GitHub Desktop.
Save ebouchut/77391c3bb85b19729034 to your computer and use it in GitHub Desktop.
Add a cache buster in a Rails controller action only for XHR requests
class MyController < ApplicationController
def action_xyz
if request.xhr?
set_cache_buster
end
render json: {what: 'ever'}
end
private
# See: http://stackoverflow.com/a/748646/386517
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment