Skip to content

Commit

Permalink
fix: hash history link generation
Browse files Browse the repository at this point in the history
Fixes #457
  • Loading branch information
tannerlinsley committed Feb 6, 2023
1 parent 671eaeb commit beb1e7b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/react/basic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RootRoute,
Route,
ErrorComponent,
createHashHistory,
} from '@tanstack/react-router'
import {
Loader,
Expand Down Expand Up @@ -208,8 +209,11 @@ const routeTree = rootRoute.addChildren([
indexRoute,
])

const hashHistory = createHashHistory()

// Set up a ReactRouter instance
const router = new ReactRouter({
history: hashHistory,
routeTree,
defaultPreload: 'intent',
})
Expand Down
5 changes: 5 additions & 0 deletions packages/router/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface RouterHistory {
go: (index: number) => void
back: () => void
forward: () => void
createHref: (href: string) => string
}

export interface ParsedPath {
Expand All @@ -33,6 +34,7 @@ function createHistory(opts: {
go: (n: number) => void
back: () => void
forward: () => void
createHref: (path: string) => string
}): RouterHistory {
let currentLocation = opts.getLocation()
let unsub = () => {}
Expand Down Expand Up @@ -81,6 +83,7 @@ function createHistory(opts: {
opts.forward()
onUpdate()
},
createHref: (str) => opts.createHref(str),
}
}

Expand Down Expand Up @@ -120,6 +123,7 @@ export function createBrowserHistory(opts?: {
back: () => window.history.back(),
forward: () => window.history.forward(),
go: (n) => window.history.go(n),
createHref: (path) => createHref(path),
})
}

Expand Down Expand Up @@ -171,6 +175,7 @@ export function createMemoryHistory(
index = Math.min(index + 1, entries.length - 1)
},
go: (n) => window.history.go(n),
createHref: (path) => path,
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ export class Router<
searchStr,
state: nextState,
hash,
href: `${pathname}${searchStr}${hash}`,
href: this.history.createHref(`${pathname}${searchStr}${hash}`),
key: dest.key,
}
}
Expand Down

0 comments on commit beb1e7b

Please sign in to comment.