Skip to content

Commit

Permalink
fix(hash): correctly place query if placed before hash (#2851)
Browse files Browse the repository at this point in the history
Fixes #2125 
Closes #2262
  • Loading branch information
posva authored Jul 16, 2019
1 parent d8499b6 commit b7715dc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
81 changes: 49 additions & 32 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,49 @@ export class HashHistory extends History {
setupScroll()
}

window.addEventListener(supportsPushState ? 'popstate' : 'hashchange', () => {
const current = this.current
if (!ensureSlash()) {
return
}
this.transitionTo(getHash(), route => {
if (supportsScroll) {
handleScroll(this.router, route, current, true)
}
if (!supportsPushState) {
replaceHash(route.fullPath)
window.addEventListener(
supportsPushState ? 'popstate' : 'hashchange',
() => {
const current = this.current
if (!ensureSlash()) {
return
}
})
})
this.transitionTo(getHash(), route => {
if (supportsScroll) {
handleScroll(this.router, route, current, true)
}
if (!supportsPushState) {
replaceHash(route.fullPath)
}
})
}
)
}

push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(location, route => {
pushHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
this.transitionTo(
location,
route => {
pushHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
},
onAbort
)
}

replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(location, route => {
replaceHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
this.transitionTo(
location,
route => {
replaceHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
},
onAbort
)
}

go (n: number) {
Expand All @@ -81,9 +92,7 @@ export class HashHistory extends History {
function checkFallback (base) {
const location = getLocation(base)
if (!/^\/#/.test(location)) {
window.location.replace(
cleanPath(base + '/#' + location)
)
window.location.replace(cleanPath(base + '/#' + location))
return true
}
}
Expand Down Expand Up @@ -112,20 +121,28 @@ export function getHash (): string {
const searchIndex = href.indexOf('?')
if (searchIndex < 0) {
const hashIndex = href.indexOf('#')
if (hashIndex > -1) href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
else href = decodeURI(href)
if (hashIndex > -1) {
href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
} else href = decodeURI(href)
} else {
if (searchIndex > -1) href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
if (searchIndex > -1) {
href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
}
}

return href
}

function getUrl (path) {
const href = window.location.href
const i = href.indexOf('#')
const base = i >= 0 ? href.slice(0, i) : href
return `${base}#${path}`
const hashPos = href.indexOf('#')
let base = hashPos > -1 ? href.slice(0, hashPos) : href

const searchPos = base.indexOf('?')
const query = searchPos > -1 ? base.slice(searchPos) : ''

This comment has been minimized.

Copy link
@crawn420

crawn420 Aug 7, 2019

Don't do this,!!! The mothed getURL must be used in get new router. When we want to retain the param in redirected url, we have no chance.
哥哥,不要这样搞啊!!!这个方法一定在路由push或重定向中用到了!!!你这样改,路径中保留身份token复制到其他窗体以便直接实现登录并跳转页面,无法搞了啊!!!!

This comment has been minimized.

Copy link
@posva

posva Aug 7, 2019

Author Member

@crawn420 what is the url that fails for you? This was fixing #2125

This comment has been minimized.

Copy link
@yangshuan

yangshuan Aug 8, 2019

@crawn420 what is the url that fails for you? This was fixing #2125

The same issue appears again, see #2876

base = query ? base.slice(0, searchPos) : base

return `${base}#${path + query}`
}

function pushHash (path) {
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/specs/hash-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ module.exports = {
.waitForElementVisible('#app', 1000)
.assert.containsText('.view', 'unicode: ñ')
.assert.containsText('#query-t', '%')

// correctly placing query
// https://github.com/vuejs/vue-router/issues/2125
.url('http://localhost:8080/hash-mode/?key=foo')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/?key=foo')
.url('http://localhost:8080/hash-mode?key=foo')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/?key=foo')
.url('http://localhost:8080/hash-mode?key=foo#other')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/other?key=foo')
.end()
}
}

0 comments on commit b7715dc

Please sign in to comment.