next/navigation module provides hooks and utilities for App Router navigation, including pathname/query access, programmatic navigation, and server-side redirects.
Import
Client Hooks
useRouter
Returns a router instance for programmatic navigation.Router Methods
(href: string, options?: { scroll?: boolean }) => void
Navigate to a new URL (pushes history entry).
(href: string, options?: { scroll?: boolean }) => void
Navigate to a new URL (replaces history entry).
() => void
Navigate to the previous page.
() => void
Navigate to the next page (if available).
() => void
Re-fetch the current page’s RSC stream (soft refresh).
(href: string) => void
Manually prefetch a route.
usePathname
Returns the current pathname (without basePath).string
Current pathname, e.g.,
/blog/post-1- Excludes basePath (if configured)
- Excludes query string and hash
- Updates on navigation
useSearchParams
Returns aURLSearchParams instance for reading query parameters.
ReadonlyURLSearchParams
URLSearchParams instance with methods:
get(key: string): string | nullgetAll(key: string): string[]has(key: string): booleanentries(): Iterator<[string, string]>
useParams
Returns dynamic route parameters.Record<string, string | string[]>
Dynamic route parameters, e.g.,
{ slug: 'hello-world' }- Single segment:
{ slug: 'post-1' } - Catch-all:
{ slug: ['2024', '01', 'post'] }
useSelectedLayoutSegment
Returns the active child segment one level below the current layout.string | null
/blog→null(no child)/blog/post-1→'post-1'/blog/category/tech→'category'
useSelectedLayoutSegments
Returns all active segments below the current layout.string[]
/blog→[]/blog/post-1→['post-1']/blog/category/tech→['category', 'tech']
useServerInsertedHTML
Inject HTML during SSR from client components (CSS-in-JS libraries).Server Functions
redirect
Throw a redirect response (caught by the framework).string
required
Destination URL (relative or absolute).
'push' | 'replace' | RedirectType
default:"'replace'"
Navigation type:
'replace'— 307 redirect (default)'push'— 307 redirect with history pushRedirectType.push/RedirectType.replace
permanentRedirect
Throw a permanent redirect (308).string
required
Destination URL.
notFound
Throw a 404 Not Found error.not-found.tsx file.
forbidden
Throw a 403 Forbidden error (Next.js 16+).forbidden.tsx file (or error.tsx as fallback).
unauthorized
Throw a 401 Unauthorized error (Next.js 16+).unauthorized.tsx file (or error.tsx as fallback).
Shallow Routing
Update the URL without re-fetching data:history.pushState() / replaceState() calls, so direct manipulation works:
Prefetching
Automatic (Link)
<Link> components automatically prefetch on viewport entry (250px margin).
Manual (useRouter)
Cache TTL
Prefetched entries are valid for 30 seconds. Stale entries are re-prefetched on next trigger.basePath Support
All navigation functions automatically handlebasePath:
i18n Support
Use thelocale parameter for internationalized routing:
Limitations
Source
View source code → Implementation:/home/daytona/workspace/source/packages/vinext/src/shims/navigation.ts