Default location if no back history?
Hi, I'm trying to utilize
router.history.back() but if the user for whatever reason refreshes the page all of the history they can go back to is gone. Thus causing my back button to just refresh the page.
How can I provide a default location to navigate to if there is no back history to go to? Or how can I detect if there's history to go back to, so can instead run a navigate() to a fallback location?2 Replies
rare-sapphire•2y ago
Tanstack Router is built on top of the browser standards for navigation (with exception for hidden state property).
So if you navigate to different routes in the tree and refresh the page, the navigation history is not lost. This behaviour is the same even if you perform a hard refresh. This wouldn't work is you opened a link in a new tab since that'd be creating a new instance of History which is the native browser behaviour.
You could manually force this behaviour by passing around a previous_url query param in the URL (or in local storage) and checking if that is present before calling the
.back() method. But it certainly does introduce quite a bit of overhead since you'd be manually performing this tracking.
TLDR: since this isn't a native browser behaviour, it the Router doesn't have this either.
You could also check document.referrer but it doesn't have the most consistent behaviour.rising-crimsonOP•2y ago
Thank you
I think I'll just put a search param 🔥