Last active
March 24, 2017 15:27
-
-
Save alekseyl/ba89967fe357f7249cd1c415dbd5a060 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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