From: Eduardo San Martin Morote Date: Fri, 1 May 2020 08:00:17 +0000 (+0200) Subject: fix(scroll): do not restore on push X-Git-Tag: v4.0.0-alpha.10~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f7919585117048c379b6dee8af1cc1de5996af0;p=thirdparty%2Fvuejs%2Frouter.git fix(scroll): do not restore on push --- diff --git a/e2e/specs/scroll-behavior.js b/e2e/specs/scroll-behavior.js index 5e7b53bd..7ff21e3b 100644 --- a/e2e/specs/scroll-behavior.js +++ b/e2e/specs/scroll-behavior.js @@ -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() }, } diff --git a/src/router.ts b/src/router.ts index 768ae9e4..4cabdfb4 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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,