-
Notifications
You must be signed in to change notification settings - Fork 961
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
thejohnhoffer
wants to merge
18
commits into
remix-run:dev
Choose a base branch
from
thejohnhoffer:main-for-react-router-noslash
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
205d7cc
Update README.md
thejohnhoffer 5a3bf6b
Update README.md
thejohnhoffer abb5625
Update README.md
thejohnhoffer e796aeb
Update README.md
thejohnhoffer ee3158c
Merge branch 'dev' into main-for-react-router-noslash
thejohnhoffer 59580ad
simple solution for hashRoot parsing
thejohnhoffer 34db1b5
add basic tests for hashRoot=""
thejohnhoffer 78da42b
simplify further
thejohnhoffer 45e710e
improve tests
thejohnhoffer f595287
restore original tests
thejohnhoffer 2bf3314
use original PushRelativePathnameWarning test
thejohnhoffer 821d40c
handle all possible types for To
thejohnhoffer 51a33ef
improve tests
thejohnhoffer 077cc5f
minor change to ensure pathname overwrites
thejohnhoffer 102165a
handle relative pathnames
thejohnhoffer ec995a2
one fewer line
thejohnhoffer 7f8c9d9
only ignore prefix when path matches ../
thejohnhoffer 4afa6e5
Merge branch 'dev' into main-for-react-router-noslash
thejohnhoffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ""}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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) => { | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
|
@@ -144,4 +151,8 @@ describe('a hash history', () => { | |
BlockPopWithoutListening(history, done); | ||
}); | ||
}); | ||
} | ||
|
||
describe('a hash history', () => { | ||
testHashHistory('#/') | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
}; | ||
Comment on lines
+596
to
+598
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interchange This solves issue #912. |
||
} | ||
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, | ||
|
@@ -714,7 +725,7 @@ export function createHashHistory( | |
key: nextLocation.key, | ||
idx: index | ||
}, | ||
createHref(nextLocation) | ||
createHref(pathToGlobal(nextLocation)) | ||
]; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()
andcreateHashHistory({hashRoot: ""})