0
0
NextJSframework~20 mins

Redirect function in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Redirect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>;
}
AThe user is immediately sent to '/dashboard' without seeing 'Loading...'.
BThe user sees 'Loading...' briefly, then is redirected to '/dashboard'.
CThe page shows 'Loading...' and does not redirect.
DThe code throws a runtime error because redirect cannot be used here.
Attempts:
2 left
💡 Hint
Think about how the redirect function interrupts rendering in Next.js server components.
📝 Syntax
intermediate
2: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?
A
import { redirect } from 'next/router';

export default function Page() {
  redirect('/login');
  return null;
}
B
import { redirect } from 'next/navigation';

export default function Page() {
  redirect('/login');
  return null;
}
C
import redirect from 'next/router';

export default function Page() {
  redirect('/login');
  return null;
}
D
import { redirect } from 'next/navigation';

export default function Page() {
  return redirect('/login');
}
Attempts:
2 left
💡 Hint
Check the official Next.js docs for the correct import path for redirect.
state_output
advanced
2: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>;
}
AUser is redirected to '/signin' immediately if auth is 'false'.
BUser sees 'Welcome, authenticated user!' even if auth is 'false'.
CThe page crashes with a runtime error because redirect is conditional.
DUser sees a blank page with no content.
Attempts:
2 left
💡 Hint
Remember how redirect interrupts rendering when called.
🔧 Debug
advanced
2: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>;
}
Aredirect must be called inside useEffect, not directly in the component.
Bredirect requires an async function but Page is not async.
Credirect is called on the client side, but it only works in server components.
Dredirect requires a full URL, not a relative path.
Attempts:
2 left
💡 Hint
Think about where redirect can be used in Next.js architecture.
🧠 Conceptual
expert
2: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.
Aredirect() and useRouter().push() are interchangeable and behave the same way.
Bredirect() changes the URL without reloading; useRouter().push() reloads the entire page from the server.
Credirect() is used only in client components; useRouter().push() is used only in server components.
Dredirect() runs on the server and sends an HTTP redirect response; useRouter().push() runs on the client and changes the URL without a full reload.
Attempts:
2 left
💡 Hint
Consider where each function runs and how the browser reacts.