Skip to main content

createHashRouter

Create a new data router that manages the application path via the URL hash. This is useful for web applications that cannot configure the server to direct all traffic to the React Router application.

Function Signature

Route Configuration

The routes parameter accepts the same RouteObject[] configuration as createBrowserRouter. See the createBrowserRouter route configuration for complete details.

Options Parameter

Return Type

Returns an initialized DataRouter instance to be passed to <RouterProvider>.

Examples

Basic Usage

With Loaders and Actions

With Error Boundaries

With Basename

With Lazy Loading

Static File Hosting Example

With Hydration Data

Use Cases

When to Use createHashRouter

Use createHashRouter when:
  • Static file hosting: Deploying to services like GitHub Pages, AWS S3, or other static hosts
  • No server configuration: Cannot configure server to handle client-side routes
  • Legacy constraints: Working with legacy systems or CDNs that don’t support rewrite rules
  • File protocol: App runs via file:// protocol (e.g., Electron without custom protocol)
  • Fallback option: Server configuration is complex or impossible
  • Simple deployment: Want to avoid server configuration entirely

Advantages

  • No server configuration needed: Works on any static file host
  • All data router features: Full support for loaders, actions, lazy loading, etc.
  • Simple deployment: Just upload files and go
  • Legacy compatibility: Works in older hosting environments
  • Works everywhere: Functions on any web server, even without special configuration

Disadvantages

  • URLs include hash: Less clean URLs (e.g., example.com/#/about vs example.com/about)
  • SEO impact: Hash portion not sent to server, may affect SEO
  • Analytics: May require special configuration for page tracking
  • Server-side rendering limitations: Hash routing doesn’t work well with SSR
  • Sharing: URLs with hashes are less intuitive for users

How Hash Routing Works

The hash router uses the URL hash fragment to store the route:
Key characteristics:
  1. Client-side only: Hash portion (#/products/123) never sent to server
  2. No server changes: Server always serves index.html regardless of URL
  3. Browser history: Still uses browser back/forward buttons
  4. Anchor behavior: Overrides default anchor link behavior within app

Deployment Examples

GitHub Pages

AWS S3 Static Website

Netlify/Vercel

While these platforms support createBrowserRouter with redirects, createHashRouter works without any configuration:

SEO Considerations

Hash routing has SEO limitations:
Solutions:
  1. Use createBrowserRouter instead: Best for SEO
  2. Server-side rendering: Pre-render pages for crawlers
  3. Dynamic rendering: Detect bots and serve static HTML
  4. Sitemap submission: Help search engines discover pages

Migration from createBrowserRouter

Switching between routers is simple:
Users’ bookmarks will break, so consider:
  • Adding redirects from old URLs
  • Communicating the change
  • Updating external links

Differences from Other Routers

Analytics Configuration

Hash changes may require special tracking:

Google Analytics 4

Other Analytics

Most analytics libraries need similar configuration to track hash changes as page views.