Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocking navigation #2236

Closed
bs1180 opened this issue Jun 12, 2017 · 7 comments
Closed

Blocking navigation #2236

bs1180 opened this issue Jun 12, 2017 · 7 comments

Comments

@bs1180
Copy link

bs1180 commented Jun 12, 2017

I have a Next.js app with an editor page, and would like to warn users about unsaved changes if they try and navigate away. React Router offers a handy prompt component - is there a way to achieve something similar in Next?

@arunoda
Copy link
Contributor

arunoda commented Jun 12, 2017

You can use browser's inbuilt onbeforeunload event.
See: https://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event

@arunoda arunoda closed this as completed Jun 12, 2017
@bs1180
Copy link
Author

bs1180 commented Jun 12, 2017

Sorry, I should have said that I've already tried that :) The following snippet works fine if a user hits back etc., but does nothing if they navigate away by clicking any links within the site.

handleWindowClose = (e) => {
    e.preventDefault();
    return e.returnValue = "You have unsaved changes - are you sure you wish to close?";
 }

  componentDidMount = () => {
    window.addEventListener("beforeunload", this.handleWindowClose);
 }

  componentWillUnmount = () => {
    if (process.browser) {
      window.removeEventListener("beforeunload", this.handleWindowClose);
    }  
 }

@arunoda
Copy link
Contributor

arunoda commented Jun 12, 2017

but does nothing if they navigate away by clicking any links within the site.

Good point.
In that case, what you can do use our next/router API to implement a such functionality.
We are not directly looking to add this feature right now.
But this is something we can do in the future.

@timneutkens
Copy link
Member

I'm willing to accept an example of this in the repo 👌

@arunoda
Copy link
Contributor

arunoda commented Jun 13, 2017

Actually, we may need some core support for that as well at some point.

@liweinan0423
Copy link
Contributor

liweinan0423 commented Nov 15, 2017

I think we should be able to allow the routing to be stopped if user returns false in the callback function. such like

Router.onRouteChangeStart = () => {
  if (/*should not leave the page*/) {
    return false;
  } else {
    return true;
  }
}

Or adding another event that gets triggered before the transition starts to allow user halt the transition, e.g. Router.beforeRouteChangeStart

@arunoda
Copy link
Contributor

arunoda commented Dec 8, 2017

Closing in favor of #2694

@arunoda arunoda closed this as completed Dec 8, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Dec 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants