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.

Cache Countries/States JSON needed for address form (#13403)

Damian Legawiecaf9902ad5d6a
diff --git a/core/app/helpers/spree/addresses_helper.rb b/core/app/helpers/spree/addresses_helper.rb
index 6ce6534001..536b950f24 100644
--- a/core/app/helpers/spree/addresses_helper.rb
+++ b/core/app/helpers/spree/addresses_helper.rb
@@ -1,6 +1,14 @@
# https://github.com/spree-contrib/spree_address_book/blob/master/app/helpers/spree/addresses_helper.rb
module Spree
module AddressesHelper
+ def address_form_countries_states_cache_key
+ @address_form_countries_states_cache_key ||= [
+ I18n.locale,
+ current_store.cache_key_with_version,
+ current_store.checkout_zone&.cache_key_with_version
+ ].compact
+ end
+
def address_field(form, method, address_id = 'b', required = false, text_field_attributes: {}, &handler)
content_tag :div, id: [address_id, method].join, class: 'mb-4' do
if handler
diff --git a/core/app/views/spree/addresses/_form.html.erb b/core/app/views/spree/addresses/_form.html.erb
index 5ec01f2ecb..277200d7a0 100644
--- a/core/app/views/spree/addresses/_form.html.erb
+++ b/core/app/views/spree/addresses/_form.html.erb
@@ -3,8 +3,10 @@
<div class="inner"
data-controller="address-form address-autocomplete"
- data-address-form-countries-value="<%= available_countries.to_json %>"
- data-address-form-states-value="<%= available_states.to_json %>"
+ <% cache address_form_countries_states_cache_key do %>
+ data-address-form-countries-value="<%= available_countries.to_json %>"
+ data-address-form-states-value="<%= available_states.to_json %>"
+ <% end %>
data-address-form-current-state-id-value="<%= address.state_id || address_form.object.state_id %>"
>
<div id="<%= "#{address_id}country" %>">