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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreecurrent_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_headersif 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'elseresponse.headers['Cache-Control'] = 'private, no-store'end@@ -67,14 +70,21 @@ module Spreeexpires_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+ )endprivate# 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, channeldef collection_cache_key(collection)# For ActiveRecord collections use updated_at, for plain arrays use store's updated_at as proxylatest_updated_at = if collection.first&.respond_to?(:updated_at)@@ -92,11 +102,22 @@ module Spreeparams[: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+ endendendenddiff --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.rbindex 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 dolet!(:product) { create(:product, status: 'active') }let!(:product2) { create(:product, status: 'active') }+ let!(:pos_channel) { create(:channel, store: store, code: 'pos', name: 'POS') }before dorequest.headers['X-Spree-Api-Key'] = api_key.token@@ -18,7 +19,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doit 'sets Vary header for CDN caching' doget :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')endend@@ -136,6 +137,35 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doexpect(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+ endendcontext 'for authenticated users' do@@ -192,6 +222,34 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doexpect(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+ endendcontext 'for authenticated users' do