Skip to content

Instantly share code, notes, and snippets.

@alekseyl
Last active March 24, 2017 15:27
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 alekseyl/ba89967fe357f7249cd1c415dbd5a060 to your computer and use it in GitHub Desktop.
Save alekseyl/ba89967fe357f7249cd1c415dbd5a060 to your computer and use it in GitHub Desktop.
#in controller:
before_action :resourceful_cache_headers
before_action :resourceless_cache_headers
before_action :skip_session
def skip_session
# if you need to deliver flash, or authencity token, or you explicitly need session
request.session_options[:skip] = true unless you_need_session
end
def resourceless_cache_headers
# if you serving resourceless pages, you must specify template explicitly, or
# you'll get yourself default etag from rack, meaning actually not so conditional GET.
fresh_when( nil, template: "#{controller_path}/#{action_name}" )
end
def resourceful_cache_headers
# if resource isn't nil, the template: "#{controller_path}/#{action_name}", may be ommited
fresh_when( @resource )
end
#in controller for actions from forms which can be send from cached pages:
skip_before_action :verify_authenticity_token, only: [:create]
# also may be better to remove csrf token from layout of cached pages
# don't forget than any kind of set cookies will pass cache,
# be aware that some gems can do this for you, like, for instance, rack-mini-profiler,
# It's reasonable NOT to interfere Set-Cookie on NGINX layer,
# because Nginx can't be sure intended or not this behaviour
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment