Skip to content

Commit

Permalink
fix(hash): fix base position for hash routing
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 19, 2020
1 parent a3e0be9 commit ba40b8f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/history/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import createWebHistory from './html5'

export default function createWebHashHistory(base: string = ''): RouterHistory {
// Make sure this implementation is fine in terms of encoding, specially for IE11
return createWebHistory('/#' + base)
return createWebHistory(base + '/#')
}
9 changes: 7 additions & 2 deletions src/history/html5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ function createCurrentLocation(
): HistoryLocationNormalized {
const { pathname, search, hash } = location
// allows hash based url
if (base.indexOf('#') > -1) {
const hashPos = base.indexOf('#')
if (hashPos > -1) {
// prepend the starting slash to hash so the url starts with /#
return normalizeHistoryLocation(stripBase('/' + hash, base))
let pathFromHash = hash.slice(1)
if (pathFromHash.charAt(0) !== '/') pathFromHash = '/' + pathFromHash
return normalizeHistoryLocation(stripBase(pathFromHash, ''))
}
const path = stripBase(pathname, base)
return normalizeHistoryLocation(path + search + hash)
Expand All @@ -59,6 +62,8 @@ function useHistoryListeners(
}: {
state: StateEntry
}) => {
// TODO: state can be null when using links with a `hash` in hash mode
// maybe we should trigger a plain navigation in that case
cs.info('popstate fired', state)
cs.info('currentState', historyState)

Expand Down

0 comments on commit ba40b8f

Please sign in to comment.