Skip to content

Commit

Permalink
simple solution for hashRoot parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnhoffer committed Dec 20, 2021
1 parent ee3158c commit 59580ad
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions packages/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ export function createBrowserHistory(
// HASH
////////////////////////////////////////////////////////////////////////////////

export type HashHistoryOptions = { window?: Window };
export type HashHistoryOptions = { window?: Window, hashRoot?: string };

/**
* Hash history stores the location in window.location.hash. This makes it ideal
Expand All @@ -589,12 +589,26 @@ export function createHashHistory(
let { window = document.defaultView! } = options;
let globalHistory = window.history;

let { hashRoot = "/" } = options;

function parsePathInput(pathname) {
return parsePath(pathname.replace(hashRoot, "/"));
}

function parsePathOutput(pathname) {
return parsePath(pathname.replace(/^\//, hashRoot));
}

function validateLocation({pathname}) {
return pathname === parsePathOutput(pathname)
}

function getIndexAndLocation(): [number, Location] {
let {
pathname = '/',
search = '',
hash = ''
} = parsePath(window.location.hash.substr(1));
} = parsePathInput(window.location.hash.substr(1));
let state = globalHistory.state || {};
return [
state.idx,
Expand Down Expand Up @@ -698,7 +712,7 @@ export function createHashHistory(
pathname: location.pathname,
hash: '',
search: '',
...(typeof to === 'string' ? parsePath(to) : to),
...(typeof to === 'string' ? parsePathOutput(to) : to),
state,
key: createKey()
});
Expand Down Expand Up @@ -738,8 +752,8 @@ export function createHashHistory(
}

warning(
nextLocation.pathname.charAt(0) === '/',
`Relative pathnames are not supported in hash history.push(${JSON.stringify(
validateLocation(nextLocation),
`Locations must start with "${hashRoot}" in hash history.push(${JSON.stringify(
to
)})`
);
Expand Down Expand Up @@ -769,8 +783,8 @@ export function createHashHistory(
}

warning(
nextLocation.pathname.charAt(0) === '/',
`Relative pathnames are not supported in hash history.replace(${JSON.stringify(
validateLocation(nextLocation),
`Locations must start with "${hashRoot}" in hash history.replace(${JSON.stringify(
to
)})`
);
Expand Down

0 comments on commit 59580ad

Please sign in to comment.