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.

Use `ar_lazy_preload` gem in storefront to reduce number of queries (#13477)

Damian Legawieca3472012bd80
diff --git a/core/app/models/spree/stock/quantifier.rb b/core/app/models/spree/stock/quantifier.rb
index a4b3df4239..b0c24a1e02 100644
--- a/core/app/models/spree/stock/quantifier.rb
+++ b/core/app/models/spree/stock/quantifier.rb
@@ -35,7 +35,7 @@ module Spree
private
def association_loaded?
- variant.association(:stock_items).loaded? && variant.association(:stock_locations).loaded?
+ variant.association(:stock_items).loaded?
end
def scope_to_location(collection)
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index 306e015f73..715887e1c1 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -167,7 +167,8 @@ module Spree
products = products_finder.
new(scope: storefront_products_scope, params: finder_params).
execute.
- includes(storefront_products_includes)
+ includes(storefront_products_includes).
+ preload_associations_lazily
default_per_page = Spree::Storefront::Config[:products_per_page]
per_page = params[:per_page].present? ? params[:per_page].to_i : default_per_page
@@ -180,19 +181,13 @@ module Spree
@pagy
end
+ # These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.
def storefront_products_includes
- [
- :prices_including_master,
- :option_types,
- :option_values,
- { master: [:images, :prices, :stock_items, :stock_locations, { stock_items: :stock_location }],
- variants: [
- :images, :prices, :option_values, :stock_items, :stock_locations,
- { option_values: :option_type, stock_items: :stock_location }
- ],
- taxons: [:taxonomy],
- taggings: [:tag] }
- ]
+ {
+ taxons: [],
+ master: [:images, :prices, { stock_items: :stock_location }],
+ variants: [:images, :prices, { stock_items: :stock_location }]
+ }
end
def redirect_unauthorized_access
diff --git a/storefront/app/helpers/spree/page_helper.rb b/storefront/app/helpers/spree/page_helper.rb
index 1ca41ad423..ddbe138312 100644
--- a/storefront/app/helpers/spree/page_helper.rb
+++ b/storefront/app/helpers/spree/page_helper.rb
@@ -12,7 +12,7 @@ module Spree
page ||= current_page
sections = current_page_preview.present? ? current_page_preview.sections : page.sections
- sections_html = sections.includes(section_includes).map do |section|
+ sections_html = sections.includes(section_includes).preload_associations_lazily.map do |section|
render_section(section, variables)
end.join.html_safe
@@ -98,9 +98,7 @@ module Spree
# @return [Array] the includes for the section
def section_includes
[
- :links, :rich_text_text,
- :rich_text_description, { asset_attachment: :blob },
- { blocks: [:rich_text_text, :links] }
+ { asset_attachment: :blob }
]
end
diff --git a/storefront/app/views/themes/default/spree/products/_product.html.erb b/storefront/app/views/themes/default/spree/products/_product.html.erb
index 5a843aeac6..649142e364 100644
--- a/storefront/app/views/themes/default/spree/products/_product.html.erb
+++ b/storefront/app/views/themes/default/spree/products/_product.html.erb
@@ -55,7 +55,7 @@
</div>
<% end %>
<div class="absolute right-1 top-1 z-10" data-plp-variant-picker-target="addToWishlist">
- <%= render 'spree/products/add_to_wishlist', variant: product.first_or_default_variant(current_currency) if product %>
+ <%= render 'spree/products/add_to_wishlist', variant: selected_variant %>
</div>
<%= render 'spree/products/color_swatches', product: product if product && (defined?(show_variant_picker) ? show_variant_picker : true) %>
</div>
diff --git a/storefront/lib/spree/storefront.rb b/storefront/lib/spree/storefront.rb
index 439567c37d..0a846321a5 100644
--- a/storefront/lib/spree/storefront.rb
+++ b/storefront/lib/spree/storefront.rb
@@ -11,6 +11,7 @@ require 'tailwindcss-rails'
require 'turbo-rails'
require 'inline_svg'
require 'pagy'
+require 'ar_lazy_preload'
require 'spree/storefront/engine'
require 'spree/core/partials'
diff --git a/storefront/spree_storefront.gemspec b/storefront/spree_storefront.gemspec
index 01c7c000f6..ff276d6df0 100644
--- a/storefront/spree_storefront.gemspec
+++ b/storefront/spree_storefront.gemspec
@@ -37,4 +37,5 @@ Gem::Specification.new do |s|
s.add_dependency 'tailwindcss-ruby'
s.add_dependency 'turbo-rails'
s.add_dependency 'pagy', '~> 43.0'
+ s.add_dependency 'ar_lazy_preload', '~> 2.0'
end