]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: setup history listeners once
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 16 Mar 2022 15:44:44 +0000 (16:44 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 16 Mar 2022 15:44:44 +0000 (16:44 +0100)
Fix #1344

src/router.ts

index 9a87439b00e500c7d8d29c5c22d5fdfbd3c567c6..dee1f9b9df4af4e993b91a4768d35395ff64a253 100644 (file)
@@ -938,9 +938,11 @@ export function createRouter(options: RouterOptions): Router {
     markAsReady()
   }
 
-  let removeHistoryListener: () => void | undefined
+  let removeHistoryListener: undefined | null | (() => void)
   // attach listener to history to trigger navigations
   function setupListeners() {
+    // avoid setting up listeners twice due to an invalid first navigation
+    if (removeHistoryListener) return
     removeHistoryListener = routerHistory.listen((to, _from, info) => {
       // cannot be a redirect route because it was in history
       const toLocation = resolve(to) as RouteLocationNormalized
@@ -1220,6 +1222,7 @@ export function createRouter(options: RouterOptions): Router {
           // invalidate the current navigation
           pendingLocation = START_LOCATION_NORMALIZED
           removeHistoryListener && removeHistoryListener()
+          removeHistoryListener = null
           currentRoute.value = START_LOCATION_NORMALIZED
           started = false
           ready = false