From 57d8042c8b99f92bfe35493b8ae9bba827864bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryan=20Klarh=C3=B6lter?= Date: Fri, 8 Oct 2021 14:12:15 +0200 Subject: [PATCH] fix(scrollBehavior): trigger scroll behavior if same route with hash (#3592) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ryan Klarhölter --- src/history/base.js | 4 ++++ test/e2e/specs/scroll-behavior.js | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/history/base.js b/src/history/base.js index f96e4d9ce..4751cd97a 100644 --- a/src/history/base.js +++ b/src/history/base.js @@ -20,6 +20,7 @@ import { isNavigationFailure, NavigationFailureType } from '../util/errors' +import { handleScroll } from '../util/scroll' export class History { router: Router @@ -163,6 +164,9 @@ export class History { route.matched[lastRouteIndex] === current.matched[lastCurrentIndex] ) { this.ensureURL() + if (route.hash) { + handleScroll(this.router, current, route, false) + } return abort(createNavigationDuplicatedError(current, route)) } diff --git a/test/e2e/specs/scroll-behavior.js b/test/e2e/specs/scroll-behavior.js index db317d591..d7469e9e0 100644 --- a/test/e2e/specs/scroll-behavior.js +++ b/test/e2e/specs/scroll-behavior.js @@ -94,14 +94,19 @@ module.exports = { 'scroll to top on new entry' ) - .click('li:nth-child(4) a') - .assert.evaluate( - function () { - return document.getElementById('anchor').getBoundingClientRect().top < 1 - }, - null, - 'scroll to anchor' - ) + .perform(() => { + for (let i = 0; i < 2; i++) { + browser + .click('li:nth-child(4) a') + .assert.evaluate( + function () { + return document.getElementById('anchor').getBoundingClientRect().top < 1 + }, + null, + (i === 0) ? 'scroll to anchor' : 'scroll to same anchor again' + ) + } + }) .click('li:nth-child(5) a') .assert.evaluate(