Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/history-hash-allocations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/history': patch
---

Avoid temporary arrays when reading hash-history locations.
16 changes: 9 additions & 7 deletions packages/history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,15 @@ export function createHashHistory(opts?: { window?: any }): RouterHistory {
return createBrowserHistory({
window: win,
parseLocation: () => {
const hashSplit = win.location.hash.split('#').slice(1)
const pathPart = hashSplit[0] ?? '/'
const searchPart = win.location.search
const hashEntries = hashSplit.slice(1)
const hashPart =
hashEntries.length === 0 ? '' : `#${hashEntries.join('#')}`
const hashHref = `${pathPart}${searchPart}${hashPart}`
const hash = win.location.hash
const pathStart = hash.indexOf('#') + 1
const hashIndex = hash.indexOf('#', pathStart)
const pathPart =
pathStart === 0
? '/'
: hash.slice(pathStart, hashIndex === -1 ? undefined : hashIndex)
Comment thread
LadyBluenotes marked this conversation as resolved.
const hashPart = hashIndex === -1 ? '' : hash.slice(hashIndex)
const hashHref = `${pathPart}${win.location.search}${hashPart}`
return parseHref(hashHref, win.history.state)
},
createHref: (href) =>
Expand Down
72 changes: 72 additions & 0 deletions packages/history/tests/createHashHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,78 @@ describe('createHashHistory', () => {
history.destroy()
})

test.each([
[
'/?shell=1#/hello#section#tail',
{
href: '/hello?shell=1#section#tail',
pathname: '/hello',
search: '?shell=1',
hash: '#section#tail',
},
],
[
'/?shell=1#/hello?route=2#section',
{
href: '/hello?route=2?shell=1#section',
pathname: '/hello',
search: '?route=2?shell=1',
hash: '#section',
},
],
[
'/#/hello##tail#',
{
href: '/hello##tail#',
pathname: '/hello',
search: '',
hash: '##tail#',
},
],
[
'/#/hello%23nested?value=%23#anchor%23tail',
{
href: '/hello%23nested?value=%23#anchor%23tail',
pathname: '/hello%23nested',
search: '?value=%23',
hash: '#anchor%23tail',
},
],
[
'/?shell=1#//evil.example/path#fragment',
{
href: '/evil.example/path?shell=1#fragment',
pathname: '/evil.example/path',
search: '?shell=1',
hash: '#fragment',
},
],
[
'/?shell=1#',
{
href: '/?shell=1',
pathname: '/',
search: '?shell=1',
hash: '',
},
],
])('preserves the logical URL when reading %s', (href, expected) => {
const originalHref = window.location.href
const originalState = window.history.state
window.history.replaceState(null, '', href)
const history = createHashHistory()

try {
expect(history.location).toMatchObject(expected)
window.history.replaceState(window.history.state, '', '/shell')
window.history.pushState(window.history.state, '', href)
expect(history.location).toMatchObject(expected)
} finally {
history.destroy()
window.history.replaceState(originalState, '', originalHref)
}
})

describe('parseLocation', () => {
describe.each([
['/', { pathname: '/', search: '' }, 'neither search params nor hash'],
Expand Down
Loading