Skip to main content
TanStack Router provides a lightweight history API for managing browser navigation. While inspired by the history npm package, it’s optimized specifically for TanStack Router’s needs.

Overview

The history API provides an abstraction over browser navigation that works across different environments (browser, hash-based, memory/SSR). It handles:
  • Navigation (push, replace, go, back, forward)
  • Location tracking (pathname, search, hash, state)
  • Navigation blocking/confirmation
  • Subscription to location changes

History Types

TanStack Router supports three history implementations:

Browser History

Uses the browser’s History API with standard URLs.
This is the recommended approach for most applications. It provides:
  • Clean URLs without hash fragments
  • Full pathname support
  • Better SEO
  • Standard browser behavior
Requirements: Server must be configured to serve index.html for all routes.

Hash History

Uses hash-based routing for static hosting environments.
Use hash history when:
  • Deploying to static hosts without URL rewriting (GitHub Pages, S3)
  • Server configuration is not possible
  • Maintaining compatibility with older systems

Memory History

Uses in-memory storage for non-browser environments.
Use memory history for:
  • Server-side rendering (SSR)
  • Testing environments
  • Non-DOM platforms (React Native, Electron)

Creating a Router with History

Pass a history instance to the router:
If no history is provided, the router creates a browser history by default.

History API

All history implementations expose a common interface:

push

Navigate to a new location and add it to the history stack:

replace

Replace the current location without adding to the stack:

go

Navigate by a relative offset:

back

Navigate to the previous entry:

forward

Navigate to the next entry:

Location Properties

Access current location information:

Subscriptions

Listen to location changes:
The action type can be:
  • 'PUSH' - New entry added
  • 'REPLACE' - Current entry replaced
  • 'BACK' - Navigated backward
  • 'FORWARD' - Navigated forward
  • 'GO' - Navigated by offset

History Throttling

TanStack Router implements history throttling to prevent excessive calls to the browser’s History API.

Why Throttling?

Some browsers ignore rapid pushState/replaceState calls. Throttling ensures:
  • Consistent behavior across browsers
  • Prevention of lost state updates
  • Optimized performance for rapid navigation

How It Works

Updates are queued in a microtask and flushed asynchronously:

Forcing Immediate Flush

If you need the URL to be immediately updated:
This is rarely needed in typical applications. Block navigation to show confirmation dialogs:

Blocker Function

The blocker function receives:
  • currentLocation: Where the user currently is
  • nextLocation: Where they’re trying to go
  • action: The type of navigation (PUSH, REPLACE, etc.)
Return true to allow navigation, false to block it.

beforeunload Event

When enableBeforeUnload is true, the browser shows a confirmation dialog when:
  • Closing the tab/window
  • Navigating away from your site
  • Refreshing the page
You can also pass a function:

Multiple Blockers

You can register multiple blockers:
If any blocker returns false, navigation is cancelled.

Custom History Options

Browser History Options

Hash History Options

Memory History Options

History State

Pass custom state with navigation:

State Structure

TanStack Router adds internal state properties:
Avoid using keys starting with __TSR to prevent conflicts.

Creating Href URLs

Generate URLs for use in links:
This respects the history type and any custom createHref logic.

Server-Side Rendering

For SSR, use memory history:
On the client, hydrate with browser history:

Testing with History

Use memory history for predictable tests:

History Length

Get the total number of entries:
Check if back navigation is possible:

Cleanup

Destroy the history instance to remove event listeners:
This is typically only needed in tests or when dynamically creating/removing routers.

Best Practices

  1. Use browser history unless you have a specific reason not to
  2. Configure server redirects to support browser history
  3. Use memory history for SSR and testing
  4. Avoid direct history manipulation - use router navigation methods instead
  5. Cleanup blockers when components unmount
  6. Test navigation using memory history
  7. Prefer router.navigate() over history methods for type safety

Migration from React Router

If you’re coming from React Router: