import { goto } from "$app/navigation"; /** * Navigate "back" using real browser/Android history when possible, falling * back to an explicit path otherwise. * * Hardcoded `goto(backPath)` always sends the user to a fixed screen, which * loses track of where they actually came from (e.g. reaching the genres list * from different entry points). Preferring `history.back()` keeps the back * affordance consistent with the platform back gesture and the browser/Android * hardware back button. * * We only use history when there is somewhere to go back to *within the app*. * On a fresh deep-link (history length 1, or an external referrer) we fall back * to `fallbackPath` so the user never gets stranded or bounced out of the app. */ export function navigateBack(fallbackPath: string): void { if (canGoBack()) { history.back(); } else { goto(fallbackPath); } } /** * True when there is in-app history to pop. `history.length > 1` means the user * navigated here from another page in this session rather than landing here * directly (deep link, refresh, or first load). */ function canGoBack(): boolean { if (typeof history === "undefined") return false; return history.length > 1; }