From 3f7919585117048c379b6dee8af1cc1de5996af0 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 1 May 2020 10:00:17 +0200 Subject: [PATCH] fix(scroll): do not restore on push --- e2e/specs/scroll-behavior.js | 25 +++++++++++++++++++++++++ src/router.ts | 8 +++++--- 2 files changed, 30 insertions(+), 3 deletions(-) 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, -- 2.47.3