redirect
The redirect
function allows you to redirect the user to another URL. If you need to redirect to a 404, you can use the notFound
function.
redirect()
Invoking the redirect()
function throws a NEXT_REDIRECT
error and terminates rendering of the route segment in which it was thrown. redirect()
can be called with a relative or an absolute URL.
app/team/[id]/page.js
import { redirect } from 'next/navigation';
async function fetchTeam(id) {
const res = await fetch('https://...');
if (!res.ok) return undefined;
return res.json();
}
export default async function Profile({ params }) {
const team = await fetchTeam(params.id);
if (!team) {
redirect('/login');
}
// ...
}
Note:
redirect()
does not require you to usereturn redirect()
due to using the TypeScriptnever
type.