Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hash mode deep links (#2125) #2262

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/hash-scroll-behavior/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const scrollBehavior = (to, from, savedPosition) => {

const router = new VueRouter({
mode: 'hash',
base: __dirname,
scrollBehavior,
routes: [
{ path: '/', component: Home, meta: { scrollToTop: true }},
Expand Down
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h1>Vue Router Examples</h1>
<li><a href="data-fetching">Data Fetching</a></li>
<li><a href="navigation-guards">Navigation Guards</a></li>
<li><a href="scroll-behavior">Scroll Behavior</a></li>
<li><a href="hash-scroll-behavior">Hash Scroll Behavior</a></li>
<li><a href="lazy-loading">Lazy Loading</a></li>
<li><a href="auth-flow">Auth Flow</a></li>
<li><a href="discrete-components">Discrete Components</a></li>
Expand Down
15 changes: 9 additions & 6 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { setupScroll, handleScroll } from '../util/scroll'
import { pushState, replaceState, supportsPushState } from '../util/push-state'

export class HashHistory extends History {
constructor (router: Router, base: ?string, fallback: boolean) {
constructor (router: Router, base: ?string) {
super(router, base)
// check history fallback deeplinking
if (fallback && checkFallback(this.base)) {
if (checkFallback(this.base)) {
return
}
ensureSlash()
Expand Down Expand Up @@ -80,10 +80,13 @@ export class HashHistory extends History {

function checkFallback (base) {
const location = getLocation(base)
if (!/^\/#/.test(location)) {
window.location.replace(
cleanPath(base + '/#' + location)
)
if (location !== '/' && !/^\/#/.test(location)) {
const path = cleanPath(base + '/#' + location)
if (supportsPushState) {
replaceState(path)
} else {
window.location.replace(path)
}
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class VueRouter {
this.history = new HTML5History(this, options.base)
break
case 'hash':
this.history = new HashHistory(this, options.base, this.fallback)
this.history = new HashHistory(this, options.base)
break
case 'abstract':
this.history = new AbstractHistory(this, options.base)
Expand Down
8 changes: 7 additions & 1 deletion test/e2e/specs/hash-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ module.exports = {
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar')
.assert.containsText('.view', 'bar')

// check initial visit
// check initial visit
.url('http://localhost:8080/hash-mode/#/foo')
.waitForElementVisible('#app', 1000)
.assert.containsText('.view', 'foo')
.url('http://localhost:8080/hash-mode/#/%C3%A9')
.waitForElementVisible('#app', 1000)
.assert.containsText('.view', 'unicode')

// check hash placed correctly
.url('http://localhost:8080/hash-mode/foo?page=123')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/foo?page=123')
.assert.containsText('.view', 'foo')
.end()
}
}