Skip to main content
The 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 a URLSearchParams instance for reading query parameters.
ReadonlyURLSearchParams
URLSearchParams instance with methods:
  • get(key: string): string | null
  • getAll(key: string): string[]
  • has(key: string): boolean
  • entries(): 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
  • /blognull (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 push
  • RedirectType.push / RedirectType.replace

permanentRedirect

Throw a permanent redirect (308).
string
required
Destination URL.

notFound

Throw a 404 Not Found error.
Renders the nearest not-found.tsx file.

forbidden

Throw a 403 Forbidden error (Next.js 16+).
Renders the nearest forbidden.tsx file (or error.tsx as fallback).

unauthorized

Throw a 401 Unauthorized error (Next.js 16+).
Renders the nearest unauthorized.tsx file (or error.tsx as fallback).

Shallow Routing

Update the URL without re-fetching data:
Next.js 15+ automatically intercepts history.pushState() / replaceState() calls, so direct manipulation works:

Prefetching

<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 handle basePath:

i18n Support

Use the locale parameter for internationalized routing:

Limitations

SSR hook usage: Client hooks (useRouter, usePathname, etc.) cannot be called during SSR of client components. Use them inside useEffect or event handlers.
Concurrent navigation: Multiple rapid router.push() calls may result in race conditions. Await navigation if order matters.

Source

View source code → Implementation: /home/daytona/workspace/source/packages/vinext/src/shims/navigation.ts