Skip to main content

History

History management for browser navigation, including browser history, hash history, and memory history.

RouterHistory

The history interface used by TanStack Router.
HistoryLocation
The current location object.
number
Number of entries in the history stack.
(path: string, state?: any) => void
Push a new entry onto the history stack.
(path: string, state?: any) => void
Replace the current history entry.
(delta: number) => void
Move through the history stack by a delta.
() => void
Go back one entry in the history stack.
() => void
Go forward one entry in the history stack.
(callback: (event) => void) => () => void
Subscribe to history changes. Returns unsubscribe function.
(blocker: NavigationBlocker) => () => void
Block navigation and show confirmation. Returns unblock function.
(path: string) => string
Create an href string from a path.

HistoryLocation

Represents a location in the history stack.
string
The full URL string.
string
The path portion of the URL.
The search/query string portion of the URL.
string
The hash portion of the URL.
HistoryState
State associated with this history entry.

createBrowserHistory

Create a history object that uses the browser’s History API.

Options

Window
The window object to use. Defaults to global window.

createHashHistory

Create a history object that uses URL hash for navigation.
Use case: Useful for apps hosted on static file servers or when you can’t configure server-side routing.

Options

Window
The window object to use. Defaults to global window.

createMemoryHistory

Create a history object that stores navigation in memory (no browser integration).
Use case: Testing, server-side rendering, or non-browser environments.

Options

string[]
Initial history entries.Default: ['/']
number
Initial position in the history stack.Default: 0
Interface for blocking navigation.
BlockerFn
required
Function called when navigation is attempted.
boolean | (() => boolean)
Enable browser’s beforeunload event.

BlockerFn

Function signature for navigation blockers.
HistoryLocation
The current location.
HistoryLocation
The location being navigated to.
'PUSH' | 'REPLACE' | 'POP'
The type of navigation action.
Returns: boolean | Promise<boolean> - True to allow navigation, false to block.

Examples

Basic Browser History

Hash History for Static Hosting

Memory History for Testing

Custom History Events

Programmatic Navigation

Server-Side Rendering

Custom History Implementation