From: Eduardo San Martin Morote Date: Thu, 2 Jul 2020 20:37:31 +0000 (+0200) Subject: fix(hash): manual changes should trigger a navigation X-Git-Tag: v4.0.0-beta.1~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93891abf02fc24d66c6f43926a28f275560fb714;p=thirdparty%2Fvuejs%2Frouter.git fix(hash): manual changes should trigger a navigation Close #346 --- diff --git a/e2e/hash/index.ts b/e2e/hash/index.ts index 4f7e34af..68e61657 100644 --- a/e2e/hash/index.ts +++ b/e2e/hash/index.ts @@ -6,8 +6,8 @@ const Home: RouteComponent = { template: `
home
`, } -const Foo: RouteComponent = { template: '
foo
' } -const Bar: RouteComponent = { template: '
bar
' } +const Foo: RouteComponent = { template: '
Foo
' } +const Bar: RouteComponent = { template: '
Bar
' } const Unicode: RouteComponent = { setup() { @@ -65,6 +65,7 @@ const app = createApp({ router) +
  • /foo (regular hash)
  • @@ -75,7 +76,7 @@ const app = createApp({ hash: {{ route.hash }}

    - + `, }) diff --git a/e2e/specs/hash.js b/e2e/specs/hash.js index 3ad0c71c..5d381988 100644 --- a/e2e/specs/hash.js +++ b/e2e/specs/hash.js @@ -23,6 +23,7 @@ module.exports = { ) .click('li:nth-child(3) a') .assert.urlEquals(baseURL + '/bar') + .assert.containsText('.view', 'Bar') .click('li:nth-child(2) a') .assert.urlEquals(baseURL + '/foo') .click('li:nth-child(4) a') @@ -38,6 +39,12 @@ module.exports = { .click('li:nth-child(5) a') .assert.containsText('#param', 'é') + // regular links should not break navigation + .click('li:nth-child(10) a') + .assert.urlEquals(baseURL + '/foo') + .assert.containsText('#path', '/foo') + .assert.containsText('.view', 'Foo') + .end() }, diff --git a/src/history/html5.ts b/src/history/html5.ts index 1a2081b0..28187561 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -71,21 +71,24 @@ function useHistoryListeners( state: StateEntry | null }) => { const to = createCurrentLocation(base, window.location) - - if (!state) return replace(to.fullPath) - const from: HistoryLocationNormalized = location.value const fromState: StateEntry = historyState.value - location.value = to - historyState.value = state + let delta = 0 + + if (state) { + location.value = to + historyState.value = state - // ignore the popstate and reset the pauseState - if (pauseState && pauseState.fullPath === from.fullPath) { - pauseState = null - return + // ignore the popstate and reset the pauseState + if (pauseState && pauseState.fullPath === from.fullPath) { + pauseState = null + return + } + delta = fromState ? state.position - fromState.position : 0 + } else { + replace(to.fullPath) } - const delta = fromState ? state.position - fromState.position : 0 // console.log({ deltaFromCurrent }) // Here we could also revert the navigation by calling history.go(-delta) // this listener will have to be adapted to not trigger again and to wait for the url diff --git a/src/router.ts b/src/router.ts index fcd1c387..2ef0b8bc 100644 --- a/src/router.ts +++ b/src/router.ts @@ -755,7 +755,8 @@ export function createRouter(options: RouterOptions): Router { if ( isNavigationFailure(error, ErrorTypes.NAVIGATION_GUARD_REDIRECT) ) { - routerHistory.go(-info.delta, false) + // do not restore history on unknown direction + if (info.delta) routerHistory.go(-info.delta, false) // the error is already handled by router.push we just want to avoid // logging the error pushWithRedirect( @@ -766,8 +767,8 @@ export function createRouter(options: RouterOptions): Router { // avoid the then branch return Promise.reject() } - // TODO: test on different browsers ensure consistent behavior - routerHistory.go(-info.delta, false) + // do not restore history on unknown direction + if (info.delta) routerHistory.go(-info.delta, false) // unrecognized error, transfer to the global handler return triggerError(error) }) @@ -782,7 +783,7 @@ export function createRouter(options: RouterOptions): Router { ) // revert the navigation - if (failure) routerHistory.go(-info.delta, false) + if (failure && info.delta) routerHistory.go(-info.delta, false) triggerAfterEach( toLocation as RouteLocationNormalizedLoaded,