]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: revert history navigation if navigation is cancelled
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 16:13:59 +0000 (18:13 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 16:13:59 +0000 (18:13 +0200)
src/router.ts

index 9f32645fee0c3d0efc428ba0b6cc5a06a1c90a26..aca64d920c1de473a2bd7fae663711ccc4faa239 100644 (file)
@@ -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<NavigationError>(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<NavigationError>(
-              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)
       }
     }