From 89c411e0fcc66fb513f4d3b05886dca48de3d123 Mon Sep 17 00:00:00 2001 From: jamey Date: Mon, 9 Mar 2026 18:34:45 +0000 Subject: [PATCH] clear page-specific assigns on navigation to prevent stale data 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 --- lib/berrypod_web/live/shop/page.ex | 64 +++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/berrypod_web/live/shop/page.ex b/lib/berrypod_web/live/shop/page.ex index 1562e61..f2e2579 100644 --- a/lib/berrypod_web/live/shop/page.ex +++ b/lib/berrypod_web/live/shop/page.ex @@ -49,8 +49,11 @@ defmodule BerrypodWeb.Shop.Page do socket = if action != prev_action do - # Page type changed - call init - socket = assign(socket, :_current_page_action, action) + # Page type changed - clear previous page's assigns and call init + socket = + socket + |> assign(:_current_page_action, action) + |> clear_page_specific_assigns(prev_action) result = if action in @session_pages do @@ -151,4 +154,61 @@ defmodule BerrypodWeb.Shop.Page do end 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