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.

[SD-1098] Added preference to toggle HTTP caching for Storefrront (#10688)

Kacper Mękarski74b66b0d7103
diff --git a/frontend/app/controllers/spree/home_controller.rb b/frontend/app/controllers/spree/home_controller.rb
index fba3a0f3ff..422e225ce1 100644
--- a/frontend/app/controllers/spree/home_controller.rb
+++ b/frontend/app/controllers/spree/home_controller.rb
@@ -1,9 +1,13 @@
module Spree
class HomeController < Spree::StoreController
+ include Spree::CacheHelper
+
respond_to :html
def index
- fresh_when etag: store_etag, last_modified: store_last_modified, public: true
+ if http_cache_enabled?
+ fresh_when etag: store_etag, last_modified: store_last_modified, public: true
+ end
end
end
end
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index dac7e71e3b..302bc255af 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -2,6 +2,7 @@ module Spree
class ProductsController < Spree::StoreController
include Spree::ProductsHelper
include Spree::FrontendHelper
+ include Spree::CacheHelper
before_action :load_product, only: [:show, :related]
before_action :load_taxon, only: :index
@@ -12,7 +13,9 @@ module Spree
@searcher = build_searcher(params.merge(include_images: true, current_store_id: current_store.id))
@products = @searcher.retrieve_products
- fresh_when etag: etag_index, last_modified: last_modified_index, public: true
+ if http_cache_enabled?
+ fresh_when etag: etag_index, last_modified: last_modified_index, public: true
+ end
end
def show
@@ -20,7 +23,7 @@ module Spree
@taxon = params[:taxon_id].present? ? Spree::Taxon.find(params[:taxon_id]) : @product.taxons.first
- if stale?(etag: etag_show, last_modified: last_modified_show, public: true)
+ if !http_cache_enabled? || 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
diff --git a/frontend/app/controllers/spree/taxons_controller.rb b/frontend/app/controllers/spree/taxons_controller.rb
index d85ccc5107..7b668ace06 100644
--- a/frontend/app/controllers/spree/taxons_controller.rb
+++ b/frontend/app/controllers/spree/taxons_controller.rb
@@ -1,6 +1,7 @@
module Spree
class TaxonsController < Spree::StoreController
include Spree::FrontendHelper
+ include Spree::CacheHelper
helper 'spree/products'
before_action :load_taxon
@@ -8,13 +9,13 @@ module Spree
respond_to :html
def show
- if stale?(etag: etag, last_modified: last_modified, public: true)
+ if !http_cache_enabled? || stale?(etag: etag, last_modified: last_modified, public: true)
load_products
end
end
def product_carousel
- if stale?(etag: carousel_etag, last_modified: last_modified, public: true)
+ if !http_cache_enabled? || stale?(etag: carousel_etag, last_modified: last_modified, public: true)
load_products
if @products.reload.any?
render template: 'spree/taxons/product_carousel', layout: false
diff --git a/frontend/app/helpers/spree/cache_helper.rb b/frontend/app/helpers/spree/cache_helper.rb
new file mode 100644
index 0000000000..7d6b98af3f
--- /dev/null
+++ b/frontend/app/helpers/spree/cache_helper.rb
@@ -0,0 +1,7 @@
+module Spree
+ module CacheHelper
+ def http_cache_enabled?
+ @http_cache_enabled ||= Spree::Frontend::Config[:http_cache_enabled]
+ end
+ end
+end
diff --git a/frontend/app/models/spree/frontend_configuration.rb b/frontend/app/models/spree/frontend_configuration.rb
index 554a9922e1..d4fbdf1ca3 100644
--- a/frontend/app/models/spree/frontend_configuration.rb
+++ b/frontend/app/models/spree/frontend_configuration.rb
@@ -1,6 +1,7 @@
module Spree
class FrontendConfiguration < Preferences::Configuration
preference :coupon_codes_enabled, :boolean, default: true # Determines if we show coupon code form at cart and checkout
+ preference :http_cache_enabled, :boolean, default: true
preference :locale, :string, default: Rails.application.config.i18n.default_locale
preference :products_filters, :array, default: %w(keywords price sort_by)
preference :additional_filters_partials, :array, default: %w()
diff --git a/frontend/spec/controllers/spree/home_controller_spec.rb b/frontend/spec/controllers/spree/home_controller_spec.rb
index 99c770c833..64f7e62745 100644
--- a/frontend/spec/controllers/spree/home_controller_spec.rb
+++ b/frontend/spec/controllers/spree/home_controller_spec.rb
@@ -7,6 +7,12 @@ describe Spree::HomeController, type: :controller do
expect(response).to render_template(layout: 'spree/layouts/spree_application')
end
+ it 'calls fresh_when method' do
+ expect(subject).to receive(:fresh_when)
+
+ get :index
+ end
+
context 'different layout specified in config' do
before { Spree::Config.layout = 'layouts/application' }
@@ -15,5 +21,15 @@ describe Spree::HomeController, type: :controller do
expect(response).to render_template(layout: 'layouts/application')
end
end
+
+ context 'when http_cache_enabled is set to false' do
+ before { Spree::Frontend::Config[:http_cache_enabled] = false }
+
+ it 'does not call fresh_when method' do
+ expect(subject).not_to receive(:fresh_when)
+
+ get :index
+ end
+ end
end
end
diff --git a/frontend/spec/controllers/spree/products_controller_spec.rb b/frontend/spec/controllers/spree/products_controller_spec.rb
index edb2525e00..17920f317b 100644
--- a/frontend/spec/controllers/spree/products_controller_spec.rb
+++ b/frontend/spec/controllers/spree/products_controller_spec.rb
@@ -74,5 +74,21 @@ describe Spree::ProductsController, type: :controller do
expect(assigns(:products)).to eq([])
end
+
+ it 'calls fresh_when method' do
+ expect(subject).to receive(:fresh_when)
+
+ get :index
+ end
+
+ context 'when http_cache_enabled is set to false' do
+ before { Spree::Frontend::Config[:http_cache_enabled] = false }
+
+ it 'does not call fresh_when method' do
+ expect(subject).not_to receive(:fresh_when)
+
+ get :index
+ end
+ end
end
end