@@ -573,7 +573,7 @@ export function createBrowserHistory(
573573// HASH
574574////////////////////////////////////////////////////////////////////////////////
575575
576- export type HashHistoryOptions = { window ?: Window } ;
576+ export type HashHistoryOptions = { window ?: Window , hashRoot ?: string } ;
577577
578578/**
579579 * Hash history stores the location in window.location.hash. This makes it ideal
@@ -589,12 +589,26 @@ export function createHashHistory(
589589 let { window = document . defaultView ! } = options ;
590590 let globalHistory = window . history ;
591591
592+ let { hashRoot = "/" } = options ;
593+
594+ function parsePathInput ( pathname ) {
595+ return parsePath ( pathname . replace ( hashRoot , "/" ) ) ;
596+ }
597+
598+ function parsePathOutput ( pathname ) {
599+ return parsePath ( pathname . replace ( / ^ \/ / , hashRoot ) ) ;
600+ }
601+
602+ function validateLocation ( { pathname} ) {
603+ return pathname === parsePathOutput ( pathname )
604+ }
605+
592606 function getIndexAndLocation ( ) : [ number , Location ] {
593607 let {
594608 pathname = '/' ,
595609 search = '' ,
596610 hash = ''
597- } = parsePath ( window . location . hash . substr ( 1 ) ) ;
611+ } = parsePathInput ( window . location . hash . substr ( 1 ) ) ;
598612 let state = globalHistory . state || { } ;
599613 return [
600614 state . idx ,
@@ -698,7 +712,7 @@ export function createHashHistory(
698712 pathname : location . pathname ,
699713 hash : '' ,
700714 search : '' ,
701- ...( typeof to === 'string' ? parsePath ( to ) : to ) ,
715+ ...( typeof to === 'string' ? parsePathOutput ( to ) : to ) ,
702716 state,
703717 key : createKey ( )
704718 } ) ;
@@ -738,8 +752,8 @@ export function createHashHistory(
738752 }
739753
740754 warning (
741- nextLocation . pathname . charAt ( 0 ) === '/' ,
742- `Relative pathnames are not supported in hash history.push(${ JSON . stringify (
755+ validateLocation ( nextLocation ) ,
756+ `Locations must start with " ${ hashRoot } " in hash history.push(${ JSON . stringify (
743757 to
744758 ) } )`
745759 ) ;
@@ -769,8 +783,8 @@ export function createHashHistory(
769783 }
770784
771785 warning (
772- nextLocation . pathname . charAt ( 0 ) === '/' ,
773- `Relative pathnames are not supported in hash history.replace(${ JSON . stringify (
786+ validateLocation ( nextLocation ) ,
787+ `Locations must start with " ${ hashRoot } " in hash history.replace(${ JSON . stringify (
774788 to
775789 ) } )`
776790 ) ;
0 commit comments