Next.js Discord

Discord Forum

Difference between `NextResponse.redirect()` and `{ redirect } from 'next/navigation'`

Answered
American black bear posted this in #help-forum
Open in Discord
Avatar
American black bearOP
import { redirect } from "next/navigation"
...
redirect('https://nextjs.org')

import { NextResponse } from 'next/server';
...
NextResponse.redirect('https://nextjs.org')

Besides syntax (and optionally providing status/headers with the latter), is there any practical difference between using the two as above?
Answered by Sun bear
redirect() is used inside page components, while NextResponse.redirect() is used inside middleware, route handlers or inside the API folder.
View full answer

2 Replies

Avatar
Sun bear
redirect() is used inside page components, while NextResponse.redirect() is used inside middleware, route handlers or inside the API folder.
Answer
Avatar
American black bearOP
Perfect, thank you very much for that clarification!