clear page-specific assigns on navigation to prevent stale data
All checks were successful
deploy / deploy (push) Successful in 1m27s
All checks were successful
deploy / deploy (push) Successful in 1m27s
When navigating between page types in the unified shop LiveView, assigns from the previous page could persist and cause stale data to appear or template errors. Now explicitly nils them out. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
6aaddeaf44
commit
89c411e0fc
@ -49,8 +49,11 @@ defmodule BerrypodWeb.Shop.Page do
|
|||||||
|
|
||||||
socket =
|
socket =
|
||||||
if action != prev_action do
|
if action != prev_action do
|
||||||
# Page type changed - call init
|
# Page type changed - clear previous page's assigns and call init
|
||||||
socket = assign(socket, :_current_page_action, action)
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:_current_page_action, action)
|
||||||
|
|> clear_page_specific_assigns(prev_action)
|
||||||
|
|
||||||
result =
|
result =
|
||||||
if action in @session_pages do
|
if action in @session_pages do
|
||||||
@ -151,4 +154,61 @@ defmodule BerrypodWeb.Shop.Page do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_cleanup_previous_page(socket, _), do: socket
|
defp maybe_cleanup_previous_page(socket, _), do: socket
|
||||||
|
|
||||||
|
# Clear assigns that are specific to each page type to prevent stale data
|
||||||
|
# from bleeding into the next page's render
|
||||||
|
defp clear_page_specific_assigns(socket, :product) do
|
||||||
|
socket
|
||||||
|
|> assign(:product, nil)
|
||||||
|
|> assign(:all_images, nil)
|
||||||
|
|> assign(:variants, nil)
|
||||||
|
|> assign(:option_types, nil)
|
||||||
|
|> assign(:selected_variant, nil)
|
||||||
|
|> assign(:selected_options, nil)
|
||||||
|
|> assign(:available_options, nil)
|
||||||
|
|> assign(:display_price, nil)
|
||||||
|
|> assign(:gallery_images, nil)
|
||||||
|
|> assign(:option_urls, nil)
|
||||||
|
|> assign(:related_products, nil)
|
||||||
|
|> assign(:json_ld, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, :collection) do
|
||||||
|
socket
|
||||||
|
|> assign(:products, nil)
|
||||||
|
|> assign(:pagination, nil)
|
||||||
|
|> assign(:current_sort, nil)
|
||||||
|
|> assign(:collection_title, nil)
|
||||||
|
|> assign(:collection_slug, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, :contact) do
|
||||||
|
socket
|
||||||
|
|> assign(:contact_form, nil)
|
||||||
|
|> assign(:lookup_form, nil)
|
||||||
|
|> assign(:looked_up_orders, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, :search) do
|
||||||
|
assign(socket, :search_results, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, :orders) do
|
||||||
|
assign(socket, :orders, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, :order_detail) do
|
||||||
|
socket
|
||||||
|
|> assign(:order, nil)
|
||||||
|
|> assign(:email_form, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, :checkout_success) do
|
||||||
|
socket
|
||||||
|
|> assign(:order, nil)
|
||||||
|
|> assign(:js_purchase_tracked, nil)
|
||||||
|
|> assign(:css_purchase_tracked, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp clear_page_specific_assigns(socket, _), do: socket
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user