The redirect function helps you send users to a different page automatically. It makes navigation smooth and controls where users go next.
Redirect function in NextJS
import { redirect } from 'next/navigation'; redirect('/target-path');
Use this inside server components or server actions in Next.js 14+.
The argument is the URL path where you want to send the user.
import { redirect } from 'next/navigation'; export default function Page() { redirect('/home'); }
import { redirect } from 'next/navigation'; export async function POST() { // After form submission redirect('/thank-you'); }
This page checks if the user is logged in. If not, it sends them to the login page. Otherwise, it shows a welcome message.
import { redirect } from 'next/navigation'; export default function Page() { // Redirect to login if user is not authenticated const isAuthenticated = false; if (!isAuthenticated) { redirect('/login'); } return <h1>Welcome to your dashboard</h1>; }
The redirect function stops the current page from rendering and sends the user to the new URL immediately.
Use redirect only in server components or server actions, not in client components.
Redirect paths can be absolute URLs or relative paths within your app.
The redirect function sends users to a different page automatically.
Use it to control navigation after actions like login or form submission.
Works only in server components or server actions in Next.js 14+.
Practice
redirect function in Next.js?Solution
Step 1: Understand the redirect function role
The redirect function is designed to change the current page to another URL automatically.Step 2: Compare with other options
Fetching data, styling, or creating routes are unrelated to redirect's purpose.Final Answer:
To send users automatically to a different page -> Option BQuick Check:
Redirect = page navigation [OK]
- Confusing redirect with data fetching
- Thinking redirect styles components
- Assuming redirect creates routes
redirect function in a Next.js server component?Solution
Step 1: Identify correct import source
The redirect function is imported from 'next/navigation' as a named import.Step 2: Check usage syntax
Calling redirect('/home') after import is correct usage.Final Answer:
import { redirect } from 'next/navigation'; redirect('/home'); -> Option AQuick Check:
Correct import and call from next/navigation [OK]
- Importing redirect from 'next/router' instead
- Using default import instead of named import
- Calling redirect without importing
import { redirect } from 'next/navigation';
export default function Page() {
const isLoggedIn = false;
if (!isLoggedIn) {
redirect('/login');
}
return <h1>Welcome</h1>;
}What will the user see when visiting this page?
Solution
Step 1: Analyze the condition for redirect
The variable isLoggedIn is false, so the condition !isLoggedIn is true, triggering redirect('/login').Step 2: Understand redirect effect in server component
Calling redirect sends the user to '/login' immediately, so the return statement is not reached.Final Answer:
The user is redirected to '/login' page automatically -> Option DQuick Check:
Redirect triggers when not logged in [OK]
- Thinking the return content still renders
- Assuming redirect causes error
- Ignoring the condition logic
import { redirect } from 'next/navigation';
export default function Page() {
redirect('/dashboard');
return <h1>Dashboard</h1>;
}Solution
Step 1: Understand redirect behavior
Calling redirect immediately stops rendering and sends a redirect response.Step 2: Check code after redirect call
Returning JSX after redirect is invalid because redirect does not allow further rendering.Final Answer:
Returning JSX after redirect call -> Option AQuick Check:
Redirect stops rendering, no return allowed after [OK]
- Thinking redirect can be called in client components
- Calling redirect without conditions is allowed
- Forgetting redirect stops rendering
Solution
Step 1: Understand the logic needed
If user is logged in, send to '/profile'; if not, send to '/login'.Step 2: Match code logic with requirement
import { redirect } from 'next/navigation'; export default function Page() { const loggedIn = false; if (!loggedIn) { redirect('/login'); } else { redirect('/profile'); } return null; } checks if not logged in, redirects to '/login', else to '/profile', matching the requirement.Final Answer:
Option C code correctly implements the redirect logic -> Option CQuick Check:
Redirect to login if not logged in, else profile [OK]
- Swapping redirect URLs for logged in and not logged in
- Using wrong condition negation
- Returning JSX after redirect
Ready to prove it?
Pass the test and this pattern moves unknown
