Conditional routes are routes that render different pages or components based on certain conditions like user authentication or roles.
Click to reveal answer
beginner
How can you protect a page in Next.js so only logged-in users can access it?
You can check the user's authentication status in a client component or server component and redirect or show a login page if the user is not logged in.
Click to reveal answer
intermediate
Which Next.js feature helps you redirect users on the server side based on conditions?
The 'redirect()' function from 'next/navigation' allows server components to redirect users conditionally before rendering the page.
Click to reveal answer
intermediate
Why is it better to handle conditional routes in server components in Next.js 14+?
Handling conditional routes in server components improves performance and security by deciding access before sending the page to the browser.
Click to reveal answer
beginner
What is a simple way to show different content on a page based on user role in Next.js?
Fetch the user role in a server component and use conditional rendering to show different components or messages depending on the role.
Click to reveal answer
In Next.js, which function is used to redirect users on the server side?
Aredirect()
Bnavigate()
CuseRouter()
Dpush()
✗ Incorrect
The redirect() function from next/navigation is used in server components to redirect users before rendering.
What is the main reason to use conditional routes in Next.js?
ATo change the URL structure
BTo load different CSS files
CTo show different pages based on user state
DTo improve SEO only
✗ Incorrect
Conditional routes let you show different pages or content depending on user login or roles.
Where is it recommended to check user authentication for conditional routing in Next.js 14+?
AIn server components
BIn static JSON files
CIn CSS files
DIn client components only
✗ Incorrect
Server components can check authentication securely and redirect before sending the page.
Which hook is NOT used for conditional routing in Next.js?
AuseState
BuseEffect
CuseRouter
DuseFetch
✗ Incorrect
useFetch is not a built-in Next.js hook; useRouter is used for client-side navigation.
What happens if a user is not authenticated and tries to access a protected route with conditional routing?
AThey see the protected content anyway
BThey get redirected to login or another page
CThe page crashes
DNothing happens
✗ Incorrect
Conditional routing redirects unauthenticated users to login or a safe page.
Explain how you would implement a conditional route in Next.js to restrict access to logged-in users only.
Think about where to check login status and how to redirect.
You got /4 concepts.
Describe the benefits of handling conditional routes in server components in Next.js 14 and later.
Consider what happens before the page reaches the browser.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of conditional routes in Next.js?
easy
A. To create static pages only
B. To style pages differently
C. To fetch data from an API
D. To control access and redirect users based on conditions
Solution
Step 1: Understand conditional routes concept
Conditional routes let you decide which users can see certain pages or redirect them elsewhere.
Step 2: Identify the purpose in Next.js
Next.js uses conditional routes mainly to protect pages or redirect users based on login or roles.
Final Answer:
To control access and redirect users based on conditions -> Option D
Quick Check:
Conditional routes = control access [OK]
Hint: Think: Who should see this page? Redirect if no permission. [OK]
Common Mistakes:
Confusing conditional routes with styling or data fetching
Thinking conditional routes create static pages
Believing conditional routes only fetch data
2. Which Next.js hook is commonly used to perform client-side redirects in conditional routes?
easy
A. useState
B. useEffect
C. useRouter
D. useContext
Solution
Step 1: Identify hooks for routing
Next.js provides useRouter to access routing methods like push for redirects.
Step 2: Confirm redirect usage
While useEffect runs side effects, useRouter is needed to actually change routes.
Final Answer:
useRouter -> Option C
Quick Check:
Redirects use useRouter [OK]
Hint: Redirects need router methods, so useRouter is key. [OK]
Common Mistakes:
Using useState to redirect (it manages state only)
Confusing useEffect as the hook that changes routes
Thinking useContext handles routing
3. Given this Next.js component, what will happen if isLoggedIn is false?
import { useRouter } from 'next/router';
import { useEffect } from 'react';
export default function Dashboard() {
const router = useRouter();
const isLoggedIn = false;
useEffect(() => {
if (!isLoggedIn) {
router.push('/login');
}
}, [isLoggedIn]);
return <h1>Dashboard</h1>;
}
medium
A. The user is redirected to the /login page
B. The page reloads infinitely
C. The page shows an error
D. The user stays on the Dashboard page
Solution
Step 1: Analyze the useEffect condition
The effect checks if isLoggedIn is false, then calls router.push('/login').
Step 2: Understand router.push behavior
This causes a client-side redirect to the login page when not logged in.
Final Answer:
The user is redirected to the /login page -> Option A
Quick Check:
False login triggers redirect [OK]
Hint: False login triggers router.push to /login. [OK]
Common Mistakes:
Thinking the user stays on Dashboard despite redirect
Assuming an error occurs without redirect
Believing infinite reload happens here
4. Identify the error in this Next.js conditional route code:
import { useRouter } from 'next/router';
import { useEffect } from 'react';
export default function Profile() {
const router = useRouter();
const isAuthorized = false;
if (!isAuthorized) {
router.push('/unauthorized');
}
return <h1>Profile</h1>;
}
medium
A. Calling router.push inside the component body causes an error
B. Missing dependency array in useEffect
C. useRouter is not imported correctly
D. The component returns invalid JSX
Solution
Step 1: Check where router.push is called
router.push is called directly inside the component body, which runs on every render and is not allowed.
Step 2: Correct usage with useEffect
Redirects should be inside useEffect to avoid infinite loops or errors.
Final Answer:
Calling router.push inside the component body causes an error -> Option A
Quick Check:
Redirects must be inside useEffect [OK]
Hint: Put router.push inside useEffect, not directly in component body. [OK]
Common Mistakes:
Calling router.push directly in component body
Ignoring useEffect for side effects
Assuming missing import causes error here
5. You want to create a Next.js page that only logged-in users can access. If the user is not logged in, redirect them to '/login'. Which is the best approach?
1. Use useRouter and useEffect to check login and redirect client-side. 2. Use Next.js middleware to check authentication before rendering. 3. Render a loading state while checking login status. 4. Avoid rendering protected content until login is confirmed.
Which combination correctly applies conditional routing best practice?
hard
A. Only 1 and 2
B. All 1, 2, 3, and 4
C. Only 2 and 4
D. Only 1, 3, and 4
Solution
Step 1: Understand client-side and server-side checks
Client-side redirect with useRouter/useEffect (1) is common, but middleware (2) adds server-side protection.
Step 2: Consider user experience
Showing a loading state (3) and avoiding rendering protected content before login confirmation (4) improve UX and prevent flicker.
Step 3: Combine all for best practice
Using all four ensures secure, smooth conditional routing in Next.js.
Final Answer:
All 1, 2, 3, and 4 -> Option B
Quick Check:
Best practice = all steps combined [OK]
Hint: Combine client, server checks and loading states for best routing. [OK]
Common Mistakes:
Relying only on client-side redirects
Skipping loading states causing flicker
Ignoring server-side middleware protection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.