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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
[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.rbindex fba3a0f3ff..422e225ce1 100644--- a/frontend/app/controllers/spree/home_controller.rb+++ b/frontend/app/controllers/spree/home_controller.rb@@ -1,9 +1,13 @@module Spreeclass HomeController < Spree::StoreController+ include Spree::CacheHelper+respond_to :htmldef 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+ endendendenddiff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rbindex dac7e71e3b..302bc255af 100644--- a/frontend/app/controllers/spree/products_controller.rb+++ b/frontend/app/controllers/spree/products_controller.rb@@ -2,6 +2,7 @@ module Spreeclass ProductsController < Spree::StoreControllerinclude Spree::ProductsHelperinclude Spree::FrontendHelper+ include Spree::CacheHelperbefore_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+ endenddef 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).amountdiff --git a/frontend/app/controllers/spree/taxons_controller.rb b/frontend/app/controllers/spree/taxons_controller.rbindex d85ccc5107..7b668ace06 100644--- a/frontend/app/controllers/spree/taxons_controller.rb+++ b/frontend/app/controllers/spree/taxons_controller.rb@@ -1,6 +1,7 @@module Spreeclass TaxonsController < Spree::StoreControllerinclude Spree::FrontendHelper+ include Spree::CacheHelperhelper 'spree/products'before_action :load_taxon@@ -8,13 +9,13 @@ module Spreerespond_to :htmldef 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_productsendenddef 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_productsif @products.reload.any?render template: 'spree/taxons/product_carousel', layout: falsediff --git a/frontend/app/helpers/spree/cache_helper.rb b/frontend/app/helpers/spree/cache_helper.rbnew file mode 100644index 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+enddiff --git a/frontend/app/models/spree/frontend_configuration.rb b/frontend/app/models/spree/frontend_configuration.rbindex 554a9922e1..d4fbdf1ca3 100644--- a/frontend/app/models/spree/frontend_configuration.rb+++ b/frontend/app/models/spree/frontend_configuration.rb@@ -1,6 +1,7 @@module Spreeclass FrontendConfiguration < Preferences::Configurationpreference :coupon_codes_enabled, :boolean, default: true # Determines if we show coupon code form at cart and checkout+ preference :http_cache_enabled, :boolean, default: truepreference :locale, :string, default: Rails.application.config.i18n.default_localepreference :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.rbindex 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 doexpect(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' dobefore { Spree::Config.layout = 'layouts/application' }@@ -15,5 +21,15 @@ describe Spree::HomeController, type: :controller doexpect(response).to render_template(layout: 'layouts/application')endend++ 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+ endendenddiff --git a/frontend/spec/controllers/spree/products_controller_spec.rb b/frontend/spec/controllers/spree/products_controller_spec.rbindex 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 doexpect(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+ endendend