next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Route cancellation

Open cmwd opened this issue 9 years ago • 59 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

A method in router API that allows aborting upcoming route change.

Current Behavior

Right now router exposes abortComponentLoad method but it seems to be designed only for internal use and it doesn't work if it's called in routeChangeStart handler. It works only after this piece of code was executed:

async fetchComponent (route, as) {
  let cancelled = false
  const cancel = this.componentLoadCancel = function () {
    cancelled = true
  }

Workaround:

Router.ready(() => {
  Router.router.on('routeChangeStart', () => {
    setTimeout(() => {
      Router.router.abortComponentLoad();
    });
  });
});

Context

This is useful for example when building complex forms and we want to warn a user before he leaves the page without submitting.

Your Environment

ANY

cmwd avatar Jul 06 '17 05:07 cmwd

If someone needs this feature I found that better workaround is just to throw an error in routeChangeStart event handler:

Router.ready(() => {
  Router.router.on('routeChangeStart', () => {
    throw new Error('Abort');
  });
});

cmwd avatar Jul 11 '17 12:07 cmwd

Seems like a reasonable solution would be to abort the routing if the return value from onRouteChangeStart() is === false. That's what I attempted before having to search and find this issue.

erikras avatar Jul 19 '17 18:07 erikras

The workarounds above no longer work in Next.js 4, I believe this is because there are no methods exposed on the router itself that would give us control over this. Particularly, there is no way to expose componentLoadCancel to manually cancel a route

A use-case where this would be helpful is "unsaved changes" dialogs/warnings.

I'm open to making a PR if this is something that would be desired.

tlackemann avatar Oct 25 '17 14:10 tlackemann

The workarounds only work for a link clicked but don't work when using browser navigation for instance. I agree with @erikras, that would be really nice to be able to return a boolean in onRouteChangeStart to cancel the route change!

Router.onRouteChangeStart = (url) => {
  if (!window.confirm('You really want to leave?')) {
    return false;
  }
  return true;
}

Unfortunately, onRouteChangeStart is just an event and can't manage any value to return.

DonovanCharpin avatar Dec 27 '17 04:12 DonovanCharpin

I think it makes sense to cancel route by throwing a custom error.

Router.onRouteChangeStart = (url) => {
  if (!window.confirm('You really want to leave?')) {
    throw new Router.Abort();
  }
}

What do you think?

cmwd avatar Dec 27 '17 22:12 cmwd

Both work for me as long as we have this feature which is really useful.

I think It's more common to manage this with a boolean but more understandable with this Abort method.

DonovanCharpin avatar Dec 27 '17 22:12 DonovanCharpin

Did anything like this make it into 5.0? Anybody done something like this in 5.0 yet?

ryanmeisters avatar Mar 10 '18 18:03 ryanmeisters

Also looking for this feature. Working on a editor app and need to be able to cancel route changes in case there are unsaved changes.

paulobarcelos avatar Jun 25 '18 20:06 paulobarcelos

The only way to work with this was using

Router.router.events.on('routeChangeStart', () => {
    throw new Error('nooo')
})

nalejandroveron avatar Aug 15 '18 17:08 nalejandroveron

@Negan1911 , where do you catch the raised error?

justbrody avatar Oct 01 '18 19:10 justbrody

@justbrody no need to catch it, it will appear on the console but prevent the navigation

nalejandroveron avatar Oct 01 '18 19:10 nalejandroveron

@Negan1911 , Thanks for the quick reply. I'm trying not to polute the console, people will get nervous seeing errors in the console.

justbrody avatar Oct 01 '18 19:10 justbrody

@justbrody Yeah i know, my QA got crazy because of that, but nothing to do at this level ¯_(ツ)_/¯¯

nalejandroveron avatar Oct 01 '18 19:10 nalejandroveron

@Negan1911 I'm going to wrap the Router calls with my own implementation for more control. Nextjs-team, thanks for a great library and I hope the requested feature of @cmwd will someday be implemented.

justbrody avatar Oct 01 '18 19:10 justbrody

Also looking for this feature. Working on a editor app and need to be able to cancel route changes in case there are unsaved changes.

I just ended up dumping work-in-progress stuff to localStorage. Asking the user if they want to restore or not when they come back suffices for my scenario (accidental navigation).

thebearingedge avatar Oct 01 '18 20:10 thebearingedge

While I agree that users need the ability to intercept routing events, I don't necessarily think that onRouteChangeStart is the right place.

Rather, (taking after React of course) what about a hook like shouldRouteChange that returns true by default, but can be overridden with proprietary logic?

isaachinman avatar Oct 07 '18 18:10 isaachinman

@isaachinman I think that creating a hook would restrict the number of possibilities. You don't want to have to use react to handle this kind of event. You want to be able to intercept it anywhere in the app (can be a library, singleton, component etc.). There is an attempt to solve this issue here https://github.com/zeit/next.js/issues/2694

DonovanCharpin avatar Oct 07 '18 19:10 DonovanCharpin

@DonovanCharpin I never said anything about using React to handle the event, simply mentioned that the implementation would be similar.

isaachinman avatar Oct 07 '18 19:10 isaachinman

Any new with this? @Negan1911 I tried your proposal of the workaround in Next.js 9.0.4 and is blocking the back refresh, that is cool... However, the URL changed to the previous history state instead of keeping the same 😕.

aralroca avatar Aug 21 '19 16:08 aralroca

I know it doesn't solve the underlying issue but since we have some control over Next's Link component, I imagine we could override the default behavior to add a check to whether it should be triggered or not. This adds extra complexity, though, as you have to communicate the state to a custom Link component that wraps Next's logic. An alternative would be to go through Router instead of Link if you go this way.

bebraw avatar Oct 11 '19 13:10 bebraw