docbrown/app/controllers/concerns/edge_cache_safety_check.rb
Jacob Herrington 5afb4d9a24
[15-Minute Fix] Refactor code smells (#13347)
* Remove duplicate method calls

* DRY up duplicated code

* Address code smells in concerns

* Update based on PR feedback
2021-04-21 14:52:32 -05:00

18 lines
781 B
Ruby

# Included in Application Controller to monkey patch Devise::Controllers::Helpers#current_user
# https://github.com/heartcombo/devise/blob/5d5636f03ac19e8188d99c044d4b5e90124313af/lib/devise/controllers/helpers.rb#L103
module EdgeCacheSafetyCheck
extend ActiveSupport::Concern
CANNOT_USE_CURRENT_USER = "You may not use current_user in this cached code path.".freeze
def current_user
# In production, current_user will cause a cache leak if it's placed within an edge-cached code path.
# More information here:
# https://docs.forem.com/technical-overview/architecture/#we-cache-many-content-pages-on-the-edge
return super unless RequestStore.store[:edge_caching_in_place]
return if session_current_user_id.blank?
CANNOT_USE_CURRENT_USER
end
end