Client Navigation
Client Navigation
Use @dex/router/client for client-side navigation, prefetching, concurrent route transitions, error boundaries, subdomain routing, and SSG prerender context.
<Link /> Component
<Link /> renders an accessible <a> tag, intercepts in-app clicks via the History API, rewrites subdomain hrefs, validates URL schemes, and supports Data Saver-aware prefetching:
import { Link } from '@dex/router/client'
export default function Nav() {
return (
<nav>
{/* Accepts `to` or `href`; default prefetch="intent" (65ms hover debounce + focus) */}
<Link to="/" prefetch="intent">Home</Link>
{/* Prefetches immediately when the link mounts */}
<Link href="/about" prefetch="render">About</Link>
{/* Disable prefetching (`"none"` or `false`) */}
<Link to="/admin" prefetch="none">Admin</Link>
{/* Replace current history entry */}
<Link to="/login" replace>Login</Link>
{/* External link: automatically receives rel="noopener noreferrer" */}
<Link to="https://example.com" target="_blank">External Docs</Link>
</nav>
)
}Destination Validation & Security
- Required
toorhref:<Link />accepts eithertoorhref. If neither is astring, it throws[dex-router] <Link> requires a valid "to" or "href" destination prop.at runtime. - Unsafe scheme neutralization:
hasUnsafeScheme()strips control characters/whitespace and blocksjavascript:,data:,vbscript:, unknown non-allowlisted schemes (onlyhttp,https,mailto,tel, and relative paths are allowed), protocol-relative//, and/\bypasses. Unsafe links renderhref="#"and cancel clicks. target="_blank"hardening: Whentarget="_blank"is set,<Link />automatically mergesnoopenerandnoreferrerintorel.- Subdomain href rewriting: When a
RouterContextis mounted withsubdomains,<Link />rewrites destination paths throughctx.resolveHref(to)so links on a subdomain omit the internal mount prefix (for example/blog/post-1renders ashref="/post-1"onblog.example.com). - Native fallback: External URLs, modifier-key clicks (
Cmd/Ctrl/Shift/Alt), non-_selftargets, or<Link />rendered without aRouterContextbehave as standard<a>navigation.
[!TIP] Data Saver Aware: When
navigator.connection.saveData === true, Dex automatically disables"intent"and"render"prefetching to conserve user bandwidth.
<FileRouter /> & <RouterErrorBoundary />
<FileRouter /> manages route matching, async module loading, concurrent React transitions, and error handling:
import { FileRouter } from '@dex/router/client'
import { routes } from '@core/router/.generated/routes'
import { layouts } from '@core/router/.generated/layouts'
import GlobalLayout from '../../web/layouts/global'
export function App() {
return (
<FileRouter
routes={routes}
layouts={layouts}
GlobalLayout={GlobalLayout}
subdomains={{ blog: '/blog' }}
loading={<div>Loading...</div>}
notFound={<div>Page Not Found</div>}
error={({ error }) => <div>Failed to load route: {String(error)}</div>}
errorBoundary={({ error, reset }) => (
<div role="alert">
<h2>Render error</h2>
<p>{error instanceof Error ? error.message : String(error)}</p>
<button type="button" onClick={reset}>Retry</button>
</div>
)}
/>
)
}Concurrent Transitions & Module Caching
- Zero-flicker
useTransition: When navigating between routes,<FileRouter />keeps the previous page rendered until the next page and layout modules finish loading, then commits the swap inside ReactstartTransition. UseuseIsNavigating()to display a subtle progress indicator while transitioning. - Error-evicting module cache: Page and layout modules are cached in memory (
pageModuleCache,layoutModuleCache) for instant repeat visits. If a dynamic import fails due to a transient network error, the failed entry is automatically deleted from the cache so subsequent navigation attempts can retry cleanly.
Error Boundaries
- Built-in
<RouterErrorBoundary />:<FileRouter />wraps its rendered tree in<RouterErrorBoundary fallback={props.errorBoundary} resetKey={loc.pathname}>, catching synchronous React render errors in pages and layouts. - Automatic reset on navigation: Whenever
loc.pathnamechanges,RouterErrorBoundaryclears its error state automatically viagetDerivedStateFromProps. - Custom fallback: Pass
errorBoundary?: React.ComponentType<{ error: unknown; reset: () => void }>to<FileRouter />(or use<RouterErrorBoundary />directly) to render a custom fallback UI with aresethandler.
Subdomain Routing
You can mount route subtrees on dedicated subdomains by setting subdomains in dex.config.ts and passing the same map to <FileRouter subdomains={...} />:
// dex.config.ts
export default {
mode: 'spa',
renderStrategy: 'ssg',
subdomains: {
blog: '/blog',
docs: '/docs',
},
}- Subdomain mounting (
resolveSubdomainMount,toInternalPathname): Visitingblog.example.com/helloresolves internally to/blog/hellosoweb/pages/blog/[slug].tsxmatches seamlessly. - Clean URL bars (
toExternalPathname,toExternalHref): Visitingblog.example.com/blog/hellonormalizes the browser URL bar to/hello, and<Link to="/blog/hello">resolves tohref="/hello"onblog.example.com. - Main-domain canonical redirects (
resolveMainDomainSubdomainRedirect): Visitingexample.com/blog/helloredirects toblog.example.com/hello. - Local development safety:
localhost,*.localhost, IPv4 addresses, andhost:porthosts skip subdomain rewriting and redirects so local development continues to work athttp://localhost:7990/blog/hello.
SSG Router Context
During build-time prerendering (dexPrerender) or custom SSR with renderToString, wrap your app tree with RouterContext.Provider and createStaticRouterState(pathname, params?):
import { renderToString } from 'react-dom/server'
import { RouterContext, createStaticRouterState } from '@dex/router/client'
import BlogPost from './web/pages/blog/[slug]'
const html = renderToString(
<RouterContext.Provider value={createStaticRouterState('/blog/hello?ref=rss', { slug: 'hello' })}>
<BlogPost />
</RouterContext.Provider>
)This ensures useLocation(), useParams(), useQuery(), useRouterState(), and <Link /> work identically during prerendering without a browser window object.
Navigation & State Hooks
useNavigate()
import { useNavigate } from '@dex/router/client'
export default function LoginButton() {
const navigate = useNavigate()
const handleLogin = async () => {
navigate('/dashboard')
// Or replace current history entry:
// navigate('/dashboard', { replace: true })
}
return <button onClick={handleLogin}>Log In</button>
}useIsNavigating()
import { useIsNavigating } from '@dex/router/client'
export function GlobalNavProgress() {
const isNavigating = useIsNavigating()
if (!isNavigating) return null
return <div className="fixed top-0 left-0 right-0 h-1 bg-blue-500 animate-pulse" />
}usePrefetch()
import { usePrefetch } from '@dex/router/client'
export function QuickMenu() {
const prefetch = usePrefetch()
return (
<div onMouseEnter={() => prefetch('/heavy-dashboard')}>
Hover to warm up dashboard
</div>
)
}useParams(), useLocation(), and useQuery()
import { useParams, useLocation, useQuery } from '@dex/router/client'
export default function FilterPage() {
const { id } = useParams<{ id: string }>()
const { pathname, search, hash } = useLocation()
const query = useQuery()
const tab = query.get('tab') || 'overview'
return (
<div>
<p>Route: {pathname}{search}{hash}</p>
<p>Item: {id} / Tab: {tab}</p>
</div>
)
}<ClientOnly /> & clientOnly()
Prevent hydration mismatches for components that rely on browser-only APIs during SSG prerendering:
import { ClientOnly, clientOnly } from '@dex/router/client'
import DynamicChart from '../components/Chart'
export function Dashboard() {
return (
<ClientOnly fallback={<p>Loading chart...</p>}>
<DynamicChart />
</ClientOnly>
)
}
export const ClientChart = clientOnly(DynamicChart, <p>Loading...</p>)See Also
- Routing Rules — Route patterns and precedence
- Dynamic Routes — Capturing URL parameters
- Layouts — Layout composition and page metadata
- Router API Reference — Complete API signatures