Challenge - 5 Problems
Next.js Redirect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when this Next.js redirect function runs?
Consider this Next.js server action code snippet that uses the redirect function. What will the user experience after this code runs?
NextJS
import { redirect } from 'next/navigation'; export default function Page() { redirect('/dashboard'); return <p>Loading...</p>; }
Attempts:
2 left
💡 Hint
Think about how the redirect function interrupts rendering in Next.js server components.
✗ Incorrect
In Next.js, calling redirect() in a server component immediately stops rendering and sends a redirect response to the browser. So the user never sees the returned JSX.
📝 Syntax
intermediate2:00remaining
Which option correctly imports and uses the redirect function in Next.js?
You want to redirect users to '/login' inside a server component. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the official Next.js docs for the correct import path for redirect.
✗ Incorrect
The redirect function is exported from 'next/navigation' and is called directly inside the component. It does not return JSX, so returning redirect(...) is incorrect.
❓ state_output
advanced2:00remaining
What is the output when redirect is conditionally called in this Next.js server component?
Analyze this code and determine what the user sees or experiences when visiting the page with query param ?auth=false.
NextJS
import { redirect } from 'next/navigation'; export default function Page({ searchParams }) { if (searchParams.auth === 'false') { redirect('/signin'); } return <p>Welcome, authenticated user!</p>; }
Attempts:
2 left
💡 Hint
Remember how redirect interrupts rendering when called.
✗ Incorrect
If the condition is met, redirect sends a redirect response immediately, so the JSX is never rendered.
🔧 Debug
advanced2:00remaining
Why does this Next.js redirect code cause a runtime error?
Identify the cause of the error in this code snippet.
NextJS
'use client'; import { redirect } from 'next/navigation'; export default function Page() { if (typeof window !== 'undefined') { redirect('/home'); } return <p>Redirecting...</p>; }
Attempts:
2 left
💡 Hint
Think about where redirect can be used in Next.js architecture.
✗ Incorrect
redirect from 'next/navigation' only works in server components or server actions. Checking for window means this runs on client side, causing an error.
🧠 Conceptual
expert2:00remaining
What is the main difference between redirect() and useRouter().push() in Next.js?
Choose the statement that best explains how redirect() differs from useRouter().push() in Next.js routing.
Attempts:
2 left
💡 Hint
Consider where each function runs and how the browser reacts.
✗ Incorrect
redirect() is a server-side function that sends a redirect HTTP response, causing a full navigation. useRouter().push() is a client-side method that changes the URL and updates the page without a full reload.