Navigating back to previous page

So i am trying to create a back button in a web app which s working fine using history.back() but the problem is if i copy paste the link and then i hit the back button the user will go back to the previous tab even if its not our domain. i want the user to go to home page if previous page is not a page from our hostname using react-router-dom for navigation
export const useCustomBackNavigation = () => {
const navigate = useNavigate();
// how do i check if the previous page is not our page
const handleBackClick = () => {
navigate(-1);
};

return handleBackClick;
};
export const useCustomBackNavigation = () => {
const navigate = useNavigate();
// how do i check if the previous page is not our page
const handleBackClick = () => {
navigate(-1);
};

return handleBackClick;
};
No description
1 Reply
MarkBoots
MarkBoots6mo ago
unfortunately gladly you can not check if the history-1 is another domain. this would open up a lot of privacy concerns. you probably could sessionStorage the page urls during a visit in a list and use that to navigate back. it will require some extra logic that i'm not able to provide you right now. But maybe this is enough for you to get on track. just thinking. for your use case you might also get away with document.referrer it will give you the url of the page that linked to the current page. if there is none, it will return empty (page was opened directly)
Want results from more Discord servers?
Add your server