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.

Add last_modified and etag helper methods for Spree::ProductsController

Nathaniel Nande90365e79748d
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index ce4c951f1b..dac7e71e3b 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -12,16 +12,7 @@ module Spree
@searcher = build_searcher(params.merge(include_images: true, current_store_id: current_store.id))
@products = @searcher.retrieve_products
- last_modified = @products.maximum(:updated_at)&.utc if @products.respond_to?(:maximum)
-
- etag = [
- store_etag,
- last_modified&.to_i,
- available_option_types_cache_key,
- filtering_params_cache_key
- ]
-
- fresh_when etag: etag, last_modified: last_modified, public: true
+ fresh_when etag: etag_index, last_modified: last_modified_index, public: true
end
def show
@@ -29,7 +20,7 @@ module Spree
@taxon = params[:taxon_id].present? ? Spree::Taxon.find(params[:taxon_id]) : @product.taxons.first
- if stale?(etag: product_etag, last_modified: @product.updated_at.utc, public: true)
+ if stale?(etag: etag_show, last_modified: last_modified_show, public: true)
@product_summary = Spree::ProductSummaryPresenter.new(@product).call
@product_properties = @product.product_properties.includes(:property)
@product_price = @product.price_in(current_currency).amount
@@ -96,7 +87,16 @@ module Spree
end
end
- def product_etag
+ def etag_index
+ [
+ store_etag,
+ last_modified_index,
+ available_option_types_cache_key,
+ filtering_params_cache_key
+ ]
+ end
+
+ def etag_show
[
store_etag,
@product,
@@ -105,5 +105,21 @@ module Spree
@product.possible_promotions.maximum(:updated_at),
]
end
+
+ alias product_etag etag_show
+
+ def last_modified_index
+ products_last_modified = @products.maximum(:updated_at)&.utc if @products.respond_to?(:maximum)
+ current_store_last_modified = current_store.updated_at.utc
+
+ [products_last_modified, current_store_last_modified].compact.max
+ end
+
+ def last_modified_show
+ product_last_modified = @product.updated_at.utc
+ current_store_last_modified = current_store.updated_at.utc
+
+ [product_last_modified, current_store_last_modified].compact.max
+ end
end
end