uni-routefinity
v0.2.0
Published
Infinite logical history with bounded page stack for uni-app routing
Maintainers
Readme
uni-routefinity
A robust routing layer for uni-app mini programs. It provides a unified navigation API, a logical route history, and predictable fallback behavior when the native page stack is constrained.
Why This Library
uni-app navigation works with a native page stack that has practical limits. In medium/large apps, this can lead to inconsistent back behavior, repeated detail-page stacking, and hard-to-debug route flows.
uni-routefinity addresses this by adding a logical history layer on top of native routing while keeping the original uni.* navigation model.
Features
- Unified Promise-based routing API via
routeranduseRouter() - Logical history stack (
list,peek,findLast,clear) - Dual-stack model: physical stack + graph stack for fallback past hard limits
- Automatic
navigateTo -> redirectTodowngrade near stack limits - Native-first back strategy with logical-history synchronization
- Setup-once auto reconcile on page
onShow(enabled by default) - Optional route-stack graph logging through
onLog
Installation
pnpm add uni-routefinityQuick Start
import { setupUniRouter, useRouter } from "uni-routefinity";
// alias: router.setup(options) also works
setupUniRouter({
stackSafeLimit: 9,
pageHardLimit: 5,
debounceMs: 400,
protectedPaths: ["/pages/user/home/index", "/pages/user/mine/index"],
autoReconcileOnShow: true,
onLog: (graph, history) => console.log(graph)
});
const router = useRouter();
await router.navigateTo("/pages-user-course/course-detail/index", {
course_id: "2037xxxx"
});
await router.navigateBack();API
Setup
setupUniRouter(policy?: RoutefinityOptions): voidrouter.setup(policy?: RoutefinityOptions): void(alias)
Notes:
- Idempotent: calling it multiple times only initializes once.
- Should be called during app bootstrap.
Router Methods
router.navigateTo(url: string, params?: Record<string, unknown>): Promise<void>router.redirectTo(url: string, params?: Record<string, unknown>): Promise<void>router.reLaunch(url: string, params?: Record<string, unknown>): Promise<void>router.switchTab(url: string): Promise<void>router.navigateBack(delta = 1): Promise<void>router.reconcileWithNativePages(reason?: string, force?: boolean): boolean
History Methods
router.history.list(): RouteSnapshot[]— physical stack, aligned with nativerouter.history.listForGraph(): RouteSnapshot[]— graph stack; longer thanlist()when stack limit caused redirects to swap the toprouter.history.peek(): RouteSnapshot | undefinedrouter.history.findLast(path: string): RouteSnapshot | undefinedrouter.history.clear(): voidrouter.history.reconcile(reason?: string, force?: boolean): boolean
Configuration
setupUniRouter accepts RoutefinityOptions:
| Field | Type | Default | Description |
| --------------------- | -------------------------- | ----------- | -------------------------------------------------------------------- |
| stackSafeLimit | number | 9 | Soft threshold for switching navigateTo to redirectTo |
| pageHardLimit | number | 5 | Hard limit threshold for downgrade |
| debounceMs | number | 400 | Debounce window for repeated navigateTo |
| protectedPaths | string[] | [] | Back fallback to these paths uses reLaunch instead of redirectTo |
| autoReconcileOnShow | boolean | true | Auto patch page onShow to reconcile logical/native stacks |
| onLog | (graph, history) => void | undefined | Debug hook for route graph and snapshots |
Routing Strategy
- Duplicate
navigateToin a short window is debounced. - Same path with different params uses
redirectToand replaces the logical current entry. - When native stack depth approaches configured limits,
navigateTodegrades toredirectTo. The logical stack appliesreplaceCurrentto mirror the physical frame, and the graph stack additionally records the swapped-out entry so the access chain is preserved for debugging and back fallback. - If native and logical stacks diverge, history can be realigned from
getCurrentPages()before route decisions. navigateBackprefers native back when target alignment is verified; on success, logical history is synchronized.- When the graph stack is longer than the physical stack (hard limit caused several top swaps),
navigateBackredirects one step at a time back through the graph chain, until physical and graph stacks align and the system back path resumes. - Back fallback to a path in
protectedPathsusesreLaunch, otherwiseredirectTo. - System swipe-back is reconciled automatically through setup-installed page
onShowpatch.
Migration Notes
Current public API:
setupUniRouter(...)routeruseRouter()
Removed legacy exports:
setupRoutefinityappRoutergetRouteHistoryrouteHistory
Development
pnpm install
pnpm run check
pnpm run buildLicense
MIT
