createHashRouter
Create a new data router that manages the application path via the URLhash. This is useful for web applications that cannot configure the server to direct all traffic to the React Router application.
Function Signature
Route Configuration
Theroutes parameter accepts the same RouteObject[] configuration as createBrowserRouter. See the createBrowserRouter route configuration for complete details.
Options Parameter
Return Type
Returns an initializedDataRouter 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
UsecreateHashRouter 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/#/aboutvsexample.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:- Client-side only: Hash portion (
#/products/123) never sent to server - No server changes: Server always serves
index.htmlregardless of URL - Browser history: Still uses browser back/forward buttons
- Anchor behavior: Overrides default anchor link behavior within app
Deployment Examples
GitHub Pages
AWS S3 Static Website
Netlify/Vercel
While these platforms supportcreateBrowserRouter with redirects, createHashRouter works without any configuration:
SEO Considerations
Hash routing has SEO limitations:- Use
createBrowserRouterinstead: Best for SEO - Server-side rendering: Pre-render pages for crawlers
- Dynamic rendering: Detect bots and serve static HTML
- Sitemap submission: Help search engines discover pages
Migration from createBrowserRouter
Switching between routers is simple:- Adding redirects from old URLs
- Communicating the change
- Updating external links
Differences from Other Routers
| Feature | createHashRouter | createBrowserRouter | createMemoryRouter |
|---|---|---|---|
| URL Format | #/path | /path | N/A |
| Server Config | ✗ Not needed | ✓ Required | ✗ Not needed |
| SEO Friendly | ✗ No | ✓ Yes | N/A |
| Clean URLs | ✗ No | ✓ Yes | N/A |
| Static Hosting | ✓ Perfect | ✗ Needs config | N/A |
| SSR Support | Limited | ✓ Full | ✓ Full |
| Browser History | ✓ Yes | ✓ Yes | ✗ In-memory |
| Production Use | ✓ Fallback | ✓ Recommended | ✗ Testing only |
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.Related
<RouterProvider>- Component to render the routercreateBrowserRouter- Browser history-based routing (recommended for most apps)createMemoryRouter- In-memory routing for testing<HashRouter>- Declarative version for simple use cases