]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(scroll): do not restore on push
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 08:00:17 +0000 (10:00 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 08:00:17 +0000 (10:00 +0200)
e2e/specs/scroll-behavior.js
src/router.ts

index 5e7b53bd96911f351083cda12895fb7820233167..7ff21e3bd5cfcd0b4e70c6ba800a281d36586e27 100644 (file)
@@ -152,6 +152,31 @@ module.exports = {
         'restores scroll position when reloading'
       )
 
+      // going again to a popped entry should not restore the saved position
+      .click('li:nth-child(1) a')
+      .waitForElementPresent('.view.home', TIMEOUT)
+      .click('li:nth-child(4) a')
+      .waitForElementPresent('.view.bar', TIMEOUT)
+      // at this point we scrolled to the anchor, scroll again somewhere else
+      // and then go back
+      .execute(function () {
+        window.scrollTo(0, 100)
+        window.history.back()
+      })
+      .waitForElementPresent('.view.home', TIMEOUT)
+      // go to the same location again but without using history.forward
+      .click('li:nth-child(4) a')
+      .waitForElementPresent('.view.bar', TIMEOUT)
+      .assert.evaluate(
+        function () {
+          return (
+            document.getElementById('anchor').getBoundingClientRect().top < 1
+          )
+        },
+        null,
+        'scroll to anchor'
+      )
+
       .end()
   },
 }
index 768ae9e46b619fd598d988717745a64452f9299d..4cabdfb41dd169e9ae0f14d4de702c67e3443b66 100644 (file)
@@ -573,9 +573,11 @@ export function createRouter(options: RouterOptions): Router {
     // duplicated navigation (e.g. same anchor navigation). It needs exposing
     // the navigation information (type, direction)
     if (isBrowser) {
-      const savedScroll = getSavedScrollPosition(
-        getScrollKey(toLocation.fullPath, 0)
-      )
+      // if we are pushing, we cannot have a saved position. This is important
+      // when visiting /b from /a, scrolling, going back to /a by with the back
+      // button and then clicking on a link to /b instead of the forward button
+      const savedScroll =
+        !isPush && getSavedScrollPosition(getScrollKey(toLocation.fullPath, 0))
       handleScroll(
         toLocation,
         from,