How Guards Work
Guards are executed:- Before route rendering: Guards run during the route’s loader phase
- In order: Guards in the array are evaluated sequentially
- Short-circuit on non-pass: The first guard that returns
hidden()orredirectTo()stops execution - Once per navigation: Re-evaluated when navigating to the route
Guard Function Type
Basic Usage
Module-Level Guards
Apply guards to an entire module and all its resources:Resource-Level Guards
Apply guards to individual resources:Composable Guards
Guards are composable - define reusable guard functions:Async Guards
Guards can be async for permission checks or feature flags:Accessing Context Data
Guards receive thecontext object from AppShell’s contextData prop:
Guard Results
Guards must return one of three result types:pass()
Allow access and continue to the next guard or render the component.
hidden()
Deny access and show a 404 Not Found page.
redirectTo(path)
Redirect the user to another path.
Effect on Navigation
When guards deny access:- Navigation: The route is not accessible via direct URL or client-side navigation
- Sidebar: Hidden routes do not appear in navigation menus
- Command Palette: Hidden routes are excluded from search results
- 404 Behavior:
hidden()returns a 404 response - Redirects:
redirectTo()performs a client-side redirect
Common Patterns
Authentication Check
Role-Based Access
Feature Flag
Permission Check
Next Steps
- pass() - Allow access to routes
- hidden() - Deny access and show 404
- redirectTo() - Redirect to another path