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

Support hashRoot in HashHistoryOptions #911

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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
6 changes: 6 additions & 0 deletions packages/history/__tests__/hash-root-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import expect from 'expect';
import { testHashHistory } from './hash-test.js';

describe('a hash history with no slash', () => {
testHashHistory('#', {hashRoot: ""})
});
19 changes: 15 additions & 4 deletions packages/history/__tests__/hash-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import expect from 'expect';
import { createHashHistory } from 'history';
import { createHashHistory, createPath } from 'history';

import Listen from './TestSequences/Listen.js';
import InitialLocationDefaultKey from './TestSequences/InitialLocationDefaultKey.js';
Expand All @@ -21,12 +21,19 @@ import BlockPopWithoutListening from './TestSequences/BlockPopWithoutListening.j
// const canGoWithoutReload = window.navigator.userAgent.indexOf('Firefox') === -1;
// const describeGo = canGoWithoutReload ? describe : describe.skip;

describe('a hash history', () => {
export const testHashHistory = (initialRoot, options) => {
Copy link
Author

@thejohnhoffer thejohnhoffer Dec 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can test both createHashHistory() and createHashHistory({hashRoot: ""})

let history;
beforeEach(() => {
window.history.replaceState(null, null, '#/');
history = createHashHistory();
window.history.replaceState(null, null, initialRoot);
history = createHashHistory(options);
});
afterEach(() => {
// Test window location at every step
const { hashRoot = "/" } = options || {};
const historyHref = createPath(history.location);
const windowHref = window.location.hash.substr(1);
expect(historyHref.replace(/^\//, hashRoot)).toEqual(windowHref);
Comment on lines +32 to +35
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solves issue #918

})

it('knows how to create hrefs from location objects', () => {
const href = history.createHref({
Expand Down Expand Up @@ -144,4 +151,8 @@ describe('a hash history', () => {
BlockPopWithoutListening(history, done);
});
});
}

describe('a hash history', () => {
testHashHistory('#/')
});
17 changes: 14 additions & 3 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,23 @@ export function createHashHistory(
let { window = document.defaultView! } = options;
let globalHistory = window.history;

let { hashRoot = '/' } = options;

function prefixPathname([base, root]: string[], partial: Partial<Path>) {
const input = partial.pathname || base;
return input.match(/^\.+\//) ? partial : {
...partial, pathname: input.replace(base, root)
};
}
const pathFromGlobal = prefixPathname.bind(null, [hashRoot, '/']);
const pathToGlobal = prefixPathname.bind(null, ['/', hashRoot]);

function getIndexAndLocation(): [number, Location] {
let {
pathname = '/',
search = '',
hash = ''
} = parsePath(window.location.hash.substr(1));
} = pathFromGlobal(parsePath(window.location.hash.substr(1)));
let state = globalHistory.state || {};
return [
state.idx,
Expand Down Expand Up @@ -714,7 +725,7 @@ export function createHashHistory(
key: nextLocation.key,
idx: index
},
createHref(nextLocation)
createHref(pathToGlobal(nextLocation))
];
}

Expand Down