From: Eduardo San Martin Morote Date: Wed, 8 Apr 2020 16:13:59 +0000 (+0200) Subject: fix: revert history navigation if navigation is cancelled X-Git-Tag: v4.0.0-alpha.6~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8a0d117dbede9b177f06c8ebab201d12dfca0c0;p=thirdparty%2Fvuejs%2Frouter.git fix: revert history navigation if navigation is cancelled --- diff --git a/src/router.ts b/src/router.ts index 9f32645f..aca64d92 100644 --- a/src/router.ts +++ b/src/router.ts @@ -271,7 +271,7 @@ export function createRouter({ return pushWithRedirect(error.to, redirectedFrom || toLocation) } - // unknown error + // unknown error, throws triggerError(error) } @@ -448,31 +448,27 @@ export function createRouter({ false ) } catch (error) { + // if a different navigation is happening, abort this one + if (pendingLocation !== toLocation) { + return triggerError( + createRouterError(ErrorTypes.NAVIGATION_CANCELLED, { + from, + to: toLocation, + }), + false + ) + } + if (error.type === ErrorTypes.NAVIGATION_GUARD_REDIRECT) { - // TODO: refactor the duplication of new NavigationCancelled by - // checking instanceof NavigationError (it's another TODO) - // a more recent navigation took place - if (pendingLocation !== toLocation) { - return triggerError( - createRouterError( - ErrorTypes.NAVIGATION_CANCELLED, - { - from, - to: toLocation, - } - ), - false - ) - } - triggerError(error, false) + // the error is already handled by router.push we just want to avoid + // logging the error + return pushWithRedirect(error.to, toLocation).catch(() => {}) + } - // the error is already handled by router.push - // we just want to avoid logging the error - pushWithRedirect(error.to, toLocation).catch(() => {}) - } else if (error.type === ErrorTypes.NAVIGATION_ABORTED) { - // TODO: test on different browsers ensure consistent behavior - history.go(-info.distance, false) - } else { + // TODO: test on different browsers ensure consistent behavior + history.go(-info.distance, false) + // unrecognized error, transfer to the global handler + if (error.type !== ErrorTypes.NAVIGATION_ABORTED) { triggerError(error, false) } }