From ba40b8f0cf2d6d85533e0e7e7daaadd088298f19 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 19 Mar 2020 23:27:15 +0100 Subject: [PATCH] fix(hash): fix base position for hash routing --- src/history/hash.ts | 2 +- src/history/html5.ts | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/history/hash.ts b/src/history/hash.ts index 08a6cae07..baff03419 100644 --- a/src/history/hash.ts +++ b/src/history/hash.ts @@ -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 + '/#') } diff --git a/src/history/html5.ts b/src/history/html5.ts index 637b7523f..290e04383 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -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) @@ -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)