Skip to content

Commit

Permalink
fix(hash): manual changes should trigger a navigation
Browse files Browse the repository at this point in the history
Close #346
  • Loading branch information
posva committed Jul 2, 2020
1 parent bb9d0ee commit 93891ab
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
7 changes: 4 additions & 3 deletions e2e/hash/index.ts
Expand Up @@ -6,8 +6,8 @@ const Home: RouteComponent = {
template: `<div>home</div>`,
}

const Foo: RouteComponent = { template: '<div>foo</div>' }
const Bar: RouteComponent = { template: '<div>bar</div>' }
const Foo: RouteComponent = { template: '<div>Foo</div>' }
const Bar: RouteComponent = { template: '<div>Bar</div>' }

const Unicode: RouteComponent = {
setup() {
Expand Down Expand Up @@ -65,6 +65,7 @@ const app = createApp({
router)</a
>
</li>
<li><a href="#/foo">/foo (regular hash)</a></li>
</ul>
<p>
Expand All @@ -75,7 +76,7 @@ const app = createApp({
hash: <code id="hash">{{ route.hash }}</code>
</p>
<router-view></router-view>
<router-view class="view"></router-view>
</div>
`,
})
Expand Down
7 changes: 7 additions & 0 deletions e2e/specs/hash.js
Expand Up @@ -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')
Expand All @@ -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()
},

Expand Down
23 changes: 13 additions & 10 deletions src/history/html5.ts
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/router.ts
Expand Up @@ -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(
Expand All @@ -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)
})
Expand All @@ -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,
Expand Down

0 comments on commit 93891ab

Please sign in to comment.