Overview
UI-Router Core delegates URL reading/writing toLocationServices and configuration to LocationConfig. By implementing these interfaces, you can customize how UI-Router interacts with the browser URL or use alternative URL storage mechanisms.
LocationServices Interface
TheLocationServices interface (from src/common/coreservices.ts:64-70) handles low-level URL read/write operations:
Method Signatures
LocationConfig Interface
TheLocationConfig interface (from src/common/coreservices.ts:86-93) provides URL configuration metadata:
Method Signatures
BaseLocationServices
UI-Router provides an abstract base class (from src/vanilla/baseLocationService.ts:8) to simplify implementation:Key Points
_get(): Return the current internal URL (path + search + hash, no protocol/host/port)_set(): Update the URL; update both internal state and browser URLfireAfterUpdate: If true, trigger onChange listeners after URL updates
Built-in Implementations
PushStateLocationService
Uses HTML5pushState API (from src/vanilla/pushStateLocationService.ts:10):
HashLocationService
Uses URL hash fragment (from src/vanilla/hashLocationService.ts:6):MemoryLocationService
Stores URL in memory (useful for testing or server-side rendering):BrowserLocationConfig
Standard browser-based configuration (from src/vanilla/browserLocationConfig.ts:5):Custom Implementation Example
Custom Iframe Location Service
Manage routing within an iframe with parent window communication:Custom Electron Location Service
For Electron apps without real URLs:Creating a Location Plugin
Package your location service as a plugin:Initializing with Custom Services
Provide services directly to the UIRouter constructor:Testing with MemoryLocationService
Best Practices
- Extend BaseLocationServices - Reduces boilerplate; only implement
_get()and_set() - Handle cleanup properly - Remove event listeners in
dispose() - Support both push and replace - Respect the
replaceparameter in_set() - Fire onChange correctly - Call listeners when URL changes externally
- Parse URLs consistently - Use UI-Router’s utility functions for URL parsing
- Document configuration options - Clearly explain baseHref, hashPrefix, html5Mode behavior
- Test thoroughly - Use MemoryLocationService for unit tests
Reference
- Source:
src/common/coreservices.ts - Source:
src/vanilla/baseLocationService.ts - Source:
src/vanilla/pushStateLocationService.ts - Source:
src/vanilla/hashLocationService.ts - Source:
src/vanilla/browserLocationConfig.ts