fix 404 handling for unknown url prefixes
All checks were successful
deploy / deploy (push) Successful in 2m18s

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 <noreply@anthropic.com>
This commit is contained in:
jamey 2026-04-01 01:04:42 +01:00
parent a41771efc8
commit 3b23a413ed

View File

@ -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