From 3b23a413eddb00845063dcd16ac79641fc76e94c Mon Sep 17 00:00:00 2001 From: jamey Date: Wed, 1 Apr 2026 01:04:42 +0100 Subject: [PATCH] fix 404 handling for unknown url prefixes Unknown prefixes like /shoppppp/thing now correctly return 404 instead of trying to resolve as a custom page. Also records broken URLs for admin review. Co-Authored-By: Claude Opus 4.5 --- lib/berrypod_web/live/shop/page.ex | 41 +++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/berrypod_web/live/shop/page.ex b/lib/berrypod_web/live/shop/page.ex index 2041efe..5cd8717 100644 --- a/lib/berrypod_web/live/shop/page.ex +++ b/lib/berrypod_web/live/shop/page.ex @@ -194,6 +194,12 @@ defmodule BerrypodWeb.Shop.Page do # For custom_page action, check if slug is a custom URL for a system page {action, params} = resolve_custom_slug(action, params) + # Handle unrecognised routes (e.g., /shoppppp/thing) + if action == :not_found do + record_broken_url(current_path) + raise BerrypodWeb.NotFoundError + end + # Update live_action if we resolved to a different page type socket = if action != socket.assigns.live_action do @@ -430,9 +436,42 @@ defmodule BerrypodWeb.Shop.Page do nil -> # Not a known prefix - check if combined path is a system page (e.g. checkout/success) combined_slug = prefix <> "/" <> id_or_slug - resolve_custom_slug(:custom_page, Map.put(other_params, "slug", combined_slug)) + + case R.page_type_from_slug(combined_slug) do + {:page, page_type} -> + {page_type, other_params} + + {:custom, _page} -> + {:custom_page, Map.put(other_params, "slug", combined_slug)} + + nil -> + # Unknown prefix and not a valid page - 404 + {:not_found, other_params} + end end end defp resolve_custom_slug(action, params), do: {action, params} + + # Record broken URL for 404 tracking (mirrors CustomPage.record_broken_url) + defp record_broken_url(path) do + unless static_path?(path) do + prior_hits = Berrypod.Analytics.count_pageviews_for_path(path) + Berrypod.Redirects.record_broken_url(path, prior_hits) + + if prior_hits > 0 do + Berrypod.Redirects.attempt_auto_resolve(path) + end + end + rescue + _ -> :ok + end + + defp static_path?(path) do + String.starts_with?(path, "/assets/") or + String.starts_with?(path, "/images/") or + String.starts_with?(path, "/image_cache/") or + String.starts_with?(path, "/mockups/") or + String.starts_with?(path, "/favicon") + end end