Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ All notable changes to this project will be documented in this file.
- default `devise_modules` moved to folio config, so each app can set their own set. Default are `%i[database_authenticatable recoverable rememberable trackable invitable timeoutable lockable]
- added option `Rails.application.config.folio_cookie_consent_configuration[:keep_attached_after_accept]` (default false). Set it `true` if app allways display link to open cookies settings.

### Fixed

- set `Folio::Current.cache_key_base` only after request locale resolution and use a locale-independent cache key for current site lookup

## [6.5.1] - 2025-06-18

### Added
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/concerns/folio/application_controller_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Folio::ApplicationControllerBase
layout :current_site_based_layout

before_action :set_i18n_locale
before_action :set_folio_current_cache_key_base

before_action :set_cookies_for_log

Expand All @@ -36,6 +37,10 @@ def set_i18n_locale
end
end

def set_folio_current_cache_key_base
Folio::Current.cache_key_base = Rails.application.config.action_controller.perform_caching ? try(:cache_key_base) : nil
end

def default_url_options
{ only_path: true }
end
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/concerns/folio/set_current_request_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ def set_up_current_from_request
Folio::Current.setup!(request:,
user: current_user,
session:,
cache_key_base: Rails.application.config.action_controller.perform_caching ? try(:cache_key_base) : nil)
site_cache_key_base: Rails.application.config.action_controller.perform_caching ? folio_current_site_cache_key_base : nil)
end
end

def folio_current_site_cache_key_base
@folio_current_site_cache_key_base ||= Rails.cache.fetch(["Folio::Current.site_cache_key_base", request.host], expires_in: 30.seconds) do
[
ENV["CURRENT_RELEASE_COMMIT_HASH"],
request.host,
Folio::Site.maximum(:updated_at)&.to_f,
Folio::Site.count,
]
end
end
end
20 changes: 11 additions & 9 deletions app/models/folio/current.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Folio::Current < ActiveSupport::CurrentAttributes
:session,
:ability,
:cache_key_base,
:site_cache_key_base,
:skip_caching

SITE_KEYS = %i[
Expand All @@ -27,11 +28,11 @@ def to_h
attributes
end

def setup!(request:, user: nil, session: nil, cache_key_base: nil)
setup_folio_data(request:, user:, session:, cache_key_base:)
def setup!(request:, user: nil, session: nil, cache_key_base: nil, site_cache_key_base: cache_key_base)
setup_folio_data(request:, user:, session:, cache_key_base:, site_cache_key_base:)
end

def setup_folio_data(request:, user:, session:, cache_key_base:)
def setup_folio_data(request:, user:, session:, cache_key_base:, site_cache_key_base:)
self.host = request.host
self.request_id = request.uuid
self.user_agent = request.user_agent
Expand All @@ -41,8 +42,9 @@ def setup_folio_data(request:, user:, session:, cache_key_base:)
self.session = session

self.cache_key_base = cache_key_base
self.site_cache_key_base = site_cache_key_base

# keep the code that uses site under cache_key_base
# keep the code that uses site under site_cache_key_base
nillify_site_records unless self.class.site_matches_host?(site:, host:)
self.ability = Folio::Ability.new(user, site)
end
Expand All @@ -68,9 +70,9 @@ def self.site_matches_host?(site:, host:)
end

def self.cache_aware_get_site(host: nil)
if !skip_caching && cache_key_base
# cache_key_base should contain request.host
Rails.cache.fetch(["Folio::Current.cache_aware_get_site"] + cache_key_base, expires_in: Folio.expires_in) do
if !skip_caching && site_cache_key_base
# site_cache_key_base should contain request.host
Rails.cache.fetch(["Folio::Current.cache_aware_get_site"] + site_cache_key_base, expires_in: Folio.expires_in) do
get_site(host:)
end
else
Expand Down Expand Up @@ -101,8 +103,8 @@ def site
end

def self.cache_aware_get_main_site
if !skip_caching && cache_key_base
Rails.cache.fetch(["Folio::Current.cache_aware_get_main_site"] + cache_key_base, expires_in: Folio.expires_in) do
if !skip_caching && site_cache_key_base
Rails.cache.fetch(["Folio::Current.cache_aware_get_main_site"] + site_cache_key_base, expires_in: Folio.expires_in) do
get_main_site
end
else
Expand Down