vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.84k stars 26.65k forks source link

Get Previous Url in nextjs router. #4136

Closed mira-thakkar closed 5 years ago

mira-thakkar commented 6 years ago

I am building react js app based on nextjs boilerplate.

Expected Behavior

Check if router has previous url or not.

Current Behavior

The tab is being closed on router.back()

Steps to Reproduce (for bugs)

router.back()

Context

I need to get the previous url to redirect the user back to the page after signin in my app. It works fine if there is any other page visited before the signin page. But when user directly open signin page in new tab, there will be no previous url. So in router.back() it closes the tab.

Your Environment

Tech Version
next ^3.2.2
node 9.4.0
OS MacOS Sierra 10.12.6
browser Google Chrome Version 65.0.3325.181
lfades commented 6 years ago

if the sign process is being done in the client set a flag that will be true when any page (except the signin page) is loaded, and if it's true use Router.back() otherwise don't.

kasbah commented 6 years ago

I think Router.back() which seems to just call window.history.back() is not ideal for this use case in general because the resulting state of the history, i.e. forward button will go to the login page again, is unexpected to the user.

timneutkens commented 5 years ago

You could in theory keep track of previously visited pages using a global state. It's not up to the Next.js router to track this as we just use browser APIs / state