Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Include channel in guest Store API cache identity (F5)

Damian Legawieced77229ee447
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/http_caching.rb b/spree/api/app/controllers/concerns/spree/api/v3/http_caching.rb
index 54cb70d925..95e1af1f5f 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/http_caching.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/http_caching.rb
@@ -22,10 +22,13 @@ module Spree
current_user.nil?
end
- # Set Vary headers to ensure proper CDN caching by currency/locale
+ # Set Vary headers to ensure proper CDN caching by currency/locale/channel.
+ # X-Spree-Channel is included because the resolved channel changes the
+ # serialized body (price hiding + channel-scoped visibility), so a shared
+ # cache must not serve one channel's guest response to another channel.
def set_vary_headers
if guest_user?
- response.headers['Vary'] = 'Accept, x-spree-currency, x-spree-locale'
+ response.headers['Vary'] = 'Accept, x-spree-currency, x-spree-locale, x-spree-channel'
else
response.headers['Cache-Control'] = 'private, no-store'
end
@@ -67,14 +70,21 @@ module Spree
expires_in expires_in, public: true
- # Use Rails' stale? which handles ETag and Last-Modified
- stale?(resource, public: true)
+ # Use Rails' stale? which handles ETag and Last-Modified. The channel
+ # is folded into the ETag so a shared cache never serves one channel's
+ # guest response to another; Last-Modified mirrors the resource's own
+ # timestamp as before.
+ stale?(
+ etag: [resource, cache_channel_fragment],
+ last_modified: resource.try(:updated_at),
+ public: true
+ )
end
private
# Build a cache key for a collection
- # Includes: latest updated_at, total count, query params, pagination, expand, currency, locale
+ # Includes: latest updated_at, total count, query params, pagination, expand, currency, locale, channel
def collection_cache_key(collection)
# For ActiveRecord collections use updated_at, for plain arrays use store's updated_at as proxy
latest_updated_at = if collection.first&.respond_to?(:updated_at)
@@ -92,11 +102,22 @@ module Spree
params[:page],
params[:limit],
current_currency,
- current_locale
+ current_locale,
+ cache_channel_fragment
]
parts.compact.join('/')
end
+
+ # Identifies the resolved channel for cache-key purposes. The channel
+ # changes the serialized body (price hiding + channel-scoped visibility),
+ # so it must be part of the guest cache identity. Uses the same
+ # +current_channel+ source of truth as serialization/gating.
+ def cache_channel_fragment
+ return nil unless respond_to?(:current_channel, true)
+
+ current_channel&.id
+ end
end
end
end
diff --git a/spree/api/spec/controllers/concerns/spree/api/v3/http_caching_spec.rb b/spree/api/spec/controllers/concerns/spree/api/v3/http_caching_spec.rb
index 9048a5d561..2fbab14205 100644
--- a/spree/api/spec/controllers/concerns/spree/api/v3/http_caching_spec.rb
+++ b/spree/api/spec/controllers/concerns/spree/api/v3/http_caching_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
let!(:product) { create(:product, status: 'active') }
let!(:product2) { create(:product, status: 'active') }
+ let!(:pos_channel) { create(:channel, store: store, code: 'pos', name: 'POS') }
before do
request.headers['X-Spree-Api-Key'] = api_key.token
@@ -18,7 +19,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
it 'sets Vary header for CDN caching' do
get :index
- expect(response.headers['Vary']).to eq('Accept, x-spree-currency, x-spree-locale')
+ expect(response.headers['Vary']).to eq('Accept, x-spree-currency, x-spree-locale, x-spree-channel')
end
end
@@ -136,6 +137,35 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(eur_etag).not_to eq(original_etag)
end
+
+ # Publish the same products on both channels so the identical collection
+ # is served either way — isolating the channel as the sole cache
+ # dimension that differs (guards against a shared CDN cross-serving).
+ context 'when the same products are visible on two channels' do
+ before { pos_channel.add_products([product.id, product2.id]) }
+
+ it 'changes ETag when the resolved channel changes' do
+ get :index
+ default_channel_etag = response.headers['ETag']
+
+ request.headers['x-spree-channel'] = 'pos'
+ get :index
+ pos_channel_etag = response.headers['ETag']
+
+ expect(pos_channel_etag).not_to eq(default_channel_etag)
+ end
+
+ it 'does not serve a 304 across channels for the same fresh ETag' do
+ get :index
+ default_channel_etag = response.headers['ETag']
+
+ request.headers['x-spree-channel'] = 'pos'
+ request.headers['If-None-Match'] = default_channel_etag
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
end
context 'for authenticated users' do
@@ -192,6 +222,34 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
end
+
+ # Publish the same product on both channels so it resolves either way —
+ # isolating the channel as the sole cache dimension that differs.
+ context 'when the product is visible on two channels' do
+ before { pos_channel.add_products([product.id]) }
+
+ it 'changes ETag when the resolved channel changes' do
+ get :show, params: { id: product.prefixed_id }
+ default_channel_etag = response.headers['ETag']
+
+ request.headers['x-spree-channel'] = 'pos'
+ get :show, params: { id: product.prefixed_id }
+ pos_channel_etag = response.headers['ETag']
+
+ expect(pos_channel_etag).not_to eq(default_channel_etag)
+ end
+
+ it 'does not serve a 304 across channels for the same fresh ETag' do
+ get :show, params: { id: product.prefixed_id }
+ default_channel_etag = response.headers['ETag']
+
+ request.headers['x-spree-channel'] = 'pos'
+ request.headers['If-None-Match'] = default_channel_etag
+ get :show, params: { id: product.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
end
context 'for authenticated users' do