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.

Introduce fragment caching to Featured Taxon page section (#13024)

Damian Legawiec6439963ae1b3
diff --git a/core/app/jobs/spree/products/touch_taxons_job.rb b/core/app/jobs/spree/products/touch_taxons_job.rb
index 6762daf911..8a36eca08b 100644
--- a/core/app/jobs/spree/products/touch_taxons_job.rb
+++ b/core/app/jobs/spree/products/touch_taxons_job.rb
@@ -6,6 +6,7 @@ module Spree
def perform(taxon_ids, taxonomy_ids)
Spree::Taxon.where(id: taxon_ids).update_all(updated_at: Time.current)
Spree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)
+ Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids)
end
end
end
diff --git a/core/app/services/spree/taxons/add_products.rb b/core/app/services/spree/taxons/add_products.rb
index 1c91c2af56..c749d843e5 100644
--- a/core/app/services/spree/taxons/add_products.rb
+++ b/core/app/services/spree/taxons/add_products.rb
@@ -30,7 +30,10 @@ module Spree
# clearing cache
Spree::Product.where(id: products.pluck(:id)).touch_all
- Spree::Taxon.where(id: taxons.pluck(:id)).touch_all
+
+ taxon_ids = taxons.pluck(:id)
+ Spree::Taxon.where(id: taxon_ids).touch_all
+ Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids)
success(true)
end
diff --git a/core/app/services/spree/taxons/remove_products.rb b/core/app/services/spree/taxons/remove_products.rb
index c7109f66f2..2be051cc6b 100644
--- a/core/app/services/spree/taxons/remove_products.rb
+++ b/core/app/services/spree/taxons/remove_products.rb
@@ -44,7 +44,10 @@ module Spree
# clear cache
Spree::Product.where(id: products.pluck(:id)).touch_all
- Spree::Taxon.where(id: taxons.pluck(:id)).touch_all
+
+ taxon_ids = taxons.pluck(:id)
+ Spree::Taxon.where(id: taxon_ids).touch_all
+ Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids)
success(true)
end
diff --git a/core/spec/jobs/spree/products/touch_taxons_job_spec.rb b/core/spec/jobs/spree/products/touch_taxons_job_spec.rb
new file mode 100644
index 0000000000..16a201567f
--- /dev/null
+++ b/core/spec/jobs/spree/products/touch_taxons_job_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Spree::Products::TouchTaxonsJob, type: :job do
+ describe '#perform' do
+ subject { described_class.perform_now(taxon_ids, taxonomy_ids) }
+
+ let!(:taxonomy) { create(:taxonomy) }
+ let!(:taxon_1) { create(:taxon, taxonomy: taxonomy) }
+ let!(:taxon_2) { create(:taxon, taxonomy: taxonomy) }
+ let!(:other_taxon) { create(:taxon) }
+
+ let!(:featured_section) { create(:featured_taxon_page_section, preferred_taxon_id: taxon_1.id) }
+
+ let(:taxon_ids) { [taxon_1.id, taxon_2.id] }
+ let(:taxonomy_ids) { [taxonomy.id] }
+
+ it 'touches all specified taxons' do
+ expect { subject }.to change { Spree::Taxon.where(id: taxon_ids).pluck(:updated_at) }
+ end
+
+ it 'touches all specified taxonomies' do
+ expect { subject }.to change { Spree::Taxonomy.where(id: taxonomy_ids).pluck(:updated_at) }
+ end
+
+ it 'touches featured sections for the specified taxons' do
+ expect { subject }.to change { Spree::PageSections::FeaturedTaxon.where(id: featured_section.id).pluck(:updated_at) }
+ end
+
+ it 'does not touch other taxons' do
+ expect { subject }.not_to change { other_taxon.reload.updated_at }
+ end
+ end
+end
diff --git a/core/spec/services/spree/taxons/add_products_spec.rb b/core/spec/services/spree/taxons/add_products_spec.rb
index bf9b267953..ab23d85df8 100644
--- a/core/spec/services/spree/taxons/add_products_spec.rb
+++ b/core/spec/services/spree/taxons/add_products_spec.rb
@@ -5,6 +5,7 @@ module Spree
let(:service) { described_class }
let(:taxons) { create_list(:taxon, 2) }
let(:products) { create_list(:product, 3) }
+ let!(:featured_sections) { create_list(:featured_taxon_page_section, 2, preferred_taxon_id: taxons.first.id) }
describe '#call' do
subject { service.call(taxons: taxons, products: products) }
@@ -28,6 +29,10 @@ module Spree
expect { subject }.to change { Spree::Taxon.where(id: taxons.pluck(:id)).pluck(:updated_at) }
end
+ it 'touches all featured sections' do
+ expect { subject }.to change { Spree::PageSections::FeaturedTaxon.where(id: featured_sections.pluck(:id)).pluck(:updated_at) }
+ end
+
it 'returns a successful result' do
expect(subject.success?).to be true
expect(subject.value).to eq true
diff --git a/core/spec/services/spree/taxons/remove_products_spec.rb b/core/spec/services/spree/taxons/remove_products_spec.rb
index a51f393a74..1a049205ac 100644
--- a/core/spec/services/spree/taxons/remove_products_spec.rb
+++ b/core/spec/services/spree/taxons/remove_products_spec.rb
@@ -5,6 +5,7 @@ module Spree
let(:service) { described_class }
let(:taxons) { create_list(:taxon, 2) }
let(:products) { create_list(:product, 3) }
+ let!(:featured_sections) { create_list(:featured_taxon_page_section, 2, preferred_taxon_id: taxons.first.id) }
describe '#call' do
subject { service.call(taxons: taxons, products: products) }
@@ -36,6 +37,10 @@ module Spree
expect { subject }.to change { Spree::Taxon.where(id: taxons.pluck(:id)).pluck(:updated_at) }
end
+ it 'touches all featured sections' do
+ expect { subject }.to change { Spree::PageSections::FeaturedTaxon.where(id: featured_sections.pluck(:id)).pluck(:updated_at) }
+ end
+
it 'returns a successful result' do
expect(subject.success?).to be true
expect(subject.value).to eq true
diff --git a/storefront/app/views/themes/default/spree/page_sections/_featured_taxon.html.erb b/storefront/app/views/themes/default/spree/page_sections/_featured_taxon.html.erb
index c97454b2d1..b6e4815459 100644
--- a/storefront/app/views/themes/default/spree/page_sections/_featured_taxon.html.erb
+++ b/storefront/app/views/themes/default/spree/page_sections/_featured_taxon.html.erb
@@ -1,117 +1,119 @@
-<div style='<%= section_styles(section) %>' class='animate-fadeIn'>
- <div class='page-container'>
- <% heading_size = case section.preferred_heading_size
- when 'small' then 'text-base font-medium'
- when 'medium' then 'text-lg lg:text-xl font-medium'
- when 'large' then 'text-xl lg:text-2xl font-medium'
- end %>
- <% if section.taxon&.page_builder_image&.attached? && section.preferred_show_taxon_image %>
- <% desktop_slides_amount = 2.5 %>
- <% section_with_image = true %>
- <% arrows_on_top = false %>
- <% else %>
- <% desktop_slides_amount = 4 %>
- <% section_with_image = false %>
- <% arrows_on_top = true %>
- <% end %>
- <% if section.preferred_heading.present? %>
- <div class='mb-8 flex justify-between items-end'>
- <div class="md:basis-2/5">
- <h3
- class='<%= heading_size %> featured-taxon--title font-medium text-<%= section.preferred_heading_alignment %>'
- data-title="<%= section.preferred_heading.downcase %>"
- style='<%= section_heading_styles(section) %>'>
- <% if section.taxon.present? %>
- <%= link_to section.preferred_heading, spree_storefront_resource_url(section.taxon), data: { turbo_frame: "_top" } %>
- <% else %>
- <%= section.preferred_heading %>
- <% end %>
- </h3>
+<% cache_unless page_builder_enabled?, [spree_base_cache_key, section.cache_key_with_version, loaded.to_b] do %>
+ <div style='<%= section_styles(section) %>' class='animate-fadeIn'>
+ <div class='page-container'>
+ <% heading_size = case section.preferred_heading_size
+ when 'small' then 'text-base font-medium'
+ when 'medium' then 'text-lg lg:text-xl font-medium'
+ when 'large' then 'text-xl lg:text-2xl font-medium'
+ end %>
+ <% if section.taxon&.page_builder_image&.attached? && section.preferred_show_taxon_image %>
+ <% desktop_slides_amount = 2.5 %>
+ <% section_with_image = true %>
+ <% arrows_on_top = false %>
+ <% else %>
+ <% desktop_slides_amount = 4 %>
+ <% section_with_image = false %>
+ <% arrows_on_top = true %>
+ <% end %>
+ <% if section.preferred_heading.present? %>
+ <div class='mb-8 flex justify-between items-end'>
+ <div class="md:basis-2/5">
+ <h3
+ class='<%= heading_size %> featured-taxon--title font-medium text-<%= section.preferred_heading_alignment %>'
+ data-title="<%= section.preferred_heading.downcase %>"
+ style='<%= section_heading_styles(section) %>'>
+ <% if section.taxon.present? %>
+ <%= link_to section.preferred_heading, spree_storefront_resource_url(section.taxon), data: { turbo_frame: "_top" } %>
+ <% else %>
+ <%= section.preferred_heading %>
+ <% end %>
+ </h3>
- <% if section.description_to_use.present? %>
- <div class='pt-4 text-<%= section.preferred_description_alignment %>'>
- <%= section.description_to_use %>
- </div>
+ <% if section.description_to_use.present? %>
+ <div class='pt-4 text-<%= section.preferred_description_alignment %>'>
+ <%= section.description_to_use %>
+ </div>
+ <% end %>
+ </div>
+ <% if section.taxon.present? %>
+ <%= link_to spree_storefront_resource_url(section.taxon), class: class_names(section.preferred_button_style == "primary" ? "btn-primary" : "btn-secondary", " text-center hidden md:inline-block"), data: { turbo_frame: '_top' } do %>
+ <%= section.preferred_button_text %>
+ <% end %>
<% end %>
</div>
- <% if section.taxon.present? %>
- <%= link_to spree_storefront_resource_url(section.taxon), class: class_names(section.preferred_button_style == "primary" ? "btn-primary" : "btn-secondary", " text-center hidden md:inline-block"), data: { turbo_frame: '_top' } do %>
- <%= section.preferred_button_text %>
- <% end %>
- <% end %>
- </div>
- <% end %>
- <% if loaded && section.taxon.present? %>
- <% taxon_products = taxon_products(section.taxon).limit(section.preferred_max_products_to_show) %>
- <% if taxon_products.any? %>
- <div class='grid grid-cols-1 lg:grid-cols-12 gap-4 lg:gap-6'>
- <% if section_with_image %>
- <div class='lg:col-span-5'>
- <%= link_to spree.nested_taxons_path(section.taxon), data: { turbo_frame: "_top" } do %>
- <%= spree_image_tag(section.taxon.page_builder_image, height: 500, width: 500, class: 'h-full w-full object-cover object-center', loading: :lazy) %>
- <% end %>
+ <% end %>
+ <% if loaded && section.taxon.present? %>
+ <% taxon_products = taxon_products(section.taxon).limit(section.preferred_max_products_to_show) %>
+ <% if taxon_products.any? %>
+ <div class='grid grid-cols-1 lg:grid-cols-12 gap-4 lg:gap-6'>
+ <% if section_with_image %>
+ <div class='lg:col-span-5'>
+ <%= link_to spree.nested_taxons_path(section.taxon), data: { turbo_frame: "_top" } do %>
+ <%= spree_image_tag(section.taxon.page_builder_image, height: 500, width: 500, class: 'h-full w-full object-cover object-center', loading: :lazy) %>
+ <% end %>
+ </div>
+ <% end %>
+ <div class='<%= section_with_image ? "lg:col-span-7" : "lg:col-span-12" %>'>
+ <%= render 'spree/products/swiper', products: taxon_products, desktop_slides: desktop_slides_amount, pagination: 'bottom', arrows_on_top: arrows_on_top, section: section %>
</div>
- <% end %>
- <div class='<%= section_with_image ? "lg:col-span-7" : "lg:col-span-12" %>'>
- <%= render 'spree/products/swiper', products: taxon_products, desktop_slides: desktop_slides_amount, pagination: 'bottom', arrows_on_top: arrows_on_top, section: section %>
</div>
- </div>
- <% end %>
- <% else %>
- <div class='grid grid-cols-1 md:grid-cols-12 gap-4 md:gap-6'>
- <% if section_with_image %>
- <div class='md:col-span-5 w-full aspect-1 md:h-[536px] bg-accent'></div>
<% end %>
- <div class='<%= section_with_image ? "md:col-span-7" : "md:col-span-12" %> flex justify-between flex-col overflow-hidden'>
+ <% else %>
+ <div class='grid grid-cols-1 md:grid-cols-12 gap-4 md:gap-6'>
<% if section_with_image %>
- <div class='grid gap-4 md:gap-6 grid-cols-8 md:grid-cols-5'>
- <div class='col-span-6 md:col-span-2'>
- <div class='aspect-1 bg-accent w-full'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
- </div>
- <div class='col-span-2 flex flex-col h-full overflow-hidden'>
- <div class='h-full md:aspect-1 bg-accent w-full'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded shrink-0'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded shrink-0'></div>
- </div>
- <div class='hidden md:block'>
- <div class='aspect-[1/2] bg-accent w-full'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
- </div>
- </div>
- <div class='md:flex <%= section.preferred_show_more_button ? "justify-between" : "justify-end" %> items-end'>
- <div class='bg-accent h-12 rounded-full w-full md:w-56 mt-8 md:order-1 hidden md:block'></div>
- <div class='<%= section.preferred_show_more_button ? "" : "hidden" %> bg-accent h-12 rounded-button w-full md:w-72 mt-6'></div>
- </div>
- <% else %>
- <div class='grid gap-4 md:gap-6 grid-cols-8 relative'>
- <div class='col-span-6 md:col-span-2'>
- <div class='aspect-1 bg-accent w-full'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
- </div>
- <div class='col-span-2 md:col-span-2 flex flex-col'>
- <div class='aspect-[1/3] md:aspect-1 bg-accent w-full flex-1'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ <div class='md:col-span-5 w-full aspect-1 md:h-[536px] bg-accent'></div>
+ <% end %>
+ <div class='<%= section_with_image ? "md:col-span-7" : "md:col-span-12" %> flex justify-between flex-col overflow-hidden'>
+ <% if section_with_image %>
+ <div class='grid gap-4 md:gap-6 grid-cols-8 md:grid-cols-5'>
+ <div class='col-span-6 md:col-span-2'>
+ <div class='aspect-1 bg-accent w-full'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ </div>
+ <div class='col-span-2 flex flex-col h-full overflow-hidden'>
+ <div class='h-full md:aspect-1 bg-accent w-full'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded shrink-0'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded shrink-0'></div>
+ </div>
+ <div class='hidden md:block'>
+ <div class='aspect-[1/2] bg-accent w-full'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ </div>
</div>
- <div class='col-span-2 hidden md:block'>
- <div class='aspect-1 bg-accent w-full'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ <div class='md:flex <%= section.preferred_show_more_button ? "justify-between" : "justify-end" %> items-end'>
+ <div class='bg-accent h-12 rounded-full w-full md:w-56 mt-8 md:order-1 hidden md:block'></div>
+ <div class='<%= section.preferred_show_more_button ? "" : "hidden" %> bg-accent h-12 rounded-button w-full md:w-72 mt-6'></div>
</div>
- <div class='col-span-2 hidden md:block'>
- <div class='aspect-1 bg-accent w-full'></div>
- <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
- <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ <% else %>
+ <div class='grid gap-4 md:gap-6 grid-cols-8 relative'>
+ <div class='col-span-6 md:col-span-2'>
+ <div class='aspect-1 bg-accent w-full'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ </div>
+ <div class='col-span-2 md:col-span-2 flex flex-col'>
+ <div class='aspect-[1/3] md:aspect-1 bg-accent w-full flex-1'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ </div>
+ <div class='col-span-2 hidden md:block'>
+ <div class='aspect-1 bg-accent w-full'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ </div>
+ <div class='col-span-2 hidden md:block'>
+ <div class='aspect-1 bg-accent w-full'></div>
+ <div class='h-6 mt-2 w-32 bg-accent rounded'></div>
+ <div class='h-8 mt-2 w-16 bg-accent rounded'></div>
+ </div>
</div>
- </div>
- <div class="md:hidden w-full block rounded-button bg-accent h-12 mt-8"></div>
- <% end %>
+ <div class="md:hidden w-full block rounded-button bg-accent h-12 mt-8"></div>
+ <% end %>
+ </div>
</div>
- </div>
- <% end %>
+ <% end %>
+ </div>
</div>
-</div>
+<% end %>
\ No newline at end of file