MENU
Routing Functions
Next.js exposes two families of routing helpers: server-side functions imported from next/navigation that work by throwing a special internal signal Next.js catches, and client-side hooks (also from next/navigation) that read or change the current URL inside interactive components. See Redirects for a narrative walkthrough of when to use which.
Server-Side Functions
These all live in next/navigation and can be called from Server Components, Server Actions, and Route Handlers. Each one works by throwing internally, so anything after the call in the same function does not execute.
| redirect(path, type?) | Stops rendering and redirects the client to path. Defaults to a temporary (307/303) redirect; pass a relative path or an absolute URL. Can also be called from Client Components, but only during rendering, not inside event handlers. |
| permanentRedirect(path, type?) | Same behavior as redirect(), but issues a permanent (308) redirect, signaling to browsers and search engines that the destination should replace the original URL in caches and indexes. |
| notFound() | Stops rendering and renders the nearest not-found.js boundary with a 404 status, instead of whatever the current segment would have rendered. |
| forbidden() | Stops rendering and renders the nearest forbidden.js boundary with a 403 status, for authenticated users who lack permission for the current resource. Requires the experimental authInterrupts config flag. |
| unauthorized() | Stops rendering and renders the nearest unauthorized.js boundary with a 401 status, for requests that aren't authenticated at all. Also requires authInterrupts. |
Client-Side Hooks
These hooks only work inside Client Components (files or subtrees marked with 'use client') because they subscribe to live client-side router state.
| useRouter() | Returns a router object with imperative methods -- push(), replace(), refresh(), back(), forward(), prefetch() -- for navigating in response to events like button clicks or form submissions. |
| usePathname() | Returns the current URL's pathname as a plain string (no query string), re-rendering the calling component whenever the route changes. Useful for highlighting the active link in a nav bar. |
| useSearchParams() | Returns a read-only, URLSearchParams-like object for the current URL's query string. Reading it opts the component out of static rendering unless wrapped in a <Suspense> boundary. |
| useParams() | Returns an object of the current route's dynamic segment values (e.g. { slug: 'hello' } for a [slug] segment), read from the closest dynamic segments above the calling component. |