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.

Return 304 on fresh cache (#14267)

Kacper Mękarskic989d018f701
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 69039e761e..54cb70d925 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
@@ -48,7 +48,12 @@ module Spree
response.headers['ETag'] = %("#{Digest::MD5.hexdigest(cache_key)}")
# Return false if client has fresh cache (304 Not Modified)
- !request.fresh?(response)
+ if request.fresh?(response)
+ head :not_modified
+ false
+ else
+ true
+ end
end
# Apply HTTP caching for a single resource (show actions)
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 602eae3856..9048a5d561 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
@@ -70,6 +70,16 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
end
+ it 'returns 304 Not Modified when the collection has not changed' do
+ get :index
+ etag = response.headers['ETag']
+
+ request.headers['If-None-Match'] = etag
+ get :index
+
+ expect(response).to have_http_status(:not_modified)
+ end
+
it 'changes ETag when a product is updated' do
get :index
original_etag = response.headers['ETag']