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.

Added simple caching of PLP filters and JSON LD code

Damian Legawiecec9592c63305
diff --git a/storefront/app/helpers/spree/filters_helper.rb b/storefront/app/helpers/spree/filters_helper.rb
index ee1739dc07..ce5d53a9e1 100644
--- a/storefront/app/helpers/spree/filters_helper.rb
+++ b/storefront/app/helpers/spree/filters_helper.rb
@@ -69,7 +69,7 @@ module Spree
end
def default_storefront_filter_values_scope
- Spree::OptionValue.for_products(products_for_filters_default_scope).distinct
+ @default_storefront_filter_values_scope ||= Spree::OptionValue.for_products(products_for_filters_default_scope).distinct
end
def storefront_filter_values_scope(filter_selected)
@@ -82,7 +82,11 @@ module Spree
def filter_values_for_filter(filter)
selected = single_option_filter_selected?(filter.name)
- filter.option_values.where(id: storefront_filter_values_scope(selected))
+ if filter.option_values.loaded?
+ filter.option_values.find_all { |option_value| storefront_filter_values_scope(selected).include?(option_value.id) }
+ else
+ filter.option_values.where(id: storefront_filter_values_scope(selected))
+ end
end
def storefront_products_for_taxon_filters
@@ -90,7 +94,7 @@ module Spree
end
def filter_taxon_ids
- @filter_taxon_ids ||= begin
+ @filter_taxon_ids ||= Rails.cache.fetch([spree_base_cache_key, storefront_products_for_taxon_filters, @taxon], expires_in: 1.day) do
scope = storefront_products_for_taxon_filters.joins(:classifications)
scope = scope.in_taxon(@taxon).unscope(:order) if @taxon.present?
scope.distinct.pluck(:taxon_id)
@@ -106,15 +110,17 @@ module Spree
end
def product_taxon_aggregations
- @product_taxon_aggregations ||= current_store.taxons.
- joins(:classifications).
- where("#{Spree::Classification.table_name}.product_id" => storefront_products_for_taxon_filters.ids).
+ @product_taxon_aggregations ||= Rails.cache.fetch([spree_base_cache_key, storefront_products_for_taxon_filters, current_store.taxons], expires_in: 1.day) do
+ current_store.taxons.
+ joins(:classifications).
+ where("#{Spree::Classification.table_name}.product_id" => storefront_products_for_taxon_filters.ids).
group(
"#{Spree::Taxon.table_name}.id",
"#{Spree::Classification.table_name}.product_id"
).
- unscope(:order).
- count("#{Spree::Classification.table_name}.product_id")
+ unscope(:order).
+ count("#{Spree::Classification.table_name}.product_id")
+ end
end
def products_count_for_taxon(taxon)
diff --git a/storefront/app/views/themes/default/spree/products/_json_ld_list.html.erb b/storefront/app/views/themes/default/spree/products/_json_ld_list.html.erb
index 2042d541fc..c1d971ce7a 100644
--- a/storefront/app/views/themes/default/spree/products/_json_ld_list.html.erb
+++ b/storefront/app/views/themes/default/spree/products/_json_ld_list.html.erb
@@ -1,7 +1,9 @@
-<script type="application/ld+json" data-test-id="product-list-json-ld">
- {
- "@context": "https://schema.org",
- "@type": "ItemList",
- "itemListElement": <%= product_list_json_ld_elements(products.to_a.pluck(:slug)).to_json.html_safe %>
- }
-</script>
+<% cache [spree_base_cache_key, products,'json-ld-list'], expires_in: 1.day do %>
+ <script type="application/ld+json" data-test-id="product-list-json-ld">
+ {
+ "@context": "https://schema.org",
+ "@type": "ItemList",
+ "itemListElement": <%= product_list_json_ld_elements(products.to_a.pluck(:slug)).to_json.html_safe %>
+ }
+ </script>
+<% end %>
\ No newline at end of file